Implement reducer and state mapping functions
[arvados-workbench2.git] / src / views-components / collection-panel-files / collection-panel-files.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { CollectionPanelFiles as Component, CollectionPanelFilesProps } from "../../components/collection-panel-files/collection-panel-files";
7 import { RootState } from "../../store/store";
8 import { TreeItemStatus, TreeItem } from "../../components/tree/tree";
9 import { CollectionPanelFile } from "../../store/collection-panel/collection-panel-files/collection-panel-files-state";
10 import { FileTreeData } from "../../components/file-tree/file-tree-data";
11 import { Dispatch } from "redux";
12 import { collectionPanelFilesAction } from "../../store/collection-panel/collection-panel-files/collection-panel-files-actions";
13
14 const mapStateToProps = (state: RootState): Pick<CollectionPanelFilesProps, "items"> => ({
15     items: state.collectionPanelFiles
16         .filter(f => f.parentId === undefined)
17         .map(fileToTreeItem(state.collectionPanelFiles))
18 });
19
20 const mapDispatchToProps = (dispatch: Dispatch): Pick<CollectionPanelFilesProps, 'onCollapseToggle' | 'onSelectionToggle'> => ({
21     onCollapseToggle: (id) => dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id })),
22     onSelectionToggle: (event, item) => dispatch(collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({id: item.id})),
23 });
24
25
26 export const CollectionPanelFiles = connect(mapStateToProps, mapDispatchToProps)(Component);
27
28 const fileToTreeItem = (files: CollectionPanelFile[]) => (file: CollectionPanelFile): TreeItem<FileTreeData> => {
29     return {
30         active: false,
31         data: {
32             name: file.name,
33             size: file.size,
34             type: file.type
35         },
36         id: file.id,
37         items: files
38             .filter(f => f.parentId === file.id)
39             .map(fileToTreeItem(files)),
40         open: !file.collapsed,
41         selected: file.selected,
42         status: TreeItemStatus.LOADED
43     };
44 };