Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Dec 5, 2024
1 parent f237cbd commit 6c865b2
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = ({
Expand All @@ -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);

Expand Down Expand Up @@ -127,7 +84,7 @@ export const BulkEditInAppLayer = ({
<BulkEditInApp
fields={fields}
setFields={setFields}
options={sortedOptions}
options={options}
areAllOptionsLoaded={areAllOptionsLoaded}
/>
</BulkEditLayer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/hooks/useMarcApproach.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const useMarcApproach = () => {

const closeMarcLayer = useCallback(() => {
setIsMarcLayerOpen(false);
// setMarcFields(initialFields);
setParam('approach', null);
}, [setParam]);

Expand Down
66 changes: 66 additions & 0 deletions src/hooks/useOptionsWithTenants.js
Original file line number Diff line number Diff line change
@@ -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
};
};
13 changes: 7 additions & 6 deletions src/utils/sortAlphabetically.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down
50 changes: 25 additions & 25 deletions src/utils/sortAlphabetically.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down

0 comments on commit 6c865b2

Please sign in to comment.