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 { ProjectResource } from "models/project";
8 import { GroupResource } from "models/group";
10 export type ResourcesState = { [key: string]: Resource };
12 const getResourceWritableBy = (state: ResourcesState, id: string, userUuid: string): string[] => {
17 if (id === userUuid) {
21 const resource = (state[id] as ProjectResource);
27 const { writableBy } = resource;
29 return writableBy || getResourceWritableBy(state, resource.ownerUuid, userUuid);
32 export const getResourceWithEditableStatus = <T extends EditableResource & GroupResource>(id: string, userUuid?: string) =>
33 (state: ResourcesState): T | undefined => {
34 if (state[id] === undefined) { return; }
36 const resource = JSON.parse(JSON.stringify(state[id] as T));
39 resource.isEditable = userUuid ? getResourceWritableBy(state, id, userUuid).indexOf(userUuid) > -1 : false;
45 export const getResource = <T extends Resource = Resource>(id: string) =>
46 (state: ResourcesState): T | undefined =>
49 export const setResource = <T extends Resource>(id: string, data: T) =>
50 (state: ResourcesState) => ({
55 export const deleteResource = (id: string) =>
56 (state: ResourcesState) => {
57 const newState = { ...state };
62 export const filterResources = (filter: (resource: Resource) => boolean) =>
63 (state: ResourcesState) =>
66 .reduce((resources, id) => {
67 const resource = getResource(id)(state);
69 ? [...resources, resource]
74 export const filterResourcesByKind = (kind: ResourceKind) =>
75 (state: ResourcesState) =>
76 filterResources(resource => resource.kind === kind)(state);