Move Resource interface to models, unify ResourceKind enum
[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 { ResourceKind } from "../../models/kinds";
8 import { Dispatch } from "redux";
9 import { groupsService } from "../../services/services";
10 import { serverApi } from "../../common/api/server-api";
11 import { Resource } from "../../models/resource";
12
13 const actions = unionize({
14     TOGGLE_DETAILS_PANEL: ofType<{}>(),
15     LOAD_DETAILS: ofType<{ uuid: string, kind: ResourceKind }>(),
16     LOAD_DETAILS_SUCCESS: ofType<{ item: Resource }>(),
17 }, { tag: 'type', value: 'payload' });
18
19 export default actions;
20
21 export type DetailsPanelAction = UnionOf<typeof actions>;
22
23 export const loadDetails = (uuid: string, kind: ResourceKind) =>
24     (dispatch: Dispatch) => {
25         dispatch(actions.LOAD_DETAILS({ uuid, kind }));
26         getService(kind)
27             .get(uuid)
28             .then(project => {
29                 dispatch(actions.LOAD_DETAILS_SUCCESS({ item: project }));
30             });
31     };
32
33 const getService = (kind: ResourceKind) => {
34     switch (kind) {
35         case ResourceKind.Project:
36             return new CommonResourceService(serverApi, "groups");
37         case ResourceKind.Collection:
38             return new CommonResourceService(serverApi, "collections");
39         default:
40             return new CommonResourceService(serverApi, "");
41     }
42 };
43
44
45