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 verifyAndUpdateLink = async (link: LinkResource, dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<LinkResource> => {
14 //head resource should already be in the store
15 let headResource: Resource | undefined = getState().resources[link.headUuid];
18 headResource = await fetchResource(link.headUuid)(dispatch, getState, services);
20 if (!link.name) console.error('Could not validate link', link, 'because link head', link.headUuid, 'is not available');
25 if (validateLinkNameProp(link, headResource) === true) return link;
27 const updatedLink = updateLinkNameProp(link, headResource);
28 updateRemoteLinkName(updatedLink)(dispatch, getState, services);
33 export const verifyAndUpdateLinks = async (links: LinkResource[], dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35 const updatedLinks = links.map((link) => verifyAndUpdateLink(link, dispatch, getState, services));
36 return Promise.all(updatedLinks);
39 const fetchResource = (uuid: string, showErrors?: boolean) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41 const kind = extractUuidKind(uuid);
42 const service = getResourceService(kind)(services);
44 const resource = await service.get(uuid, showErrors);
48 console.error(`Could not fetch resource ${uuid}`, e);
53 const validateLinkNameProp = (link: LinkResource, head: NameableResource) => {
54 if (!link.name || link.name !== head.name) return false;
58 const updateLinkNameProp = (link: LinkResource, head: NameableResource) => {
59 const updatedLink = { ...link };
60 if (head.name) updatedLink.name = head.name;
64 const updateRemoteLinkName = (link: LinkResource) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66 const kind = extractUuidKind(link.uuid);
67 const service = getResourceService(kind)(services);
69 service.update(link.uuid, { name: link.name });
72 console.error('Could not update link name', link, error);