X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/97de0bf90660e72220f350483981e35d53a5998f..7437e0b4a85480fc6ca977488a5bb501e7fa1e3e:/src/store/resources/resources.ts diff --git a/src/store/resources/resources.ts b/src/store/resources/resources.ts index 10c82ffe..696a1362 100644 --- a/src/store/resources/resources.ts +++ b/src/store/resources/resources.ts @@ -2,11 +2,46 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { Resource } from "~/models/resource"; -import { ResourceKind } from '../../models/resource'; +import { Resource, EditableResource } from "~/models/resource"; +import { ResourceKind } from '~/models/resource'; +import { ProjectResource } from "~/models/project"; +import { GroupResource } from "~/models/group"; export type ResourcesState = { [key: string]: Resource }; +const getResourceWritableBy = (state: ResourcesState, id: string, userUuid: string): string[] => { + if (!id) { + return []; + } + + if (id === userUuid) { + return [userUuid]; + } + + const resource = (state[id] as ProjectResource); + + if (!resource) { + return []; + } + + const { writableBy } = resource; + + return writableBy || getResourceWritableBy(state, resource.ownerUuid, userUuid); +}; + +export const getResourceWithEditableStatus = (id: string, userUuid?: string) => + (state: ResourcesState): T | undefined => { + if (state[id] === undefined) { return; } + + const resource = JSON.parse(JSON.stringify(state[id] as T)); + + if (resource) { + resource.isEditable = userUuid ? getResourceWritableBy(state, id, userUuid).indexOf(userUuid) > -1 : false; + } + + return resource; + }; + export const getResource = (id: string) => (state: ResourcesState): T | undefined => state[id] as T;