1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Resource, EditableResource } from "models/resource";
6 import { ResourceKind } from 'models/resource';
7 import { GroupResource } from "models/group";
9 export type ResourcesState = { [key: string]: Resource };
11 export const getResourceWithEditableStatus = <T extends GroupResource & EditableResource>(id: string, userUuid?: string) =>
12 (state: ResourcesState): T | undefined => {
13 if (state[id] === undefined) { return; }
15 const resource = JSON.parse(JSON.stringify(state[id])) as T;
18 if (resource.canWrite === undefined) {
19 resource.isEditable = (state[resource.ownerUuid] as GroupResource)?.canWrite;
21 resource.isEditable = resource.canWrite;
28 export const getResource = <T extends Resource = Resource>(id: string) =>
29 (state: ResourcesState): T | undefined =>
32 export const setResource = <T extends Resource>(id: string, data: T) =>
33 (state: ResourcesState) => ({
38 export const deleteResource = (id: string) =>
39 (state: ResourcesState) => {
40 const newState = { ...state };
45 export const filterResources = (filter: (resource: Resource) => boolean) =>
46 (state: ResourcesState) =>
49 .reduce((resources, id) => {
50 const resource = getResource(id)(state);
52 ? [...resources, resource]
57 export const filterResourcesByKind = (kind: ResourceKind) =>
58 (state: ResourcesState) =>
59 filterResources(resource => resource.kind === kind)(state);