left-side-panel-small-refactor
[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
7 import actions, { SidePanelAction } from './side-panel-action';
8 import { SidePanelItem } from '../../components/side-panel/side-panel';
9
10 export type SidePanelState = SidePanelItem[];
11
12 const sidePanelReducer = (state: SidePanelState = sidePanelData, action: SidePanelAction) => {
13     if (state.length === 0) {
14         return sidePanelData;
15     } else {
16         return actions.match(action, {
17             TOGGLE_SIDE_PANEL_ITEM_OPEN: itemId => state.map(it => itemId === it.id && it.open === false ? {...it, open: true} : {...it, open: false}),
18             TOGGLE_SIDE_PANEL_ITEM_ACTIVE: itemId => {
19                 const sidePanel = _.cloneDeep(state);
20                 resetSidePanelActivity(sidePanel);
21                 sidePanel.map(it => {
22                     if (it.id === itemId) {
23                         it.active = true;
24                     }
25                 });
26                 return sidePanel;
27             },
28             RESET_SIDE_PANEL_ACTIVITY: () => {
29                 const sidePanel = _.cloneDeep(state);
30                 resetSidePanelActivity(sidePanel);
31                 return sidePanel;
32             },
33             default: () => state
34         });
35     }
36 };
37
38 export const sidePanelData = [
39     {
40         id: "1",
41         name: "Projects",
42         icon: "fas fa-th fa-fw",
43         open: false,
44         active: false,
45     },
46     {
47         id: "2",
48         name: "Shared with me",
49         icon: "fas fa-users fa-fw",
50         active: false,
51     },
52     {
53         id: "3",
54         name: "Workflows",
55         icon: "fas fa-cogs fa-fw",
56         active: false,
57     },
58     {
59         id: "4",
60         name: "Recent open",
61         icon: "icon-time fa-fw",
62         active: false,
63     },
64     {
65         id: "5",
66         name: "Favorites",
67         icon: "fas fa-star fa-fw",
68         active: false,
69     },
70     {
71         id: "6",
72         name: "Trash",
73         icon: "fas fa-trash-alt fa-fw",
74         active: false,
75     }
76 ];
77
78 function resetSidePanelActivity(sidePanel: SidePanelItem[]) {
79     for (const t of sidePanel) {
80         t.active = false;
81     }
82 }
83
84 export default sidePanelReducer;