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

fix: filtering error on first render for create display #4200

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
8 changes: 5 additions & 3 deletions src/web/hooks/__tests__/usePageFilter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ describe('usePageFilter tests', () => {
}),
);

expect(result.current[0]).toEqual(
Filter.fromString('location=query rows=42'),
);
const expectedFilter = Filter.fromString('location=query rows=42');

await waitFor(() => {
expect(result.current[0].toString()).toEqual(expectedFilter.toString());
});
});
test('should prefer pageFilter over defaultSettingFilter', async () => {
const pFilter = Filter.fromString('page=filter');
Expand Down
39 changes: 28 additions & 11 deletions src/web/hooks/usePageFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {useCallback, useEffect, useState} from 'react';
import {useCallback, useEffect, useState, useRef} from 'react';

import {useSearchParams} from 'react-router-dom';

Expand Down Expand Up @@ -79,7 +79,9 @@ const usePageFilter = (
);

// use null as value for not set at all
let returnedFilter;
const returnedFilterRef = useRef(null);

let returnedFilter = returnedFilterRef.current;
// only use searchParams directly if locationQueryFilterString is undefined
const locationQueryFilterString =
initialLocationQueryFilterString || searchParams.get('filter');
Expand All @@ -90,6 +92,8 @@ const usePageFilter = (
: undefined,
);

const [initialFilterSet, setInitialFilterSet] = useState(false);

useEffect(() => {
if (
!isDefined(defaultSettingFilter) &&
Expand Down Expand Up @@ -120,30 +124,43 @@ const usePageFilter = (
setLocationQueryFilter(undefined);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (!initialFilterSet && isDefined(rowsPerPage)) {
setInitialFilterSet(true);
if (!returnedFilterRef.current.has('rows')) {
returnedFilterRef.current = returnedFilterRef.current.set(
'rows',
rowsPerPage,
);
}
}
}, [initialFilterSet, rowsPerPage]);

if (hasValue(locationQueryFilter)) {
returnedFilter = locationQueryFilter;
if (!locationQueryFilter.has('rows') && isDefined(rowsPerPage)) {
locationQueryFilter.set('rows', rowsPerPage);
}
returnedFilterRef.current = locationQueryFilter;
} else if (isDefined(pageFilter)) {
returnedFilter = pageFilter;
returnedFilterRef.current = pageFilter;
} else if (
isDefined(defaultSettingFilter) &&
!isDefined(defaultSettingsFilterError) &&
defaultSettingFilter !== null
) {
returnedFilter = defaultSettingFilter;
returnedFilterRef.current = defaultSettingFilter;
} else if (isDefined(fallbackFilter)) {
returnedFilter = fallbackFilter;
returnedFilterRef.current = fallbackFilter;
} else {
returnedFilter = DEFAULT_FALLBACK_FILTER;
returnedFilterRef.current = DEFAULT_FALLBACK_FILTER;
}

returnedFilter = returnedFilterRef.current;

if (!isDefined(rowsPerPage) && isDefined(rowsPerPageError)) {
rowsPerPage = DEFAULT_ROWS_PER_PAGE;
}

if (!returnedFilter.has('rows') && isDefined(rowsPerPage)) {
returnedFilter = returnedFilter.set('rows', rowsPerPage);
}

const finishedLoading =
isDefined(returnedFilter) &&
(isDefined(defaultSettingFilter) ||
Expand Down