From 66b876550ce298b35cfb75b0da5d6756203e61ba Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:40:26 +0100 Subject: [PATCH 1/3] IBX-4925 udw fetched version if fast clicked button --- .../universal.discovery.module.js | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js index d9a12ade81..2a3241eaaa 100644 --- a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js +++ b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState, createContext, useRef } from 'react'; +import React, { useEffect, useCallback, useState, createContext, useRef } from 'react'; import PropTypes from 'prop-types'; import Icon from '../common/icon/icon'; @@ -156,18 +156,6 @@ const UniversalDiscoveryModule = (props) => { 'm-ud': true, 'm-ud--locations-selected': !!selectedLocations.length && props.allowConfirmation, }); - const onConfirm = (selectedItems = selectedLocations) => { - const updatedLocations = selectedItems.map((selectedItem) => { - const clonedLocation = deepClone(selectedItem.location); - const contentType = clonedLocation.ContentInfo.Content.ContentType; - - clonedLocation.ContentInfo.Content.ContentTypeInfo = contentTypesInfoMap[contentType._href]; - - return clonedLocation; - }); - - props.onConfirm(updatedLocations); - }; const loadPermissions = () => { const locationIds = selectedLocations .filter((item) => !item.permissions) @@ -182,7 +170,7 @@ const UniversalDiscoveryModule = (props) => { loadLocationsWithPermissions({ locationIds, signal: abortControllerRef.current.signal }, (response) => resolve(response)); }); }; - const loadVersions = () => { + const loadVersions = (hasSignal = true) => { const locationsWithoutVersion = selectedLocations.filter( (selectedItem) => !selectedItem.location.ContentInfo.Content.CurrentVersion.Version, ); @@ -194,9 +182,42 @@ const UniversalDiscoveryModule = (props) => { const contentId = locationsWithoutVersion.map((item) => item.location.ContentInfo.Content._id).join(','); return new Promise((resolve) => { - loadContentInfo({ ...restInfo, contentId, signal: abortControllerRef.current.signal }, (response) => resolve(response)); + loadContentInfo({ ...restInfo, contentId, signal: hasSignal ? abortControllerRef.current.signal : null }, (response) => + resolve(response), + ); }); }; + const onConfirm = useCallback( + (selectedItems = selectedLocations) => { + loadVersions(false).then((locationsWithVersions) => { + const clonedSelectedLocation = deepClone(selectedItems); + + if (Array.isArray(locationsWithVersions)) { + locationsWithVersions.forEach((content) => { + const clonedLocation = clonedSelectedLocation.find( + (clonedItem) => clonedItem.location.ContentInfo.Content._id === content._id, + ); + + if (clonedLocation) { + clonedLocation.location.ContentInfo.Content.CurrentVersion.Version = content.CurrentVersion.Version; + } + }); + } + + const updatedLocations = clonedSelectedLocation.map((selectedItem) => { + const clonedLocation = deepClone(selectedItem.location); + const contentType = clonedLocation.ContentInfo.Content.ContentType; + + clonedLocation.ContentInfo.Content.ContentTypeInfo = contentTypesInfoMap[contentType._href]; + + return clonedLocation; + }); + + props.onConfirm(updatedLocations); + }); + }, + [selectedLocations, contentTypesInfoMap], + ); useEffect(() => { const addContentTypesInfo = (contentTypes) => { From c5b3d38dc7eaef455c2d83cd417cb2c940146c41 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Tue, 31 Jan 2023 14:37:41 +0100 Subject: [PATCH 2/3] returned useEffect abort, passed object --- .../universal.discovery.module.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js index 2a3241eaaa..4c82da8143 100644 --- a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js +++ b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js @@ -170,7 +170,7 @@ const UniversalDiscoveryModule = (props) => { loadLocationsWithPermissions({ locationIds, signal: abortControllerRef.current.signal }, (response) => resolve(response)); }); }; - const loadVersions = (hasSignal = true) => { + const loadVersions = (signal = null) => { const locationsWithoutVersion = selectedLocations.filter( (selectedItem) => !selectedItem.location.ContentInfo.Content.CurrentVersion.Version, ); @@ -182,14 +182,12 @@ const UniversalDiscoveryModule = (props) => { const contentId = locationsWithoutVersion.map((item) => item.location.ContentInfo.Content._id).join(','); return new Promise((resolve) => { - loadContentInfo({ ...restInfo, contentId, signal: hasSignal ? abortControllerRef.current.signal : null }, (response) => - resolve(response), - ); + loadContentInfo({ ...restInfo, contentId, signal }, (response) => resolve(response)); }); }; const onConfirm = useCallback( (selectedItems = selectedLocations) => { - loadVersions(false).then((locationsWithVersions) => { + loadVersions().then((locationsWithVersions) => { const clonedSelectedLocation = deepClone(selectedItems); if (Array.isArray(locationsWithVersions)) { @@ -268,7 +266,7 @@ const UniversalDiscoveryModule = (props) => { abortControllerRef.current = new AbortController(); - Promise.all([loadPermissions(), loadVersions()]).then((response) => { + Promise.all([loadPermissions(), loadVersions(abortControllerRef.current.signal)]).then((response) => { const [locationsWithPermissions, locationsWithVersions] = response; if (!locationsWithPermissions.length && !locationsWithVersions.length) { @@ -302,6 +300,9 @@ const UniversalDiscoveryModule = (props) => { locations: clonedSelectedLocation, }); }); + return () => { + abortControllerRef.current?.abort(); + }; }, [selectedLocations]); useEffect(() => { From 23361afe9280a3b54f72da1b28aa96e9b39f0ca8 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Tue, 31 Jan 2023 14:39:59 +0100 Subject: [PATCH 3/3] prettier --- .../modules/universal-discovery/universal.discovery.module.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js index 4c82da8143..69c3c26ea4 100644 --- a/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js +++ b/src/bundle/ui-dev/src/modules/universal-discovery/universal.discovery.module.js @@ -300,6 +300,7 @@ const UniversalDiscoveryModule = (props) => { locations: clonedSelectedLocation, }); }); + return () => { abortControllerRef.current?.abort(); };