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-553 Replace generic error with the detailed error message - include bulk-operation #625

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { useSearchParams } from '../../../../hooks';
import { RootContext } from '../../../../context/RootContext';
import { getVisibleColumnsKeys } from '../../../../utils/helpers';
import { useErrorMessages } from '../../../../hooks/useErrorMessages';


export const BulkEditPreviewModalList = ({
Expand All @@ -44,6 +45,7 @@ export const BulkEditPreviewModalList = ({
const intl = useIntl();
const { visibleColumns } = useContext(RootContext);
const { currentRecordType } = useSearchParams();
const { showErrorMessage } = useErrorMessages();
const {
pagination,
changePage,
Expand All @@ -62,6 +64,7 @@ export const BulkEditPreviewModalList = ({
capabilities: currentRecordType,
queryOptions: {
enabled: isPreviewEnabled && bulkDetails?.status !== JOB_STATUSES.DATA_MODIFICATION_IN_PROGRESS,
onSuccess: showErrorMessage,
onError: () => {
callout({
type: 'error',
Expand Down
37 changes: 29 additions & 8 deletions src/hooks/api/useBulkOperationDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
import { buildSearch } from '@folio/stripes-acq-components';
import { JOB_STATUSES } from '../../constants';
import { useErrorMessages } from '../useErrorMessages';

export const BULK_OPERATION_DETAILS_KEY = 'BULK_OPERATION_DETAILS_KEY';

Expand All @@ -16,18 +18,12 @@ export const useBulkOperationDetails = ({
const [namespaceKey] = useNamespace({ key: BULK_OPERATION_DETAILS_KEY });
const history = useHistory();
const [refetchInterval, setRefetchInterval] = useState(interval);

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

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

const clearIntervalAndRedirect = (pathname, searchParams) => {
clearInterval();

Expand All @@ -37,6 +33,31 @@ export const useBulkOperationDetails = ({
});
};

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

showErrorMessage(response);

if (response.status === JOB_STATUSES.FAILED || response?.errorMessage) {
clearInterval();
}

return response;
} catch (e) {
showErrorMessage(e);
clearInterval();

return e;
}
},
...options,
});

return {
bulkDetails: data,
isLoading,
Expand Down
82 changes: 82 additions & 0 deletions src/hooks/api/useBulkOperationDetails.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
import { useBulkOperationDetails, BULK_OPERATION_DETAILS_KEY } from './useBulkOperationDetails';
import { useErrorMessages } from '../useErrorMessages';


jest.mock('@folio/stripes-acq-components', () => ({
buildSearch: jest.fn(),
}));

jest.mock('react-query', () => ({
useQuery: jest.fn(),
}));

jest.mock('@folio/stripes/core', () => ({
useNamespace: jest.fn(),
useOkapiKy: jest.fn(),
}));

jest.mock('react-router-dom', () => ({
useHistory: jest.fn(),
}));

jest.mock('../useErrorMessages', () => ({
useErrorMessages: jest.fn(),
}));

describe('useBulkOperationDetails', () => {
let kyMock;
let historyMock;
let showErrorMessageMock;

beforeEach(() => {
kyMock = { get: jest.fn() };
historyMock = { replace: jest.fn(), location: { search: '' } };
showErrorMessageMock = jest.fn();

useOkapiKy.mockReturnValue(kyMock);
useNamespace.mockReturnValue(['namespace-key']);
useHistory.mockReturnValue(historyMock);
useErrorMessages.mockReturnValue({ showErrorMessage: showErrorMessageMock });

// Set a default implementation for useQuery to prevent infinite renders
useQuery.mockReturnValue({ data: null, isLoading: false });
jest.clearAllMocks();
});

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

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

expect(result.current.isLoading).toBe(false);
expect(useQuery).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [BULK_OPERATION_DETAILS_KEY, 'namespace-key', id, refetchInterval],
refetchInterval,
enabled: true,
})
);
});

it('should clear interval and redirect when clearIntervalAndRedirect is called', () => {
const id = 'test-id';
const pathname = '/new-path';
const searchParams = { query: 'test' };

const { result } = renderHook(() => useBulkOperationDetails({ id }));

act(() => {
result.current.clearIntervalAndRedirect(pathname, searchParams);
});

expect(historyMock.replace).toHaveBeenCalledWith({
pathname,
search: undefined,
});
});
});
4 changes: 1 addition & 3 deletions src/hooks/useErrorMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export const useErrorMessages = () => {
};

const showErrorMessage = (res) => {
const messageInBody = res?.errorMessage;
const messageWhenError = res?.message;
const message = messageInBody || messageWhenError;
const message = res?.errorMessage;

// check if error message should be translated (if it's exist in translations)
const prefixedMessageId = `ui-bulk-edit.${message}`;
Expand Down
12 changes: 0 additions & 12 deletions src/hooks/useErrorMessages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ describe('useErrorMessages', () => {
expect(formatMessageMock).not.toHaveBeenCalled();
});

it('should show the message from the response when no error message in body is available', () => {
const { result } = renderHook(() => useErrorMessages());
const { showErrorMessage } = result.current;

showErrorMessage({ message: 'Another error occurred' });

expect(showCalloutMock).toHaveBeenCalledWith({
type: 'error',
message: 'Another error occurred',
});
});

it('should show the default error message when the response is an error object without a message', () => {
const { result } = renderHook(() => useErrorMessages());
const { showErrorMessage } = result.current;
Expand Down
Loading