1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { LinkResource } from 'models/link';
6 import { Dispatch } from 'redux';
7 import { RootState } from 'store/store';
8 import { ServiceRepository } from 'services/services';
9 import { getResourceService } from 'services/services';
10 import { Resource, extractUuidKind } from 'models/resource';
12 type NameableResource = Resource & { name?: string };
14 export const verifyAndUpdateLinkName = async (link: LinkResource, dispatch: Dispatch, getState: () => RootState, services: ServiceRepository):Promise<string> => {
15 //check if head resource is already in the store
16 let headResource: Resource | undefined = getState().resources[link.headUuid];
18 headResource = await fetchResource(link.headUuid)(dispatch, getState, services);
20 console.error('Could not verify link', link);
25 if (validateLinkNameProp(link, headResource) === true) return link.name;
27 const updatedLink = updateLinkNameProp(link, headResource);
29 return updatedLink.name;
32 const fetchResource = (uuid: string, showErrors?: boolean) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34 const kind = extractUuidKind(uuid);
35 const service = getResourceService(kind)(services);
37 const resource = await service.get(uuid, showErrors);
46 const validateLinkNameProp = (link: LinkResource, head: NameableResource) => {
47 if(!link.name || link.name !== head.name) return false;
51 const updateLinkNameProp = (link: LinkResource, head: NameableResource) => {
52 const updatedLink = {...link};
53 if(head.name) updatedLink.name = head.name;