From dab69896991dd6b8a9a26cac4b70f16a15167b7b Mon Sep 17 00:00:00 2001 From: vashjs Date: Mon, 28 Oct 2024 09:32:11 +0100 Subject: [PATCH 1/4] UIBULKED-553 Show error callouts only when job FAILED --- .../IdentifierTab/IdentifierTab.js | 16 ++++++---------- src/components/shared/ProgressBar/ProgressBar.js | 5 ++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js b/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js index 52f2f920..1a7bcf6f 100644 --- a/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js +++ b/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js @@ -4,7 +4,7 @@ import React, { useMemo, useState } from 'react'; -import { buildSearch, useShowCallout } from '@folio/stripes-acq-components'; +import { buildSearch } from '@folio/stripes-acq-components'; import { FormattedMessage } from 'react-intl'; import { useHistory, useLocation } from 'react-router-dom'; @@ -24,12 +24,13 @@ import { useBulkPermissions, useLocationFilters, useSearchParams } from '../../. import { useBulkOperationStart, useUpload } from '../../../../hooks/api'; import { getIsDisabledByPerm } from '../utils/getIsDisabledByPerm'; import { RootContext } from '../../../../context/RootContext'; +import { useErrorMessages } from '../../../../hooks/useErrorMessages'; export const IdentifierTab = () => { const history = useHistory(); const location = useLocation(); - const showCallout = useShowCallout(); const permissions = useBulkPermissions(); + const { showErrorMessage } = useErrorMessages(); const { isFileUploaded, @@ -146,7 +147,7 @@ export const IdentifierTab = () => { }); if (errorMessage?.includes(ERRORS.TOKEN)) throw Error(ERRORS.TOKEN); - if (status === JOB_STATUSES.FAILED) throw Error(); + if (status === JOB_STATUSES.FAILED) throw Error(errorMessage); history.replace({ pathname: `/bulk-edit/${id}/preview`, @@ -155,13 +156,8 @@ export const IdentifierTab = () => { } setIsFileUploaded(true); - } catch ({ message }) { - if (message === ERRORS.TOKEN) { - showCallout({ - message: , - type: 'error', - }); - } + } catch (error) { + showErrorMessage(error); } }; diff --git a/src/components/shared/ProgressBar/ProgressBar.js b/src/components/shared/ProgressBar/ProgressBar.js index f764ff48..fe046070 100644 --- a/src/components/shared/ProgressBar/ProgressBar.js +++ b/src/components/shared/ProgressBar/ProgressBar.js @@ -34,14 +34,13 @@ export const ProgressBar = () => { useEffect(() => { const nextStep = getBulkOperationStep(bulkDetails); - // Show error message if any - showErrorMessage(bulkDetails); - if (nextStep) { clearIntervalAndRedirect(`/bulk-edit/${id}/preview`, { step: nextStep, progress: null }); } if (status === JOB_STATUSES.FAILED) { + showErrorMessage(bulkDetails); + clearIntervalAndRedirect('/bulk-edit', ''); } }, [ From 13b44fa946f4aa44d52e75ad5d6c473811877ab4 Mon Sep 17 00:00:00 2001 From: vashjs Date: Mon, 28 Oct 2024 10:10:14 +0100 Subject: [PATCH 2/4] update dependency array --- src/components/shared/ProgressBar/ProgressBar.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/shared/ProgressBar/ProgressBar.js b/src/components/shared/ProgressBar/ProgressBar.js index fe046070..958981d3 100644 --- a/src/components/shared/ProgressBar/ProgressBar.js +++ b/src/components/shared/ProgressBar/ProgressBar.js @@ -48,6 +48,7 @@ export const ProgressBar = () => { bulkDetails, id, clearIntervalAndRedirect, + showErrorMessage, ]); return ( From 2ea93fdb16e2e557a6a13084c7b0503608924620 Mon Sep 17 00:00:00 2001 From: vashjs Date: Mon, 28 Oct 2024 11:05:59 +0100 Subject: [PATCH 3/4] update tests --- .../IdentifierTab/IdentifierTab.js | 2 +- src/hooks/useErrorMessages.js | 4 ++-- src/hooks/useErrorMessages.test.js | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js b/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js index 1a7bcf6f..d63922fc 100644 --- a/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js +++ b/src/components/BulkEditPane/BulkEditListSidebar/IdentifierTab/IdentifierTab.js @@ -157,7 +157,7 @@ export const IdentifierTab = () => { setIsFileUploaded(true); } catch (error) { - showErrorMessage(error); + showErrorMessage(error, { fileName: fileToUpload.name }); } }; diff --git a/src/hooks/useErrorMessages.js b/src/hooks/useErrorMessages.js index 9f9af353..c5c216f2 100644 --- a/src/hooks/useErrorMessages.js +++ b/src/hooks/useErrorMessages.js @@ -17,7 +17,7 @@ export const useErrorMessages = () => { }); }; - const showErrorMessage = (res) => { + const showErrorMessage = (res, meta) => { const message = res?.errorMessage; // check if error message should be translated (if it's exist in translations) @@ -31,7 +31,7 @@ export const useErrorMessages = () => { } else if (message) { // if error message contains token error, show a special message if (message?.includes(ERRORS.TOKEN)) { - showError(intl.formatMessage({ id: 'ui-bulk-edit.error.incorrectFormatted' }, { fileName: initialFileName })); + showError(intl.formatMessage({ id: 'ui-bulk-edit.error.incorrectFormatted' }, { fileName: meta?.fileName || initialFileName })); } else { showError(message); } diff --git a/src/hooks/useErrorMessages.test.js b/src/hooks/useErrorMessages.test.js index 9cd25ec4..6926c47d 100644 --- a/src/hooks/useErrorMessages.test.js +++ b/src/hooks/useErrorMessages.test.js @@ -98,4 +98,21 @@ describe('useErrorMessages', () => { expect(showCalloutMock).not.toHaveBeenCalled(); }); + + it('should use variables from meta object if provided', () => { + const { result } = renderHook(() => useErrorMessages()); + const { showErrorMessage } = result.current; + + formatMessageMock.mockImplementationOnce(({ id }, { fileName }) => { + return `${id}.${fileName}`; + }); + + showErrorMessage({ errorMessage: ERRORS.TOKEN }, { fileName: 'testFileName' }); + + expect(formatMessageMock).toHaveBeenCalledWith({ id: 'ui-bulk-edit.error.incorrectFormatted' }, { fileName: 'testFileName' }); + expect(showCalloutMock).toHaveBeenCalledWith({ + type: 'error', + message: 'ui-bulk-edit.error.incorrectFormatted.testFileName', + }); + }); }); From c416c5db6686386538d16ce3bf0491ec72caaee2 Mon Sep 17 00:00:00 2001 From: vashjs Date: Mon, 28 Oct 2024 11:24:30 +0100 Subject: [PATCH 4/4] fix test --- src/components/BulkEdit.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/BulkEdit.test.js b/src/components/BulkEdit.test.js index 56dc1073..f55e800f 100644 --- a/src/components/BulkEdit.test.js +++ b/src/components/BulkEdit.test.js @@ -15,6 +15,10 @@ import BulkEdit from './BulkEdit'; import { mockData, createDtWithFiles, createFile, flushPromises, dispatchEvt } from '../../test/jest/utils/fileUpload'; import { queryClient } from '../../test/jest/utils/queryClient'; +jest.mock('../hooks/useErrorMessages', () => ({ + useErrorMessages: jest.fn().mockReturnValue({ showErrorMessage: jest.fn() }), +})); + jest.mock('./BulkEditPane/BulkEditListResult', () => ({ BulkEditListResult: () => 'BulkEditListResult', }));