init load tags for collection
[arvados.git] / src / store / collection-panel / collection-panel-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { ResourceKind } from "../../models/resource";
8 import { CollectionResource } from "../../models/collection";
9 import { RootState } from "../store";
10 import { ServiceRepository } from "../../services/services";
11 import { LinkClass, LinkResource } from "../../models/link";
12
13 export const collectionPanelActions = unionize({
14     LOAD_COLLECTION: ofType<{ uuid: string, kind: ResourceKind }>(),
15     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
16     LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
17     LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: LinkResource[] }>(),
18     CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
19     CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: LinkResource }>()
20 }, { tag: 'type', value: 'payload' });
21
22 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
23
24 export const loadCollection = (uuid: string, kind: ResourceKind) =>
25     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid, kind }));
27         return services.collectionService
28             .get(uuid)
29             .then(item => {
30                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: item as CollectionResource }));
31             });
32     };
33
34 export const loadCollectionTags = (uuid: string) => 
35     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36         dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
37         return services.tagService
38             .list(uuid)
39             .then(tags => {
40                 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
41             });
42     };
43
44
45 export const createCollectionTag = (uuid: string, data: {}) => 
46     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47         const linkResource = {
48             key: 'testowanie',
49             value: 'by Arturo'
50         };
51
52         dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data: linkResource }));
53         return services.tagService
54             .create(uuid, linkResource)
55             .then(tag => {
56                 console.log('tag: ', tag);
57                 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
58             });
59     };