140cbbea45e4bf423eda511d9a393133507f0334
[arvados-workbench2.git] / src / store / collections / creator / collection-creator-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { collectionCreateActions, CollectionCreateAction } from './collection-creator-action';
6
7 export type CollectionCreatorState = CollectionCreator;
8
9 interface CollectionCreator {
10     opened: boolean;
11     ownerUuid: string;
12 }
13
14 const updateCreator = (state: CollectionCreatorState, creator?: Partial<CollectionCreator>) => ({
15     ...state,
16     ...creator
17 });
18
19 const initialState: CollectionCreatorState = {
20     opened: false,
21     ownerUuid: ''
22 };
23
24 export const collectionCreatorReducer = (state: CollectionCreatorState = initialState, action: CollectionCreateAction) => {
25     return collectionCreateActions.match(action, {
26         OPEN_COLLECTION_CREATOR: ({ ownerUuid }) => updateCreator(state, { ownerUuid, opened: true }),
27         CLOSE_COLLECTION_CREATOR: () => updateCreator(state, { opened: false }),
28         CREATE_COLLECTION: () => updateCreator(state),
29         CREATE_COLLECTION_SUCCESS: () => updateCreator(state, { opened: false, ownerUuid: "" }),
30         default: () => state
31     });
32 };