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