-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInstanceInfo.jsx
More file actions
74 lines (61 loc) · 2.72 KB
/
InstanceInfo.jsx
File metadata and controls
74 lines (61 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import styles from './InstanceInfo.module.css';
import instanceCompletedSvg from '../../assets/img/instance-completed.svg';
import instanceCanceledSvg from '../../assets/img/instance-canceled.svg';
import textContent from '../../assets/text.json';
import { api } from '../../api';
import { useState } from 'react';
import StatusLoader from '../StatusLoader/StatusLoader.jsx';
import { useDispatch, useSelector } from 'react-redux';
import { updateWorkflowDefinitions } from '../../store/actions/workflows.action.js';
const InstanceInfo = ({ workflowId, instance }) => {
const dispatch = useDispatch();
const workflows = useSelector(state => state.workflows.workflows);
const [isStatusRefreshing, setIsStatusRefreshing] = useState(false);
const { lastCompletedStep, totalSteps, workflowStatus } = instance;
const progress = Math.min((lastCompletedStep / totalSteps) * 100, 100);
const isFailed = workflowStatus.toLowerCase() === 'failed';
const isInProgress = workflowStatus.toLowerCase() === 'in progress';
const isSuccessful = workflowStatus.toLowerCase() === 'completed';
const handleCancelInstance = async () => {
setIsStatusRefreshing(true);
await api.workflows.cancelWorkflowInstance(workflowId, instance.id);
const updatedWorkflows = workflows.map(workflow => {
if (workflow.id !== workflowId) return workflow;
const newInstanceList = workflow.instances.map(inst => {
if (inst.id !== instance.id) return inst;
const updatedInstance = { ...inst, workflowStatus: 'Canceled' };
return updatedInstance;
})
const updatedWorkflow = { ...workflow, instances: newInstanceList };
return updatedWorkflow;
})
dispatch(updateWorkflowDefinitions(updatedWorkflows));
setIsStatusRefreshing(false);
}
return (
<div key={instance.id} className={styles.instanceListRow}>
<div className={styles.cell}>
<h4>{instance.name}</h4>
</div>
<div className={styles.cell}>
{isStatusRefreshing ? (
<StatusLoader />
) : isInProgress || isFailed ? (
<>
<progress className={isFailed ? styles.progressFail : styles.progressSuccess} max={100} value={progress} />
<h5>{instance.workflowStatus}</h5>
</>
) : (
<h4><img src={isSuccessful ? instanceCompletedSvg : instanceCanceledSvg} alt={workflowStatus} />{workflowStatus}</h4>
)}
</div>
<div className={styles.cell}>
<h4>{instance.startedByName}</h4>
</div>
<div className={styles.cell}>
<button disabled={!isInProgress} onClick={() => handleCancelInstance()}>{textContent.instanceList.cancelButton}</button>
</div>
</div>
);
}
export default InstanceInfo;