1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { CollectionPanelFilesState, CollectionPanelFile } from "./collection-panel-files-state";
6 import { CollectionPanelFilesAction, collectionPanelFilesAction } from "./collection-panel-files-actions";
7 import { stat } from "fs";
9 const initialState: CollectionPanelFilesState = [{
16 parentId: 'Directory 1',
19 name: 'Directory 1.1',
23 parentId: 'Directory 1',
36 parentId: 'Directory 2',
39 name: 'Directory 2.1',
43 parentId: 'Directory 2.1',
45 id: 'Directory 2.1.1',
46 name: 'Directory 2.1.1',
50 parentId: 'Directory 2.1.1',
52 id: 'Directory 2.1.1.1',
53 name: 'Directory 2.1.1.1',
58 export const collectionPanelFilesReducer = (state: CollectionPanelFilesState = initialState, action: CollectionPanelFilesAction) => {
59 return collectionPanelFilesAction.match(action, {
60 SET_COLLECTION_FILES: data => data.files,
61 TOGGLE_COLLECTION_FILE_COLLAPSE: data => toggleCollapsed(state, data.id),
62 TOGGLE_COLLECTION_FILE_SELECTION: data => toggleSelected(state, data.id),
63 SELECT_ALL_COLLECTION_FILES: () => state.map(file => ({...file, selected: true})),
64 UNSELECT_ALL_COLLECTION_FILES: () => state.map(file => ({...file, selected: false})),
69 const toggleCollapsed = (state: CollectionPanelFilesState, id: string) =>
70 state.map(file => file.id === id
71 ? { ...file, collapsed: !file.collapsed }
74 const toggleSelected = (state: CollectionPanelFilesState, id: string) =>
75 toggleAncestors(toggleDescendants(state, id), id);
77 const toggleDescendants = (state: CollectionPanelFilesState, id: string) => {
78 const ids = getDescendants(state)({ id }).map(file => file.id);
80 const selected = !state.find(f => f.id === ids[0])!.selected;
81 return state.map(file => ids.some(id => file.id === id) ? { ...file, selected } : file);
86 const toggleAncestors = (state: CollectionPanelFilesState, id: string): CollectionPanelFile[] => {
87 const file = state.find(f => f.id === id);
89 const selected = state
90 .filter(f => f.parentId === file.parentId)
91 .every(f => f.selected);
93 const newState = state.map(f => f.id === file.parentId ? { ...f, selected } : f);
94 return toggleAncestors(newState, file.parentId || "");
100 const getDescendants = (state: CollectionPanelFilesState) => ({ id }: { id: string }): CollectionPanelFile[] => {
101 const root = state.find(f => f.id === id);
103 return [root].concat(...state.filter(f => f.parentId === id).map(getDescendants(state)));
104 } else { return []; }