Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[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 { extractUuidKind, Resource } 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 deleteResources = (resources: string[]) => resourcesActions.DELETE_RESOURCES(resources);
22
23 export const loadResource = (uuid: string, showErrors?: boolean) =>
24     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25         try {
26             const kind = extractUuidKind(uuid);
27             const service = getResourceService(kind)(services);
28             if (service) {
29                 const resource = await service.get(uuid, showErrors);
30                 dispatch<any>(updateResources([resource]));
31                 return resource;
32             }
33         } catch {}
34         return undefined;
35     };