Create collection-panel-files actions and reducer
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Fri, 27 Jul 2018 11:56:46 +0000 (13:56 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Fri, 27 Jul 2018 11:56:46 +0000 (13:56 +0200)
Feature #13855

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/collection-panel/collection-panel-files/collection-panel-files-actions.ts [new file with mode: 0644]
src/store/collection-panel/collection-panel-files/collection-panel-files-state.ts [new file with mode: 0644]
src/store/collection-panel/collection-panel-files/collections-panel-files-reducer.ts [new file with mode: 0644]

diff --git a/src/store/collection-panel/collection-panel-files/collection-panel-files-actions.ts b/src/store/collection-panel/collection-panel-files/collection-panel-files-actions.ts
new file mode 100644 (file)
index 0000000..6997f08
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { default as unionize, ofType, UnionOf } from "unionize";
+import { CollectionPanelFilesState, CollectionPanelFile } from "./collection-panel-files-state";
+
+export const collectionPanelFilesAction = unionize({
+    SET_COLLECTION_FILES: ofType<{ files: CollectionPanelFilesState }>(),
+    TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
+    TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>()
+}, { tag: 'type', value: 'payload' });
+
+export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
\ No newline at end of file
diff --git a/src/store/collection-panel/collection-panel-files/collection-panel-files-state.ts b/src/store/collection-panel/collection-panel-files/collection-panel-files-state.ts
new file mode 100644 (file)
index 0000000..8869fd3
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export type CollectionPanelFilesState = Array<CollectionPanelFile>;
+
+export interface CollectionPanelFile {
+    parentId: string;
+    id: string;
+    name: string;
+    size: number;
+    collapsed: boolean;
+    selected: boolean;
+    type: string;
+}
\ No newline at end of file
diff --git a/src/store/collection-panel/collection-panel-files/collections-panel-files-reducer.ts b/src/store/collection-panel/collection-panel-files/collections-panel-files-reducer.ts
new file mode 100644 (file)
index 0000000..8a294e7
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { CollectionPanelFilesState } from "./collection-panel-files-state";
+import { CollectionPanelFilesAction, collectionPanelFilesAction } from "./collection-panel-files-actions";
+
+export const collectionPanelFilesReducer = (state: CollectionPanelFilesState = [], action: CollectionPanelFilesAction) => {
+    return collectionPanelFilesAction.match(action, {
+        SET_COLLECTION_FILES: data => data.files,
+        TOGGLE_COLLECTION_FILE_COLLAPSE: data => toggle(state, data.id, "collapsed"),
+        TOGGLE_COLLECTION_FILE_SELECTION: data => toggle(state, data.id, "selected"),
+        default: () => state
+    });
+};
+
+const toggle = (state: CollectionPanelFilesState, id: string, key: "collapsed" | "selected") =>
+    state.map(file => file.id === id
+        ? { ...file, [key]: !file[key] }
+        : file);
\ No newline at end of file