merge master
[arvados-workbench2.git] / src / store / details-panel / details-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 { CommonResourceService } from "../../common/api/common-resource-service";
7 import { Dispatch } from "redux";
8 import { Resource, ResourceKind } from "../../models/resource";
9 import { RootState } from "../store";
10 import { ServiceRepository } from "../../services/services";
11
12 export const detailsPanelActions = unionize({
13     TOGGLE_DETAILS_PANEL: ofType<{}>(),
14     LOAD_DETAILS: ofType<{ uuid: string, kind: ResourceKind }>(),
15     LOAD_DETAILS_SUCCESS: ofType<{ item: Resource }>(),
16 }, { tag: 'type', value: 'payload' });
17
18 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
19
20 export const loadDetails = (uuid: string, kind: ResourceKind) =>
21     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
22         dispatch(detailsPanelActions.LOAD_DETAILS({ uuid, kind }));
23         getService(services, kind)
24             .get(uuid)
25             .then(project => {
26                 dispatch(detailsPanelActions.LOAD_DETAILS_SUCCESS({ item: project }));
27             });
28     };
29
30 const getService = (services: ServiceRepository, kind: ResourceKind) => {
31     switch (kind) {
32         case ResourceKind.PROJECT:
33             return services.projectService;
34         case ResourceKind.COLLECTION:
35             return services.collectionService;
36         default:
37             return services.projectService;
38     }
39 };
40
41
42