Merge branch '22433-rails-postinst-exit-code'
[arvados.git] / services / workbench2 / 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         const items: Resource[] = [];
30         for (const id in state) {
31             const resource = state[id];
32             if (resource && filter(resource)) {
33                 items.push(resource);
34             }
35         }
36         return items;
37     };
38
39 export const filterResourcesByKind = (kind: ResourceKind) =>
40     (state: ResourcesState) =>
41         filterResources(resource => resource.kind === kind)(state);