refs #13887 Merge branch 'origin/13887-extract-common-functionality-from-project...
[arvados.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 = {
8     creator: CollectionCreator
9 };
10
11 interface CollectionCreator {
12     opened: boolean;
13     ownerUuid: string;
14 }
15
16 const updateCreator = (state: CollectionCreatorState, creator?: Partial<CollectionCreator>) => ({
17     ...state,
18     creator: {
19         ...state.creator,
20         ...creator
21     }
22 });
23
24 const initialState: CollectionCreatorState = {
25     creator: {
26         opened: false,
27         ownerUuid: ""
28     }
29 };
30
31 export const collectionCreationReducer = (state: CollectionCreatorState = initialState, action: CollectionCreateAction) => {
32     return collectionCreateActions.match(action, {
33         OPEN_COLLECTION_CREATOR: ({ ownerUuid }) => updateCreator(state, { ownerUuid, opened: true }),
34         CLOSE_COLLECTION_CREATOR: () => updateCreator(state, { opened: false }),
35         CREATE_COLLECTION: () => updateCreator(state),
36         CREATE_COLLECTION_SUCCESS: () => updateCreator(state, { opened: false, ownerUuid: "" }),
37         default: () => state
38     });
39 };