cae42ac38456f2d4d7b8835865dcf04367941dd6
[arvados-workbench2.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 * as _ from "lodash";
6 import { sidePanelActions, SidePanelAction } from './side-panel-action';
7 import { SidePanelItem } from '../../components/side-panel/side-panel';
8 import { ProjectsIcon, ShareMeIcon, WorkflowIcon, RecentIcon, FavoriteIcon, TrashIcon } from "../../components/icon/icon";
9 import { Dispatch } from "redux";
10 import { push } from "react-router-redux";
11 import { favoritePanelActions } from "../favorite-panel/favorite-panel-action";
12
13 export type SidePanelState = SidePanelItem[];
14
15 export const sidePanelReducer = (state: SidePanelState = sidePanelData, action: SidePanelAction) => {
16     if (state.length === 0) {
17         return sidePanelData;
18     } else {
19         return sidePanelActions.match(action, {
20             TOGGLE_SIDE_PANEL_ITEM_OPEN: itemId =>
21                 state.map(it => ({...it, open: itemId === it.id && it.open === false})),
22             TOGGLE_SIDE_PANEL_ITEM_ACTIVE: itemId => {
23                 const sidePanel = _.cloneDeep(state);
24                 resetSidePanelActivity(sidePanel);
25                 sidePanel.forEach(it => {
26                     if (it.id === itemId) {
27                         it.active = true;
28                     }
29                 });
30                 return sidePanel;
31             },
32             RESET_SIDE_PANEL_ACTIVITY: () => {
33                 const sidePanel = _.cloneDeep(state);
34                 resetSidePanelActivity(sidePanel);
35                 return sidePanel;
36             },
37             default: () => state
38         });
39     }
40 };
41
42 export enum SidePanelIdentifiers {
43     PROJECTS = "Projects",
44     SHARED_WITH_ME = "SharedWithMe",
45     WORKFLOWS = "Workflows",
46     RECENT_OPEN = "RecentOpen",
47     FAVORITES = "Favourites",
48     TRASH = "Trash"
49 }
50
51 export const sidePanelData = [
52     {
53         id: SidePanelIdentifiers.PROJECTS,
54         name: "Projects",
55         icon: ProjectsIcon,
56         open: false,
57         active: false,
58         margin: true,
59         openAble: true
60     },
61     {
62         id: SidePanelIdentifiers.SHARED_WITH_ME,
63         name: "Shared with me",
64         icon: ShareMeIcon,
65         active: false,
66     },
67     {
68         id: SidePanelIdentifiers.WORKFLOWS,
69         name: "Workflows",
70         icon: WorkflowIcon,
71         active: false,
72     },
73     {
74         id: SidePanelIdentifiers.RECENT_OPEN,
75         name: "Recent open",
76         icon: RecentIcon,
77         active: false,
78     },
79     {
80         id: SidePanelIdentifiers.FAVORITES,
81         name: "Favorites",
82         icon: FavoriteIcon,
83         active: false,
84         activeAction: (dispatch: Dispatch) => {
85             dispatch(push("/favorites"));
86             dispatch(favoritePanelActions.RESET_PAGINATION());
87             dispatch(favoritePanelActions.REQUEST_ITEMS());
88         }
89     },
90     {
91         id: SidePanelIdentifiers.TRASH,
92         name: "Trash",
93         icon: TrashIcon,
94         active: false,
95     }
96 ];
97
98 function resetSidePanelActivity(sidePanel: SidePanelItem[]) {
99     for (const t of sidePanel) {
100         t.active = false;
101     }
102 }