Merge branch 'master' into 14491-graph-field-is-too-big-when-no-workflows-were-chosen
[arvados-workbench2.git] / src / store / progress-indicator / progress-indicator-reducer.ts
index 7e1c2362802376092c6c458fbf78be191e2bb930..dbd1beb30dd546f48ca91856ac7c18c724ea1b6a 100644 (file)
@@ -4,31 +4,37 @@
 
 import { ProgressIndicatorAction, progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
 
-export interface ProgressIndicatorState {
-    'workbenchProgress': { started: boolean };
-    'contentProgress': { started: boolean };
-    'detailsProgress': { started: boolean };
-}
+export type ProgressIndicatorState = { id: string, working: boolean }[];
 
-const initialState: ProgressIndicatorState = {
-    'workbenchProgress': { started: false },
-    'contentProgress': { started: false },
-    'detailsProgress': { started: false }
-};
-
-export enum ProgressIndicatorData {
-    WORKBENCH_PROGRESS = 'workbenchProgress',
-    CONTENT_PROGRESS = 'contentProgress',
-    DETAILS_PROGRESS = 'detailsProgress'
-}
+const initialState: ProgressIndicatorState = [];
 
 export const progressIndicatorReducer = (state: ProgressIndicatorState = initialState, action: ProgressIndicatorAction) => {
+    const stopWorking = (id: string) => state.filter(p => p.id !== id);
+
     return progressIndicatorActions.match(action, {
-        START_SUBMIT: ({ id }) => ({ ...state, [id]: { started: true } }),
-        STOP_SUBMIT: ({ id }) => ({
-            ...state,
-            [id]: state[id] ? { ...state[id], started: false } : { started: false }
-        }),
+        START_WORKING: id => startWorking(id, state),
+        STOP_WORKING: id => stopWorking(id),
+        PERSIST_STOP_WORKING: id => state.map(p => ({
+            ...p,
+            working: p.id === id ? false : p.working
+        })),
+        TOGGLE_WORKING: ({ id, working }) => working ? startWorking(id, state) : stopWorking(id),
         default: () => state,
     });
 };
+
+const startWorking = (id: string, state: ProgressIndicatorState) => {
+    return getProgressIndicator(id)(state)
+        ? state.map(indicator => indicator.id === id
+            ? { ...indicator, working: true }
+            : indicator)
+        : state.concat({ id, working: true });
+};
+
+export function isSystemWorking(state: ProgressIndicatorState): boolean {
+    return state.length > 0 && state.reduce((working, p) => working ? true : p.working, false);
+}
+
+export const getProgressIndicator = (id: string) =>
+    (state: ProgressIndicatorState) =>
+        state.find(state => state.id === id);