modals-are-closed-after-using-browsers-back-or-forward-option
[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             .reduce((resources, id) => {
32                 const resource = getResource(id)(state);
33                 return resource
34                     ? [...resources, resource]
35                     : resources;
36             }, [])
37             .filter(filter);
38
39 export const filterResourcesByKind = (kind: ResourceKind) =>
40     (state: ResourcesState) =>
41         filterResources(resource => resource.kind === kind)(state);