Merge branch 'master' into 13905-restoring-correct-tree-state-and-panel-item-highligh...
[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 { collectionPanelFilesAction } from "./collection-panel-files/collection-panel-files-actions";
10 import { createTree } from "../../models/tree";
11 import { RootState } from "../store";
12 import { ServiceRepository } from "../../services/services";
13
14 export const collectionPanelActions = unionize({
15     LOAD_COLLECTION: ofType<{ uuid: string, kind: ResourceKind }>(),
16     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
17 }, { tag: 'type', value: 'payload' });
18
19 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
20
21 export const loadCollection = (uuid: string, kind: ResourceKind) =>
22     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
23         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid, kind }));
24         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
25         return services.collectionService
26             .get(uuid)
27             .then(item => {
28                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item }));
29                 return services.collectionFilesService.getFiles(item.uuid);
30             })
31             .then(files => {
32                 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
33             });
34     };
35
36
37