diff --git a/src/components/BulkEditPane/BulkEditInAppLayer/BulkEditInAppLayer.js b/src/components/BulkEditPane/BulkEditInAppLayer/BulkEditInAppLayer.js index 981783a7..cf37c46c 100644 --- a/src/components/BulkEditPane/BulkEditInAppLayer/BulkEditInAppLayer.js +++ b/src/components/BulkEditPane/BulkEditInAppLayer/BulkEditInAppLayer.js @@ -1,8 +1,5 @@ import React, { useState } from 'react'; import PropTypes from 'prop-types'; -import { useIntl } from 'react-intl'; - -import { checkIfUserInCentralTenant, useStripes } from '@folio/stripes/core'; import { BulkEditLayer } from '../BulkEditListResult/BulkEditInAppLayer/BulkEditLayer'; import { BulkEditInApp } from '../BulkEditListResult/BulkEditInApp/BulkEditInApp'; @@ -14,26 +11,11 @@ import { } from '../BulkEditListResult/BulkEditInApp/ContentUpdatesForm/helpers'; import { QUERY_KEY_DOWNLOAD_PREVIEW_MODAL, - useBulkOperationTenants, useContentUpdate, - useHoldingsNotes, - useHoldingsNotesEcs, - useInstanceNotes, - useItemNotes, - useItemNotesEcs } from '../../../hooks/api'; import { useConfirmChanges } from '../../../hooks/useConfirmChanges'; import { savePreviewFile } from '../../../utils/files'; -import { useSearchParams } from '../../../hooks'; -import { - CAPABILITIES, - getHoldingsOptions, - getInstanceOptions, - getItemsOptions, - getUserOptions -} from '../../../constants'; -import { removeDuplicatesByValue } from '../../../utils/helpers'; -import { sortAlphabetically } from '../../../utils/sortAlphabetically'; +import { useOptionsWithTenants } from '../../../hooks/useOptionsWithTenants'; export const BulkEditInAppLayer = ({ @@ -42,36 +24,11 @@ export const BulkEditInAppLayer = ({ paneProps, onInAppLayerClose, }) => { - const stripes = useStripes(); - const isCentralTenant = checkIfUserInCentralTenant(stripes); - - const { formatMessage } = useIntl(); - const { currentRecordType } = useSearchParams(); - - const isItemRecordType = currentRecordType === CAPABILITIES.ITEM; - const isHoldingsRecordType = currentRecordType === CAPABILITIES.HOLDING; - const isInstanceRecordType = currentRecordType === CAPABILITIES.INSTANCE; - - const { data: tenants, isLoading } = useBulkOperationTenants(bulkOperationId); - const { itemNotes, isItemNotesLoading } = useItemNotes({ enabled: isItemRecordType }); - const { holdingsNotes, isHoldingsNotesLoading } = useHoldingsNotes({ enabled: isHoldingsRecordType }); - const { instanceNotes, isInstanceNotesLoading } = useInstanceNotes({ enabled: isInstanceRecordType }); - const { notesEcs: itemNotesEcs, isFetching: isItemsNotesEcsLoading } = useItemNotesEcs(tenants, 'option', { enabled: isItemRecordType && isCentralTenant && !isLoading }); - const { notesEcs: holdingsNotesEcs, isFetching: isHoldingsNotesEcsLoading } = useHoldingsNotesEcs(tenants, 'option', { enabled: isHoldingsRecordType && isCentralTenant && !isLoading }); - - const options = ({ - [CAPABILITIES.ITEM]: getItemsOptions(formatMessage, removeDuplicatesByValue(isCentralTenant ? itemNotesEcs : itemNotes, tenants)), - [CAPABILITIES.USER]: getUserOptions(formatMessage), - [CAPABILITIES.HOLDING]: getHoldingsOptions(formatMessage, isCentralTenant ? removeDuplicatesByValue(holdingsNotesEcs, tenants) : holdingsNotes), - [CAPABILITIES.INSTANCE]: getInstanceOptions(formatMessage, instanceNotes), - })[currentRecordType]; - - const areAllOptionsLoaded = options && !isItemNotesLoading && !isInstanceNotesLoading && !isItemsNotesEcsLoading && !isHoldingsNotesLoading && !isHoldingsNotesEcsLoading; - const sortedOptions = sortAlphabetically(options, formatMessage({ id:'ui-bulk-edit.options.placeholder' })); + const [fields, setFields] = useState([]); const { contentUpdate } = useContentUpdate({ id: bulkOperationId }); + const { options, areAllOptionsLoaded } = useOptionsWithTenants(bulkOperationId); - const [fields, setFields] = useState([]); const contentUpdates = getMappedContentUpdates(fields, options); const isInAppFormValid = isContentUpdatesFormValid(contentUpdates); @@ -127,7 +84,7 @@ export const BulkEditInAppLayer = ({ diff --git a/src/components/BulkEditPane/BulkEditListResult/BulkEditInApp/BulkEditInApp.js b/src/components/BulkEditPane/BulkEditListResult/BulkEditInApp/BulkEditInApp.js index 66609e7c..cc506867 100644 --- a/src/components/BulkEditPane/BulkEditListResult/BulkEditInApp/BulkEditInApp.js +++ b/src/components/BulkEditPane/BulkEditListResult/BulkEditInApp/BulkEditInApp.js @@ -11,9 +11,9 @@ import { import { BulkEditInAppTitle } from './BulkEditInAppTitle/BulkEditInAppTitle'; import { ContentUpdatesForm } from './ContentUpdatesForm/ContentUpdatesForm'; - import { RootContext } from '../../../../context/RootContext'; + export const BulkEditInApp = ({ areAllOptionsLoaded, options, fields, setFields }) => { const { title } = useContext(RootContext); diff --git a/src/hooks/useMarcApproach.js b/src/hooks/useMarcApproach.js index 0f2a9a95..34c65712 100644 --- a/src/hooks/useMarcApproach.js +++ b/src/hooks/useMarcApproach.js @@ -11,7 +11,6 @@ export const useMarcApproach = () => { const closeMarcLayer = useCallback(() => { setIsMarcLayerOpen(false); - // setMarcFields(initialFields); setParam('approach', null); }, [setParam]); diff --git a/src/hooks/useOptionsWithTenants.js b/src/hooks/useOptionsWithTenants.js new file mode 100644 index 00000000..487523ca --- /dev/null +++ b/src/hooks/useOptionsWithTenants.js @@ -0,0 +1,66 @@ +import { useIntl } from 'react-intl'; + +import { checkIfUserInCentralTenant, useStripes } from '@folio/stripes/core'; + +import { useSearchParams } from './useSearchParams'; +import { + CAPABILITIES, + getHoldingsOptions, + getInstanceOptions, + getItemsOptions, + getUserOptions +} from '../constants'; +import { + useBulkOperationTenants, + useHoldingsNotes, + useHoldingsNotesEcs, + useInstanceNotes, + useItemNotes, + useItemNotesEcs +} from './api'; +import { removeDuplicatesByValue } from '../utils/helpers'; +import { sortAlphabetically } from '../utils/sortAlphabetically'; + + +export const useOptionsWithTenants = (bulkOperationId) => { + const stripes = useStripes(); + const { formatMessage } = useIntl(); + const { currentRecordType } = useSearchParams(); + + const isItemRecordType = currentRecordType === CAPABILITIES.ITEM; + const isHoldingsRecordType = currentRecordType === CAPABILITIES.HOLDING; + const isInstanceRecordType = currentRecordType === CAPABILITIES.INSTANCE; + + const { data: tenants, isLoading } = useBulkOperationTenants(bulkOperationId); + + const isCentralTenant = checkIfUserInCentralTenant(stripes); + + const { itemNotes, isItemNotesLoading } = useItemNotes({ enabled: isItemRecordType }); + const { notesEcs: itemNotesEcs, isFetching: isItemsNotesEcsLoading } = useItemNotesEcs(tenants, 'option', { enabled: isItemRecordType && isCentralTenant && !isLoading }); + + const { holdingsNotes, isHoldingsNotesLoading } = useHoldingsNotes({ enabled: isHoldingsRecordType }); + const { notesEcs: holdingsNotesEcs, isFetching: isHoldingsNotesEcsLoading } = useHoldingsNotesEcs(tenants, 'option', { enabled: isHoldingsRecordType && isCentralTenant && !isLoading }); + + const { instanceNotes, isInstanceNotesLoading } = useInstanceNotes({ enabled: isInstanceRecordType }); + + const itemsByTenant = isCentralTenant ? itemNotesEcs : itemNotes; + const itemsWithoutDuplicates = removeDuplicatesByValue(itemsByTenant, tenants); + + const holdingsByTenant = isCentralTenant ? holdingsNotesEcs : holdingsNotes; + const holdingsWithoutDuplicates = removeDuplicatesByValue(holdingsByTenant, tenants); + + const optionsByType = ({ + [CAPABILITIES.USER]: getUserOptions(formatMessage), + [CAPABILITIES.ITEM]: getItemsOptions(formatMessage, itemsWithoutDuplicates), + [CAPABILITIES.HOLDING]: getHoldingsOptions(formatMessage, holdingsWithoutDuplicates), + [CAPABILITIES.INSTANCE]: getInstanceOptions(formatMessage, instanceNotes), + })[currentRecordType]; + + const areAllOptionsLoaded = optionsByType && !isItemNotesLoading && !isInstanceNotesLoading && !isItemsNotesEcsLoading && !isHoldingsNotesLoading && !isHoldingsNotesEcsLoading; + const options = sortAlphabetically(optionsByType); + + return { + options, + areAllOptionsLoaded + }; +}; diff --git a/src/utils/sortAlphabetically.js b/src/utils/sortAlphabetically.js index 6f2df4fe..b404ce85 100644 --- a/src/utils/sortAlphabetically.js +++ b/src/utils/sortAlphabetically.js @@ -1,9 +1,10 @@ -export const sortAlphabetically = (array, placeholder) => array?.sort((a, b) => { +export const sortAlphabetically = (array) => array?.sort((a, b) => { const collator = new Intl.Collator(); - if (a.label === placeholder) { + // empty values are always first as they are placeholders + if (!a.value) { return -1; - } else if (b.label === placeholder) { + } else if (!b.value) { return 1; } // Compare based on category @@ -27,13 +28,13 @@ export const sortAlphabetically = (array, placeholder) => array?.sort((a, b) => return categoryComparison; }); -export const sortAlphabeticallyWithoutGroups = (array, placeholder) => { +export const sortAlphabeticallyWithoutGroups = (array) => { const collator = new Intl.Collator(); return array?.sort((a, b) => { - if (a.label === placeholder) { + if (!a.value) { return -1; - } else if (b.label === placeholder) { + } else if (!b.value) { return 1; } else { return collator.compare(a.label, b.label); diff --git a/src/utils/sortAlphabetically.test.js b/src/utils/sortAlphabetically.test.js index 1d575c3e..ef233d63 100644 --- a/src/utils/sortAlphabetically.test.js +++ b/src/utils/sortAlphabetically.test.js @@ -3,39 +3,39 @@ import { sortAlphabetically, sortAlphabeticallyWithoutGroups } from './sortAlpha describe('sortAlphabetically', () => { it('should sort the array alphabetically', () => { const inputArray = [ - { label: 'URL public note' }, - { label: 'Link text' }, - { label: 'Available' }, + { label: 'URL public note', value: 'URL public note' }, + { label: 'Link text', value: 'Link text' }, + { label: 'Available', value: 'Available' }, ]; const expectedOutput = [ - { label: 'Available' }, - { label: 'Link text' }, - { label: 'URL public note' }, + { label: 'Available', value: 'Available' }, + { label: 'Link text', value: 'Link text' }, + { label: 'URL public note', value: 'URL public note' }, ]; - const result = sortAlphabetically(inputArray, ''); + const result = sortAlphabetically(inputArray); expect(result).toEqual(expectedOutput); }); it('should handle a placeholder value and move it to the beginning', () => { const inputArray = [ - { label: 'URL public note' }, - { label: 'Link text' }, - { label: 'Available' }, - { label: 'Placeholder' }, + { label: 'URL public note', value: 'URL public note' }, + { label: 'Link text', value: 'Link text' }, + { label: 'Available', value: 'Available' }, + { label: 'Placeholder', value: '' }, ]; const expectedOutput = [ - { label: 'Placeholder' }, - { label: 'Available' }, - { label: 'Link text' }, - { label: 'URL public note' }, + { label: 'Placeholder', value: '' }, + { label: 'Available', value: 'Available' }, + { label: 'Link text', value: 'Link text' }, + { label: 'URL public note', value: 'URL public note' }, ]; - const result = sortAlphabetically(inputArray, 'Placeholder'); + const result = sortAlphabetically(inputArray); expect(result).toEqual(expectedOutput); }); @@ -44,20 +44,20 @@ describe('sortAlphabetically', () => { describe('sortAlphabeticallyActions', () => { it('should sort the array alphabetically with placeholder priority', () => { const array = [ - { label: 'Replace with' }, - { label: 'Clear field' }, - { label: 'Change note type' }, - { label: 'Placeholder' }, + { label: 'Replace with', value: 'Replace with' }, + { label: 'Clear field', value: 'Clear field' }, + { label: 'Change note type', value: 'Change note type' }, + { label: 'Placeholder', value: '' }, ]; - const sortedArray = sortAlphabeticallyWithoutGroups(array, 'Placeholder'); + const sortedArray = sortAlphabeticallyWithoutGroups(array); // Your expected sorted array based on the logic in the function const expectedSortedArray = [ - { label: 'Placeholder' }, - { label: 'Change note type' }, - { label: 'Clear field' }, - { label: 'Replace with' }, + { label: 'Placeholder', value: '' }, + { label: 'Change note type', value: 'Change note type' }, + { label: 'Clear field', value: 'Clear field' }, + { label: 'Replace with', value: 'Replace with' }, ]; expect(sortedArray).toEqual(expectedSortedArray);