Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIBULKED-497: "Are you sure" preview displays outdated values after User changed selection on bulk edit form and clicked "Confirm changes" #626

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const BulkEditInAppLayer = ({
downloadFile,
confirmChanges,
closePreviewModal,
setShouldRefetchStatus
} = useConfirmChanges({
queryDownloadKey: QUERY_KEY_DOWNLOAD_PREVIEW_MODAL,
updateFn: contentUpdate,
Expand Down Expand Up @@ -81,6 +82,7 @@ export const BulkEditInAppLayer = ({
onDownload={downloadFile}
onKeepEditing={closePreviewModal}
onChangesCommited={handleChangesCommited}
setShouldRefetchStatus={setShouldRefetchStatus}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const BulkEditPreviewModal = ({
onKeepEditing,
onDownload,
onChangesCommited,
setShouldRefetchStatus
}) => {
const callout = useShowCallout();
const intl = useIntl();
Expand Down Expand Up @@ -91,6 +92,7 @@ export const BulkEditPreviewModal = ({
bulkDetails={bulkDetails}
isPreviewEnabled={!isPreviewLoading}
onPreviewError={onKeepEditing}
setShouldRefetchStatus={setShouldRefetchStatus}
/>
</Modal>
);
Expand All @@ -103,4 +105,5 @@ BulkEditPreviewModal.propTypes = {
onKeepEditing: PropTypes.func,
onChangesCommited: PropTypes.func,
onDownload: PropTypes.func,
setShouldRefetchStatus: PropTypes.func
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import React, { useContext } from 'react';
import React, {
useContext,
useEffect,
useState
} from 'react';
import PropTypes from 'prop-types';
import {
FormattedMessage,
Expand Down Expand Up @@ -40,6 +44,7 @@ export const BulkEditPreviewModalList = ({
bulkDetails,
isPreviewEnabled,
onPreviewError,
setShouldRefetchStatus,
}) => {
const callout = useShowCallout();
const intl = useIntl();
Expand All @@ -52,6 +57,15 @@ export const BulkEditPreviewModalList = ({
} = usePagination(PAGINATION_CONFIG);

const visibleColumnKeys = getVisibleColumnsKeys(visibleColumns);
const [shouldFetch, setShouldFetch] = useState(false);

useEffect(() => {
if (isPreviewEnabled && ![JOB_STATUSES.DATA_MODIFICATION_IN_PROGRESS, JOB_STATUSES.DATA_MODIFICATION].includes(bulkDetails?.status)) {
setShouldFetch(true);
} else {
setShouldFetch(false);
}
}, [bulkDetails?.status, isPreviewEnabled]);

const {
contentData,
Expand All @@ -62,8 +76,9 @@ export const BulkEditPreviewModalList = ({
id: bulkDetails?.id,
step: EDITING_STEPS.EDIT,
capabilities: currentRecordType,
onSuccess: () => setShouldRefetchStatus(false),
queryOptions: {
enabled: isPreviewEnabled && bulkDetails?.status !== JOB_STATUSES.DATA_MODIFICATION_IN_PROGRESS,
enabled: shouldFetch,
onSuccess: showErrorMessage,
onError: () => {
callout({
Expand Down Expand Up @@ -137,4 +152,5 @@ BulkEditPreviewModalList.propTypes = {
bulkDetails: PropTypes.object,
isPreviewEnabled: PropTypes.bool,
onPreviewError: PropTypes.func,
setShouldRefetchStatus: PropTypes.func,
};
1 change: 1 addition & 0 deletions src/components/shared/ProgressBar/ProgressBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ProgressBar = () => {
const { bulkDetails, clearIntervalAndRedirect } = useBulkOperationDetails({
id,
interval: 1000 * 3,
shouldRefetch: true,
additionalQueryKeys: [step],
});

Expand Down
70 changes: 38 additions & 32 deletions src/hooks/api/useBulkOperationDetails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
Expand All @@ -12,56 +12,62 @@
id,
interval = 0,
additionalQueryKeys = [],
shouldRefetch = false,
...options
}) => {
const ky = useOkapiKy();
const [namespaceKey] = useNamespace({ key: BULK_OPERATION_DETAILS_KEY });
const history = useHistory();
const [refetchInterval, setRefetchInterval] = useState(interval);
const [refetchInterval, setRefetchInterval] = useState(0);
const { showErrorMessage } = useErrorMessages();

const clearInterval = () => {
setRefetchInterval(0);
};
const stopRefetching = () => setRefetchInterval(0);

const clearIntervalAndRedirect = (pathname, searchParams) => {
clearInterval();
const fetchBulkOperationDetails = async () => {
try {
const response = await ky.get(`bulk-operations/${id}`).json();
showErrorMessage(response);

history.replace({
pathname,
search: searchParams ? buildSearch(searchParams, history.location.search) : '',
});
if (response.status === JOB_STATUSES.FAILED || response?.errorMessage) {
stopRefetching();
}

return response;
} catch (error) {
showErrorMessage(error);
stopRefetching();

return error;
}
};

const { data, isLoading } = useQuery({
queryKey: [BULK_OPERATION_DETAILS_KEY, namespaceKey, id, refetchInterval, ...additionalQueryKeys],
const { data, isLoading, refetch } = useQuery({
queryKey: [BULK_OPERATION_DETAILS_KEY, namespaceKey, refetchInterval, id, ...additionalQueryKeys],
enabled: !!id,
refetchInterval,
queryFn: async () => {
try {
const response = await ky.get(`bulk-operations/${id}`).json();
queryFn: fetchBulkOperationDetails,
...options,
});

showErrorMessage(response);
useEffect(() => {
if (!shouldRefetch || interval <= 0) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can do this and in come cases return callback for clean up... you even have a linter error


if (response.status === JOB_STATUSES.FAILED || response?.errorMessage) {
clearInterval();
}
const intervalId = setInterval(refetch, interval);

return response;
} catch (e) {
showErrorMessage(e);
clearInterval();
return () => clearInterval(intervalId);

Check failure on line 56 in src/hooks/api/useBulkOperationDetails.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Arrow function expected no return value

Check failure on line 56 in src/hooks/api/useBulkOperationDetails.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Arrow function expected no return value
}, [shouldRefetch, interval, refetch]);

return e;
}
},
...options,
});
const redirectAndStopRefetching = (pathname, searchParams) => {
stopRefetching();
history.replace({
pathname,
search: searchParams ? buildSearch(searchParams, history.location.search) : '',
});
};

return {
bulkDetails: data,
isLoading,
clearInterval,
clearIntervalAndRedirect,
refetch,
clearIntervalAndRedirect: redirectAndStopRefetching,
};
};
8 changes: 3 additions & 5 deletions src/hooks/api/useBulkOperationDetails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ describe('useBulkOperationDetails', () => {

it('should initialize with the correct refetch interval and query key', () => {
const id = 'test-id';
const refetchInterval = 5000;
const interval = 5000;

const { result } = renderHook(() => useBulkOperationDetails({ id, interval: refetchInterval }));
renderHook(() => useBulkOperationDetails({ id, interval }));

expect(result.current.isLoading).toBe(false);
expect(useQuery).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [BULK_OPERATION_DETAILS_KEY, 'namespace-key', id, refetchInterval],
refetchInterval,
queryKey: [BULK_OPERATION_DETAILS_KEY, 'namespace-key', 0, id],
enabled: true,
})
);
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/api/useRecordsPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useIntl } from 'react-intl';

import { useNamespace, useOkapiKy } from '@folio/stripes/core';

import noop from 'lodash/noop';
import { BULK_VISIBLE_COLUMNS } from '../../constants';
import { getMappedTableData } from '../../utils/mappers';
import { RootContext } from '../../context/RootContext';
Expand All @@ -21,7 +22,8 @@ export const useRecordsPreview = ({
limit,
offset,
criteria,
queryRecordType
queryRecordType,
onSuccess = noop,
}) => {
const intl = useIntl();
const { setVisibleColumns } = useContext(RootContext);
Expand Down Expand Up @@ -83,6 +85,12 @@ export const useRecordsPreview = ({
capabilities
]);

useEffect(() => {
if (contentData?.length > 0) {
onSuccess();
}
}, [contentData, onSuccess]);

return {
isLoading,
refetch,
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/useConfirmChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useQueryClient } from 'react-query';
import { useShowCallout } from '@folio/stripes-acq-components';

import {
PREVIEW_MODAL_KEY,
BULK_OPERATION_DETAILS_KEY,
useBulkOperationDetails,
useBulkOperationStart,
Expand All @@ -32,8 +31,13 @@ export const useConfirmChanges = ({

const [isPreviewModalOpened, setIsPreviewModalOpened] = useState(false);
const [isPreviewLoading, setIsPreviewLoading] = useState(false);
const [shouldRefetchStatus, setShouldRefetchStatus] = useState(false);

const { bulkDetails } = useBulkOperationDetails({ id: bulkOperationId, interval: 1000 * 3 });
const { bulkDetails } = useBulkOperationDetails({
id: bulkOperationId,
interval: 1000 * 3,
shouldRefetch: shouldRefetchStatus,
});
const { bulkOperationStart } = useBulkOperationStart();

const totalRecords = bulkDetails?.totalNumOfRecords;
Expand All @@ -44,11 +48,13 @@ export const useConfirmChanges = ({

const closePreviewModal = () => {
setIsPreviewModalOpened(false);
setShouldRefetchStatus(false);
};

const confirmChanges = (payload) => {
setIsPreviewLoading(true);
setIsPreviewModalOpened(true);
setShouldRefetchStatus(true);

updateFn(payload)
.then(() => bulkOperationStart({
Expand All @@ -58,7 +64,6 @@ export const useConfirmChanges = ({
}))
.then(() => {
queryClient.invalidateQueries(BULK_OPERATION_DETAILS_KEY);
queryClient.invalidateQueries(PREVIEW_MODAL_KEY);
})
.catch(() => {
callout({
Expand Down Expand Up @@ -94,5 +99,6 @@ export const useConfirmChanges = ({
openPreviewModal,
closePreviewModal,
confirmChanges,
setShouldRefetchStatus
};
};
Loading