merge master
[arvados-workbench2.git] / src / store / resources / resources.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Resource } from "~/models/resource";
6 import { ResourceKind } from '../../models/resource';
7
8 export type ResourcesState = { [key: string]: Resource };
9
10 export const getResource = <T extends Resource = Resource>(id: string) =>
11     (state: ResourcesState): T | undefined =>
12         state[id] as T;
13
14 export const setResource = <T extends Resource>(id: string, data: T) =>
15     (state: ResourcesState) => ({
16         ...state,
17         [id]: data
18     });
19
20 export const deleteResource = (id: string) =>
21     (state: ResourcesState) => {
22         const newState = {...state};
23         delete newState[id];
24         return newState;
25     };
26
27 export const filterResources = (filter: (resource: Resource) => boolean) =>
28     (state: ResourcesState) =>
29         Object
30             .keys(state)
31             .map(id => getResource(id)(state))
32             .filter(filter);
33
34 export const filterResourcesByKind = (kind: ResourceKind) =>
35     (state: ResourcesState) =>
36         filterResources(resource => resource.kind === kind)(state);
37