Merge branch '14102-actions-repository'
[arvados-workbench2.git] / src / store / resources / resources-actions.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 '~/common/unionize';
6 import { Resource, extractUuidKind } from '~/models/resource';
7 import { Dispatch } from 'redux';
8 import { RootState } from '~/store/store';
9 import { ServiceRepository } from '~/services/services';
10 import { getResourceService } from '~/services/services';
11
12 export const resourcesActions = unionize({
13     SET_RESOURCES: ofType<Resource[]>(),
14     DELETE_RESOURCES: ofType<string[]>()
15 });
16
17 export type ResourcesAction = UnionOf<typeof resourcesActions>;
18
19 export const updateResources = (resources: Resource[]) => resourcesActions.SET_RESOURCES(resources);
20
21 export const loadResource = (uuid: string) =>
22     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
23         const kind = extractUuidKind(uuid);
24         const service = getResourceService(kind)(services);
25         if (service) {
26             const resource = await service.get(uuid);
27             dispatch<any>(updateResources([resource]));
28             return resource;
29         }
30         return undefined;
31     };