Merge branch '15669-search-params' refs #15669
[arvados.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 loadResource = (uuid: string) =>
22     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
23         try {
24             const kind = extractUuidKind(uuid);
25             const service = getResourceService(kind)(services);
26             if (service) {
27                 const resource = await service.get(uuid);
28                 dispatch<any>(updateResources([resource]));
29                 return resource;
30             }
31         } catch {}
32         return undefined;
33     };