Simplify side panel reducer
[arvados.git] / src / store / side-panel / side-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { sidePanelActions, SidePanelAction } from './side-panel-action';
6 import { SidePanelItem } from '~/components/side-panel/side-panel';
7 import { ProjectsIcon, ShareMeIcon, WorkflowIcon, RecentIcon, FavoriteIcon, TrashIcon } from "~/components/icon/icon";
8 import { Dispatch } from "redux";
9 import { push } from "react-router-redux";
10 import { favoritePanelActions } from "../favorite-panel/favorite-panel-action";
11 import { projectPanelActions } from "../project-panel/project-panel-action";
12 import { projectActions } from "../project/project-action";
13 import { getProjectUrl } from "../../models/project";
14 import { columns as projectPanelColumns } from "../../views/project-panel/project-panel";
15 import { columns as favoritePanelColumns } from "../../views/favorite-panel/favorite-panel";
16
17 export type SidePanelState = SidePanelItem[];
18
19 export const sidePanelReducer = (state: SidePanelState = sidePanelData, action: SidePanelAction) => {
20     return sidePanelActions.match(action, {
21         TOGGLE_SIDE_PANEL_ITEM_OPEN: itemId =>
22             state.map(it => ({...it, open: itemId === it.id && it.open === false})),
23         TOGGLE_SIDE_PANEL_ITEM_ACTIVE: itemId =>
24             state.map(it => ({...it, active: it.id === itemId})),
25         RESET_SIDE_PANEL_ACTIVITY: () =>
26             state.map(it => ({...it, active: false })),
27         default: () => state
28     });
29 };
30
31 export enum SidePanelIdentifiers {
32     PROJECTS = "Projects",
33     SHARED_WITH_ME = "SharedWithMe",
34     WORKFLOWS = "Workflows",
35     RECENT_OPEN = "RecentOpen",
36     FAVORITES = "Favourites",
37     TRASH = "Trash"
38 }
39
40 export const sidePanelData = [
41     {
42         id: SidePanelIdentifiers.PROJECTS,
43         name: "Projects",
44         icon: ProjectsIcon,
45         open: false,
46         active: false,
47         margin: true,
48         openAble: true,
49         activeAction: (dispatch: Dispatch, uuid: string) => {
50             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
51             dispatch(push(getProjectUrl(uuid)));
52             dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
53             dispatch(projectPanelActions.RESET_PAGINATION());
54             dispatch(projectPanelActions.REQUEST_ITEMS());
55         }
56     },
57     {
58         id: SidePanelIdentifiers.SHARED_WITH_ME,
59         name: "Shared with me",
60         icon: ShareMeIcon,
61         active: false,
62     },
63     {
64         id: SidePanelIdentifiers.WORKFLOWS,
65         name: "Workflows",
66         icon: WorkflowIcon,
67         active: false,
68     },
69     {
70         id: SidePanelIdentifiers.RECENT_OPEN,
71         name: "Recent open",
72         icon: RecentIcon,
73         active: false,
74     },
75     {
76         id: SidePanelIdentifiers.FAVORITES,
77         name: "Favorites",
78         icon: FavoriteIcon,
79         active: false,
80         activeAction: (dispatch: Dispatch) => {
81             dispatch(push("/favorites"));
82             dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
83             dispatch(favoritePanelActions.RESET_PAGINATION());
84             dispatch(favoritePanelActions.REQUEST_ITEMS());
85         }
86     },
87     {
88         id: SidePanelIdentifiers.TRASH,
89         name: "Trash",
90         icon: TrashIcon,
91         active: false,
92     }
93 ];