cr change
[arvados.git] / src / store / resources-data / resources-data-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ResourcesDataActions, resourcesDataActions } from "~/store/resources-data/resources-data-actions";
6 import { getNodeDescendantsIds, TREE_ROOT_ID } from "~/models/tree";
7 import { CollectionFileType } from "~/models/collection-file";
8
9 export interface ResourceData {
10     fileCount: number;
11     fileSize: number;
12 }
13
14 export type ResourcesDataState = {
15     [key: string]: ResourceData
16 };
17
18 export const resourcesDataReducer = (state: ResourcesDataState = {}, action: ResourcesDataActions) =>
19     resourcesDataActions.match(action, {
20         SET_FILES: ({uuid, files}) => {
21             const flattenFiles = getNodeDescendantsIds(TREE_ROOT_ID)(files).map(id => files[id]);
22             const [fileSize, fileCount] = flattenFiles.reduce(([size, cnt], f) =>
23                 f && f.value.type === CollectionFileType.FILE
24                 ? [size + f.value.size, cnt + 1]
25                 : [size, cnt]
26             , [0, 0]);
27             return {
28                 ...state,
29                 [uuid]: { fileCount, fileSize }
30             };
31         },
32         default: () => state,
33     });