21621: Add unit test for json copy to clipboard
[arvados.git] / services / workbench2 / src / common / link-update-name.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
10
11 type NameableResource = Resource & { name?: string };
12
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];
16     //if not, fetch it
17     if (!headResource) {
18         headResource = await fetchResource(link.headUuid)(dispatch, getState, services);
19         if (!headResource) {
20             if (!link.name) console.error('Could not validate link', link, 'because link head', link.headUuid, 'is not available');
21             return link;
22         }
23     }
24
25     if (validateLinkNameProp(link, headResource) === true) return link;
26
27     const updatedLink = updateLinkNameProp(link, headResource);
28     updateRemoteLinkName(updatedLink)(dispatch, getState, services);
29
30     return updatedLink;
31 };
32
33 export const verifyAndUpdateLinks = async (links: LinkResource[], dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34     
35     const updatedLinks = links.map((link) => verifyAndUpdateLink(link, dispatch, getState, services));
36         return Promise.all(updatedLinks);
37 };
38
39 const fetchResource = (uuid: string, showErrors?: boolean) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40     try {
41         const kind = extractUuidKind(uuid);
42         const service = getResourceService(kind)(services);
43         if (service) {
44             const resource = await service.get(uuid, showErrors);
45             return resource;
46         }
47     } catch (e) {
48         console.error(`Could not fetch resource ${uuid}`, e);
49     }
50     return undefined;
51 };
52
53 const validateLinkNameProp = (link: LinkResource, head: NameableResource) => {
54     if (!link.name || link.name !== head.name) return false;
55     return true;
56 };
57
58 const updateLinkNameProp = (link: LinkResource, head: NameableResource) => {
59     const updatedLink = { ...link };
60     if (head.name) updatedLink.name = head.name;
61     return updatedLink;
62 };
63
64 const updateRemoteLinkName = (link: LinkResource) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65     try {
66         const kind = extractUuidKind(link.uuid);
67         const service = getResourceService(kind)(services);
68         if (service) {
69             service.update(link.uuid, { name: link.name });
70         }
71     } catch (error) {
72         console.error('Could not update link name', link, error);
73     }
74 };