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, getResourceService } from 'services/services';
9 import { Resource, extractUuidKind } from 'models/resource';
11 type NameableResource = Resource & { name?: string };
13 export const verifyAndUpdateLinkName = async (link: LinkResource, dispatch: Dispatch, getState: () => RootState, services: ServiceRepository):Promise<string> => {
14 //check if head resource is already in the store
15 let headResource: Resource | undefined = getState().resources[link.headUuid];
18 headResource = await fetchResource(link.headUuid)(dispatch, getState, services);
20 console.error('Could not validate link', link, 'because link head', link.headUuid, 'is not available');
25 if (validateLinkNameProp(link, headResource) === true) return link.name;
27 const updatedLink = updateLinkNameProp(link, headResource);
28 updateRemoteLinkName(updatedLink)(dispatch, getState, services);
30 return updatedLink.name;
33 const fetchResource = (uuid: string, showErrors?: boolean) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35 const kind = extractUuidKind(uuid);
36 const service = getResourceService(kind)(services);
38 const resource = await service.get(uuid, showErrors);
42 console.error(`Could not fetch resource ${uuid}`, e);
47 const validateLinkNameProp = (link: LinkResource, head: NameableResource) => {
48 if(!link.name || link.name !== head.name) return false;
52 const updateLinkNameProp = (link: LinkResource, head: NameableResource) => {
53 const updatedLink = {...link};
54 if(head.name) updatedLink.name = head.name;
58 const updateRemoteLinkName = (link: LinkResource) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
60 const kind = extractUuidKind(link.uuid);
61 const service = getResourceService(kind)(services);
63 service.update(link.uuid, {name: link.name});
66 console.error('Could not update link name', link, error);