forked from opensearch-project/dashboards-flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflows_reducer.ts
332 lines (317 loc) · 10.4 KB
/
workflows_reducer.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { Workflow, WorkflowDict, WorkflowTemplate } from '../../../common';
import { HttpFetchError } from '../../../../../src/core/public';
import { getRouteService } from '../../services';
const initialState = {
loading: false,
errorMessage: '',
workflows: {} as WorkflowDict,
cachedWorkflow: undefined as Workflow | undefined,
};
const WORKFLOWS_ACTION_PREFIX = 'workflows';
const GET_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/get`;
const SEARCH_WORKFLOWS_ACTION = `${WORKFLOWS_ACTION_PREFIX}/search`;
const GET_WORKFLOW_STATE_ACTION = `${WORKFLOWS_ACTION_PREFIX}/getState`;
const CREATE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/create`;
const UPDATE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/update`;
const PROVISION_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/provision`;
const DEPROVISION_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/deprovision`;
const DELETE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/delete`;
const CACHE_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/cache`;
const CLEAR_CACHED_WORKFLOW_ACTION = `${WORKFLOWS_ACTION_PREFIX}/clearCache`;
export const getWorkflow = createAsyncThunk(
GET_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response: any | HttpFetchError = await getRouteService().getWorkflow(
workflowId
);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error getting workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const searchWorkflows = createAsyncThunk(
SEARCH_WORKFLOWS_ACTION,
async (body: {}, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().searchWorkflows(body);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error searching workflows: ' + response.body.message
);
} else {
return response;
}
}
);
export const getWorkflowState = createAsyncThunk(
GET_WORKFLOW_STATE_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().getWorkflowState(workflowId);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error getting workflow state: ' + response.body.message
);
} else {
return response;
}
}
);
export const createWorkflow = createAsyncThunk(
CREATE_WORKFLOW_ACTION,
async (workflowBody: {}, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().createWorkflow(workflowBody);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error creating workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const updateWorkflow = createAsyncThunk(
UPDATE_WORKFLOW_ACTION,
async (
workflowInfo: { workflowId: string; workflowTemplate: WorkflowTemplate },
{ rejectWithValue }
) => {
const { workflowId, workflowTemplate } = workflowInfo;
const response:
| any
| HttpFetchError = await getRouteService().updateWorkflow(
workflowId,
workflowTemplate
);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error updating workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const provisionWorkflow = createAsyncThunk(
PROVISION_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().provisionWorkflow(workflowId);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error provisioning workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const deprovisionWorkflow = createAsyncThunk(
DEPROVISION_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().deprovisionWorkflow(
workflowId
);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error deprovisioning workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const deleteWorkflow = createAsyncThunk(
DELETE_WORKFLOW_ACTION,
async (workflowId: string, { rejectWithValue }) => {
const response:
| any
| HttpFetchError = await getRouteService().deleteWorkflow(workflowId);
if (response instanceof HttpFetchError) {
return rejectWithValue(
'Error deleting workflow: ' + response.body.message
);
} else {
return response;
}
}
);
export const cacheWorkflow = createAsyncThunk(
CACHE_WORKFLOW_ACTION,
async (workflow: Workflow) => {
return workflow;
}
);
// A no-op function to trigger a reducer case.
// Will clear any stored workflow in the cachedWorkflow state
export const clearCachedWorkflow = createAsyncThunk(
CLEAR_CACHED_WORKFLOW_ACTION,
async () => {}
);
const workflowsSlice = createSlice({
name: 'workflows',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
// Pending states: set state consistently across all actions
.addCase(getWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(searchWorkflows.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(createWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(updateWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(provisionWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(deprovisionWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(deleteWorkflow.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(getWorkflowState.pending, (state, action) => {
state.loading = true;
state.errorMessage = '';
})
// Fulfilled states: mutate state depending on the action type
// and payloads
.addCase(getWorkflow.fulfilled, (state, action) => {
const { workflow } = action.payload;
state.workflows = {
...state.workflows,
[workflow.id]: workflow,
};
state.loading = false;
state.errorMessage = '';
})
.addCase(searchWorkflows.fulfilled, (state, action) => {
const { workflows } = action.payload as { workflows: WorkflowDict };
state.workflows = workflows;
state.loading = false;
state.errorMessage = '';
})
.addCase(getWorkflowState.fulfilled, (state, action) => {
const { workflowId, workflowState, resourcesCreated } = action.payload;
state.workflows = {
...state.workflows,
[workflowId]: {
...state.workflows[workflowId],
state: workflowState,
resourcesCreated,
},
};
state.loading = false;
state.errorMessage = '';
})
.addCase(createWorkflow.fulfilled, (state, action) => {
const workflow = action.payload;
state.workflows = {
...state.workflows,
[workflow.id]: workflow,
};
state.loading = false;
state.errorMessage = '';
})
.addCase(updateWorkflow.fulfilled, (state, action) => {
const { workflowId, workflowTemplate } = action.payload as {
workflowId: string;
workflowTemplate: WorkflowTemplate;
};
state.workflows = {
...state.workflows,
[workflowId]: {
// only overwrite the stateless / template fields. persist any existing state (e.g., lastUpdated, lastProvisioned)
...state.workflows[workflowId],
...workflowTemplate,
},
};
state.loading = false;
state.errorMessage = '';
})
.addCase(provisionWorkflow.fulfilled, (state, action) => {
state.loading = false;
state.errorMessage = '';
})
.addCase(deprovisionWorkflow.fulfilled, (state, action) => {
state.loading = false;
state.errorMessage = '';
})
.addCase(deleteWorkflow.fulfilled, (state, action) => {
const workflowId = action.payload.id;
delete state.workflows[workflowId];
state.loading = false;
state.errorMessage = '';
})
.addCase(cacheWorkflow.fulfilled, (state, action) => {
const workflow = action.payload;
state.cachedWorkflow = workflow;
})
.addCase(clearCachedWorkflow.fulfilled, (state, action) => {
state.cachedWorkflow = undefined;
})
// Rejected states: set state consistently across all actions
.addCase(getWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(searchWorkflows.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(getWorkflowState.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(createWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(updateWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(provisionWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(deprovisionWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(deleteWorkflow.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
});
},
});
export const workflowsReducer = workflowsSlice.reducer;