Create collection-panel-files actions and reducer
[arvados.git] / src / store / collection-panel / collection-panel-files / collections-panel-files-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CollectionPanelFilesState } from "./collection-panel-files-state";
6 import { CollectionPanelFilesAction, collectionPanelFilesAction } from "./collection-panel-files-actions";
7
8 export const collectionPanelFilesReducer = (state: CollectionPanelFilesState = [], action: CollectionPanelFilesAction) => {
9     return collectionPanelFilesAction.match(action, {
10         SET_COLLECTION_FILES: data => data.files,
11         TOGGLE_COLLECTION_FILE_COLLAPSE: data => toggle(state, data.id, "collapsed"),
12         TOGGLE_COLLECTION_FILE_SELECTION: data => toggle(state, data.id, "selected"),
13         default: () => state
14     });
15 };
16
17 const toggle = (state: CollectionPanelFilesState, id: string, key: "collapsed" | "selected") =>
18     state.map(file => file.id === id
19         ? { ...file, [key]: !file[key] }
20         : file);