// SPDX-License-Identifier: AGPL-3.0
import { ProgressIndicatorAction, progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
-import { Dispatch } from 'redux';
-import { RootState } from '~/store/store';
-import { ServiceRepository } from '~/services/services';
-
-export interface ProgressIndicatorState {
- 'sidePanelProgress': { started: boolean };
- 'contentProgress': { started: boolean };
- // 'workbenchProgress': { started: boolean };
-}
-const initialState: ProgressIndicatorState = {
- 'sidePanelProgress': { started: false },
- 'contentProgress': { started: false },
- // 'workbenchProgress': { started: false }
-};
+export type ProgressIndicatorState = { id: string, working: boolean }[];
-export enum ProgressIndicatorData {
- SIDE_PANEL_PROGRESS = 'sidePanelProgress',
- CONTENT_PROGRESS = 'contentProgress',
- // WORKBENCH_PROGRESS = 'workbenchProgress',
-}
+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,
});
};
-// export const getProgress = () =>
-// (dispatch: Dispatch, getState: () => RootState) => {
-// const progress = getState().progressIndicator;
-// if (progress.sidePanelProgress.started || progress.contentProgress.started) {
-// dispatch(progressIndicatorActions.START_SUBMIT({ id: ProgressIndicatorData.WORKBENCH_PROGRESS }));
-// } else {
-// dispatch(progressIndicatorActions.STOP_SUBMIT({ id: ProgressIndicatorData.WORKBENCH_PROGRESS }));
-// }
-// };
+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);