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