Skip to content

Commit

Permalink
refactor: upgrade mutation to query for better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Nov 2, 2023
1 parent e54369b commit a4b13ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
11 changes: 4 additions & 7 deletions lib/DonorsList/DonorsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import DonorsList from './DonorsList';
import { useFetchDonors } from './hooks';

function DonorsContainer({ name, donorOrganizationIds }) {
const [donors, setDonors] = useState([]);
const { fetchDonorsMutation, isLoading } = useFetchDonors();
const [donorIds, setDonorIds] = useState(() => donorOrganizationIds);
const { donors, isLoading } = useFetchDonors(donorIds);

const handleFetchDonors = useCallback(ids => {
fetchDonorsMutation({ donorOrganizationIds: ids })
.then((data) => {
setDonors(data);
});
}, [fetchDonorsMutation]);
setDonorIds(ids);
}, []);

useEffect(() => {
if (donorOrganizationIds.length) {
Expand Down
19 changes: 13 additions & 6 deletions lib/DonorsList/hooks/useFetchDonors/useFetchDonors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useMutation } from 'react-query';
import { useQuery } from 'react-query';

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

import { VENDORS_API } from '../../../constants';
import { batchRequest } from '../../../utils';
Expand All @@ -13,11 +16,13 @@ const buildQueryByIds = (itemsChunk) => {
return query || '';
};

export const useFetchDonors = () => {
export const useFetchDonors = (donorOrganizationIds = []) => {
const ky = useOkapiKy();
const namespace = useNamespace({ key: 'fetch-donors-list' });

const { isLoading, mutateAsync } = useMutation(
({ donorOrganizationIds }) => {
const { isLoading, data, refetch } = useQuery(
[namespace, donorOrganizationIds],
() => {
return batchRequest(
({ params: searchParams }) => ky
.get(VENDORS_API, { searchParams })
Expand All @@ -27,10 +32,12 @@ export const useFetchDonors = () => {
buildQueryByIds,
);
},
{ enabled: Boolean(donorOrganizationIds.length) },
);

return ({
fetchDonorsMutation: mutateAsync,
donors: data || [],
isLoading,
refetch,
});
};
12 changes: 10 additions & 2 deletions lib/DonorsList/hooks/useFetchDonors/useFetchDonors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useFetchDonors } from './useFetchDonors';
jest.mock('@folio/stripes/core', () => ({
...jest.requireActual('@folio/stripes/core'),
useOkapiKy: jest.fn(),
useNamespace: jest.fn(() => ['NameSpace']),
}));

const queryClient = new QueryClient();
Expand Down Expand Up @@ -41,12 +42,19 @@ describe('useDonors', () => {
it('should make a get a request with default search params', async () => {
const { result, waitFor } = renderHook(() => useFetchDonors(), { wrapper });

await result.current.fetchDonorsMutation({ donorOrganizationIds: ['orgId'] });
await waitFor(() => !result.current.isLoading);

expect(getMock).not.toHaveBeenCalled();
});

it('should make a get a request with default search params', async () => {
const { result, waitFor } = renderHook(() => useFetchDonors([org.id]), { wrapper });

await waitFor(() => !result.current.isLoading);

expect(getMock).toHaveBeenCalledWith(
'organizations/organizations',
{ 'searchParams': { 'limit': 1000, 'query': 'id==orgId' } },
{ 'searchParams': { 'limit': 1000, 'query': `id==${org.id}` } },
);
});
});
3 changes: 2 additions & 1 deletion translations/stripes-acq-components/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,6 @@
"donors.column.name": "Name",
"donors.modal.title": "Add donors",
"donors.modal.resultsTitle": "Donors",
"donors.noFindOrganizationPlugin": "no find-organization plugin"
"donors.noFindOrganizationPlugin": "no find-organization plugin",
"donors.button.unassign": "Unassign"
}

0 comments on commit a4b13ff

Please sign in to comment.