Further rename updator -> updater
[arvados-workbench2.git] / src / store / collections / updater / collection-updater-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { default as unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7
8 import { RootState } from "../../store";
9 import { ServiceRepository } from "../../../services/services";
10 import { CollectionResource } from '../../../models/collection';
11 import { initialize } from 'redux-form';
12 import { collectionPanelActions } from "../../collection-panel/collection-panel-action";
13
14 export const collectionUpdaterActions = unionize({
15     OPEN_COLLECTION_UPDATER: ofType<{ uuid: string }>(),
16     CLOSE_COLLECTION_UPDATER: ofType<{}>(),
17     UPDATE_COLLECTION_SUCCESS: ofType<{}>(),
18 }, {
19     tag: 'type',
20     value: 'payload'
21 });
22
23
24 export const COLLECTION_FORM_NAME = 'collectionEditDialog';
25
26 export const openUpdater = (uuid: string) =>
27     (dispatch: Dispatch, getState: () => RootState) => {
28         dispatch(collectionUpdaterActions.OPEN_COLLECTION_UPDATER({ uuid }));
29         const item = getState().collectionPanel.item;
30         if(item) {
31             dispatch(initialize(COLLECTION_FORM_NAME, { name: item.name, description: item.description }));
32         }
33     };
34
35 export const updateCollection = (collection: Partial<CollectionResource>) =>
36     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37         const { uuid } = getState().collections.updator;
38         return services.collectionService
39             .update(uuid, collection)
40             .then(collection => {
41                     dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection as CollectionResource }));
42                     dispatch(collectionUpdaterActions.UPDATE_COLLECTION_SUCCESS());
43                 }
44             );
45     };
46
47 export type CollectionUpdaterAction = UnionOf<typeof collectionUpdaterActions>;