3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import { createSlice } from '@reduxjs/toolkit' ;
6
+ import { createAsyncThunk , createSlice } from '@reduxjs/toolkit' ;
7
7
import {
8
8
Workflow ,
9
9
ReactFlowComponent ,
@@ -14,8 +14,10 @@ import {
14
14
initComponentData ,
15
15
WORKFLOW_STATE ,
16
16
} from '../../../common' ;
17
+ import { HttpFetchError } from '../../../../../src/core/public' ;
18
+ import { getRouteService } from '../../services' ;
17
19
18
- // TODO: remove after fetching from server- side
20
+ // TODO: remove hardcoded initial state below after fetching from server side
19
21
const id1 = generateId ( 'text_embedding_processor' ) ;
20
22
const id2 = generateId ( 'text_embedding_processor' ) ;
21
23
const id3 = generateId ( 'knn_index' ) ;
@@ -40,47 +42,86 @@ const dummyNodes = [
40
42
} ,
41
43
] as ReactFlowComponent [ ] ;
42
44
45
+ let workflows = { } as { [ workflowId : string ] : Workflow } ;
46
+ workflows [ 'workflow-1-id' ] = {
47
+ name : 'Workflow-1' ,
48
+ id : 'workflow-1-id' ,
49
+ description : 'description for workflow 1' ,
50
+ state : WORKFLOW_STATE . SUCCEEDED ,
51
+ workspaceFlowState : {
52
+ nodes : dummyNodes ,
53
+ edges : [ ] as ReactFlowEdge [ ] ,
54
+ } ,
55
+ template : { } ,
56
+ } as Workflow ;
57
+ workflows [ 'workflow-2-id' ] = {
58
+ name : 'Workflow-2' ,
59
+ id : 'workflow-2-id' ,
60
+ description : 'description for workflow 2' ,
61
+ state : WORKFLOW_STATE . FAILED ,
62
+ workspaceFlowState : {
63
+ nodes : dummyNodes ,
64
+ edges : [ ] as ReactFlowEdge [ ] ,
65
+ } ,
66
+ template : { } ,
67
+ } as Workflow ;
68
+
43
69
const initialState = {
44
- // TODO: fetch from server-side later
45
- workflows : [
46
- {
47
- name : 'Workflow-1' ,
48
- id : 'workflow-1-id' ,
49
- description : 'description for workflow 1' ,
50
- state : WORKFLOW_STATE . SUCCEEDED ,
51
- workspaceFlowState : {
52
- nodes : dummyNodes ,
53
- edges : [ ] as ReactFlowEdge [ ] ,
54
- } ,
55
- template : { } ,
56
- } ,
57
- {
58
- name : 'Workflow-2' ,
59
- id : 'workflow-2-id' ,
60
- description : 'description for workflow 2' ,
61
- state : WORKFLOW_STATE . FAILED ,
62
- workspaceFlowState : {
63
- nodes : dummyNodes ,
64
- edges : [ ] as ReactFlowEdge [ ] ,
65
- } ,
66
- template : { } ,
67
- } ,
68
- ] as Workflow [ ] ,
69
70
loading : false ,
71
+ errorMessage : '' ,
72
+ workflows,
70
73
} ;
71
74
75
+ // TODO: uncomment when workflow fetching is working
76
+ // const initialState = {
77
+ // loading: false,
78
+ // errorMessage: '',
79
+ // workflows: {} as { [workflowId: string]: Workflow },
80
+ // };
81
+
82
+ const WORKFLOWS_PREFIX = 'workflows' ;
83
+ const GET_WORKFLOW_ACTION = `${ WORKFLOWS_PREFIX } /getWorkflow` ;
84
+
85
+ export const getWorkflow = createAsyncThunk (
86
+ GET_WORKFLOW_ACTION ,
87
+ async ( workflowId : string , { rejectWithValue } ) => {
88
+ const response : any | HttpFetchError = await getRouteService ( ) . getWorkflow (
89
+ workflowId
90
+ ) ;
91
+ if ( response instanceof HttpFetchError ) {
92
+ return rejectWithValue (
93
+ 'Error getting workflow: ' + response . body . message
94
+ ) ;
95
+ } else {
96
+ return response ;
97
+ }
98
+ }
99
+ ) ;
100
+
72
101
const workflowsSlice = createSlice ( {
73
102
name : 'workflows' ,
74
103
initialState,
75
- reducers : {
76
- setWorkflows ( state , action ) {
77
- state . workflows = action . payload ;
78
- } ,
79
- setLoading ( state , action ) {
80
- state . loading = action . payload ;
81
- } ,
104
+ reducers : { } ,
105
+ extraReducers : ( builder ) => {
106
+ builder
107
+ . addCase ( getWorkflow . pending , ( state , action ) => {
108
+ state . loading = true ;
109
+ state . errorMessage = '' ;
110
+ } )
111
+ . addCase ( getWorkflow . fulfilled , ( state , action ) => {
112
+ const workflow = action . payload ;
113
+ state . workflows = {
114
+ ...state . workflows ,
115
+ [ workflow . id ] : workflow ,
116
+ } ;
117
+ state . loading = false ;
118
+ state . errorMessage = '' ;
119
+ } )
120
+ . addCase ( getWorkflow . rejected , ( state , action ) => {
121
+ state . errorMessage = action . payload as string ;
122
+ state . loading = false ;
123
+ } ) ;
82
124
} ,
83
125
} ) ;
84
126
85
127
export const workflowsReducer = workflowsSlice . reducer ;
86
- export const { setWorkflows, setLoading } = workflowsSlice . actions ;
0 commit comments