@@ -2,56 +2,77 @@ import { useState } from 'react';
22import { Link , useNavigate } from 'react-router-dom' ;
33import { useDispatch , useSelector } from 'react-redux' ;
44import styles from './WorkflowList.module.css' ;
5- import WorkflowStatusPill from '.. /WorkflowStatusPill/WorkflowStatusPill.jsx' ;
5+ import WorkflowStatusPill from './WorkflowStatusPill/WorkflowStatusPill.jsx' ;
66import Loader from '../Loader/Loader.jsx' ;
77import dropdownSvg from '../../assets/img/dropdown.svg' ;
8- import { ROUTE , WorkflowItemsInteractionType } from '../../constants.js' ;
8+ import { ROUTE , WorkflowItemsInteractionType , WorkflowStatus } from '../../constants.js' ;
99import textContent from '../../assets/text.json' ;
1010import { api } from '../../api' ;
1111import { cancelTriggeredWorkflow , updateWorkflowDefinitions } from '../../store/actions' ;
12+ import StatusLoader from './StatusLoader/StatusLoader.jsx' ;
1213
1314const WorkflowList = ( { items, interactionType, isLoading } ) => {
1415 const dispatch = useDispatch ( ) ;
1516 const navigate = useNavigate ( ) ;
1617 const workflows = useSelector ( state => state . workflows . workflows ) ;
17- const [ isOptionsOpen , setOptionsOpen ] = useState ( false ) ;
18- const [ dropdownIdx , setDropdownIdx ] = useState ( null ) ;
18+ const [ loadingWorkflow , setLoadingWorkflow ] = useState ( { id : '' , isLoading : false } ) ;
19+ const [ dropdownOptions , setDropdownOptions ] = useState ( { id : '' , isOpen : false } ) ;
1920
2021 const handleFocusDropdown = idx => {
2122 setTimeout ( ( ) => {
22- setDropdownIdx ( idx ) ;
23- setOptionsOpen ( true ) ;
23+ setDropdownOptions ( { id : idx , isOpen : true } ) ;
2424 } , 120 ) ;
2525 } ;
2626
2727 const handleBlurDropdown = ( ) => {
2828 setTimeout ( ( ) => {
29- setDropdownIdx ( null ) ;
30- setOptionsOpen ( false ) ;
29+ setDropdownOptions ( { id : '' , isOpen : false } ) ;
3130 } , 100 ) ;
3231 } ;
3332
3433 const handleUpdateWorkflowStatus = async workflow => {
34+ setLoadingWorkflow ( { id : workflow . id , isLoading : true } ) ;
3535 const { data : workflowInstance } = await api . workflows . getWorkflowInstance ( workflow ) ;
36- if ( workflowInstance . instanceState === workflow . instanceState ) return ;
3736
38- const updatedWorkflows = workflows . map ( workflowDefinition => {
39- if ( workflowDefinition . id === workflow . id ) {
40- return { ...workflowDefinition , instanceState : workflowInstance . instanceState } ;
41- }
42- return { ...workflowDefinition } ;
43- } ) ;
37+ if ( workflowInstance . instanceState !== workflow . instanceState ) {
38+ const updatedWorkflows = workflows . map ( w => {
39+ if ( w . id !== workflow . id ) return { ...w } ;
40+ return { ...w , instanceState : workflowInstance . instanceState } ;
41+ } ) ;
42+ dispatch ( updateWorkflowDefinitions ( updatedWorkflows ) ) ;
43+ }
4444
45- dispatch ( updateWorkflowDefinitions ( updatedWorkflows ) ) ;
46- setOptionsOpen ( false ) ;
45+ setLoadingWorkflow ( { id : '' , isLoading : false } ) ;
46+ setDropdownOptions ( { id : '' , isOpen : false } ) ;
4747 } ;
4848
4949 const handleCancelWorkflow = async workflow => {
50+ setLoadingWorkflow ( { id : workflow . id , isLoading : true } ) ;
5051 const { status } = await api . workflows . cancelWorkflowInstance ( workflow ) ;
51- if ( status !== 200 ) return ;
52+ if ( status !== 200 ) {
53+ setLoadingWorkflow ( { id : '' , isLoading : false } ) ;
54+ return ;
55+ }
56+
57+ const updatedWorkflows = await Promise . all (
58+ workflows . map ( async w => {
59+ if ( w . id !== workflow . id ) return { ...w } ;
60+
61+ const { data } = await api . workflows . getWorkflowInstances ( workflow . id ) ;
62+ const relevantInstanceState = data . length > 0 ? data [ data . length - 1 ] . instanceState : WorkflowStatus . NotRun ;
63+ return {
64+ ...workflow ,
65+ instanceState : relevantInstanceState ,
66+ } ;
67+ } )
68+ ) ;
5269
70+ // // Update workflow statuses
71+ dispatch ( updateWorkflowDefinitions ( updatedWorkflows ) ) ;
5372 dispatch ( cancelTriggeredWorkflow ( workflow . id ) ) ;
54- setOptionsOpen ( false ) ;
73+
74+ setLoadingWorkflow ( { id : '' , isLoading : false } ) ;
75+ setDropdownOptions ( { id : '' , isOpen : false } ) ;
5576 } ;
5677
5778 const listStyles = {
@@ -109,7 +130,11 @@ const WorkflowList = ({ items, interactionType, isLoading }) => {
109130 { items . map ( ( item , idx ) => (
110131 < div key = { `${ item . name } ${ idx } ` } className = "list-group-item list-group-item-action" >
111132 < div style = { { display : 'flex' , flexDirection : 'row' , gap : '16px' } } >
112- < WorkflowStatusPill status = { item . instanceState } />
133+ { loadingWorkflow . isLoading && loadingWorkflow . id === item . id ? (
134+ < StatusLoader />
135+ ) : (
136+ < WorkflowStatusPill status = { item . instanceState } />
137+ ) }
113138 < h4 > { WorkflowItemsInteractionType . TRIGGER ? item . name : item . instanceName } </ h4 >
114139 </ div >
115140 < p > { item . type } </ p >
@@ -139,7 +164,7 @@ const WorkflowList = ({ items, interactionType, isLoading }) => {
139164 </ button >
140165 < div
141166 className = { `dropdown-menu dropdown-menu-right ${ styles . dropdownMenu } ` }
142- style = { isOptionsOpen && dropdownIdx === idx ? { display : 'block' } : { } }
167+ style = { dropdownOptions . isOpen && dropdownOptions . id === idx ? { display : 'block' } : { } }
143168 >
144169 < a
145170 className = { `dropdown-item ${ styles . dropdownItem } ` }
0 commit comments