X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/6f8e509988756deb7e05a760e718d1ade164fd19..c48d10802d4ae95273f8b98e622e5df200cdc3a7:/src/store/progress-indicator/progress-indicator-reducer.ts diff --git a/src/store/progress-indicator/progress-indicator-reducer.ts b/src/store/progress-indicator/progress-indicator-reducer.ts index 190ad13f..67a06b8e 100644 --- a/src/store/progress-indicator/progress-indicator-reducer.ts +++ b/src/store/progress-indicator/progress-indicator-reducer.ts @@ -2,26 +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 interface ProgressIndicatorState { - [key: string]: { - working: boolean - }; -} +export type ProgressIndicatorState = { id: string, working: boolean }[]; -const initialState: ProgressIndicatorState = { -}; +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: id => ({ ...state, [id]: { working: true } }), - STOP: id => ({ ...state, [id]: { working: false } }), - TOGGLE: ({ id, working }) => ({ ...state, [id]: { working }}), + 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 Object.keys(state).reduce((working, k) => working ? true : state[k].working, false); + 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);