X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/12fbb73269d10424383cdbd712201498cc8d013a..e5e090c801a015f04c04327a176faf54ba3650b7:/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 7e1c2362..dbd1beb3 100644 --- a/src/store/progress-indicator/progress-indicator-reducer.ts +++ b/src/store/progress-indicator/progress-indicator-reducer.ts @@ -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);