15768: project copy-to-clipboard works Arvados-DCO-1.1-Signed-off-by: Lisa Knox ...
[arvados-workbench2.git] / src / store / progress-indicator / progress-indicator-reducer.ts
index f6a73472909cab80879268447584500073286b79..67a06b8e65942c18ee1222c8a7032caf03887e28 100644 (file)
@@ -2,24 +2,39 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { ProgressIndicatorAction, progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
+import { ProgressIndicatorAction, progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
 
 export type ProgressIndicatorState = { id: string, working: boolean }[];
 
 const initialState: ProgressIndicatorState = [];
 
 export const progressIndicatorReducer = (state: ProgressIndicatorState = initialState, action: ProgressIndicatorAction) => {
-    const startWorking = (id: string) => state.find(p => p.working) ? state : state.concat({ id, working: true });
     const stopWorking = (id: string) => state.filter(p => p.id !== id);
 
     return progressIndicatorActions.match(action, {
-        START: id => startWorking(id),
-        STOP: id => stopWorking(id),
-        TOGGLE: ({ id, working }) => working ? startWorking(id) : stopWorking(id),
+        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;
+    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);