Skip to content

Commit

Permalink
fix: filtering error on first render for create display
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Nov 6, 2024
1 parent bfbd85a commit e0da414
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gmp/models/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ export const VULNS_FILTER_FILTER = Filter.fromString('type=vuln');

export const DEFAULT_FALLBACK_FILTER = Filter.fromString('sort=name first=1');

export const NO_ROW_LIMIT = -1;
export const RESET_FILTER = Filter.fromString('first=1');

export const DEFAULT_ROWS_PER_PAGE = 50;
Expand Down
14 changes: 7 additions & 7 deletions src/web/entities/__tests__/filterprovider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('FilterProvider component tests', () => {
});

test('should prefer pageFilter over defaultSettingFilter', async () => {
const resultingFilter = Filter.fromString('page=filter rows=42');
const resultingFilter = Filter.fromString('page=filter');

const pFilter = Filter.fromString('page=filter');

Expand Down Expand Up @@ -99,7 +99,7 @@ describe('FilterProvider component tests', () => {
test('should allow to set a pageName for loading the pageFilter', async () => {
const gmpName = 'task';
const pageName = 'foo-bar-baz';
const resultingFilter = Filter.fromString('page=filter rows=42');
const resultingFilter = Filter.fromString('page=filter');

const pFilter = Filter.fromString('page=filter');

Expand Down Expand Up @@ -143,7 +143,7 @@ describe('FilterProvider component tests', () => {
});

test('should use defaultSettingFilter', async () => {
const resultingFilter = Filter.fromString('foo=bar rows=42');
const resultingFilter = Filter.fromString('foo=bar');

const defaultSettingFilter = Filter.fromString('foo=bar');

Expand Down Expand Up @@ -180,7 +180,7 @@ describe('FilterProvider component tests', () => {
});

test('should use fallbackFilter if defaultSettingFilter could not be loaded', async () => {
const resultingFilter = Filter.fromString('fall=back rows=42');
const resultingFilter = Filter.fromString('fall=back');

const fallbackFilter = Filter.fromString('fall=back');

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('FilterProvider component tests', () => {

test('should use fallbackFilter if given', async () => {
// if no usersettings defaultFilter exists use the given fallbackFilter
const resultingFilter = Filter.fromString('fall=back rows=42');
const resultingFilter = Filter.fromString('fall=back');

const fallbackFilter = Filter.fromString('fall=back');

Expand Down Expand Up @@ -260,7 +260,7 @@ describe('FilterProvider component tests', () => {
});

test('should use default fallbackFilter as last resort', async () => {
const resultingFilter = DEFAULT_FALLBACK_FILTER.copy().set('rows', 42);
const resultingFilter = DEFAULT_FALLBACK_FILTER.copy();

const getSetting = testing.fn().mockResolvedValue({});
const subscribe = testing.fn();
Expand Down Expand Up @@ -332,7 +332,7 @@ describe('FilterProvider component tests', () => {
});

test('should use default rows per page if rows per page setting could not be loaded', async () => {
const resultingFilter = Filter.fromString('fall=back rows=50');
const resultingFilter = Filter.fromString('fall=back');
const fallbackFilter = Filter.fromString('fall=back');

const getSetting = testing.fn().mockRejectedValue(new Error('an error'));
Expand Down
18 changes: 13 additions & 5 deletions src/web/hooks/usePageFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Filter, {
DEFAULT_FALLBACK_FILTER,
DEFAULT_ROWS_PER_PAGE,
RESET_FILTER,
NO_ROW_LIMIT,
} from 'gmp/models/filter';

import {isDefined, hasValue} from 'gmp/utils/identity';
Expand Down Expand Up @@ -74,6 +75,7 @@ const usePageFilter = (
userSettingDefaultSel.getError(),
];
});

const pageFilter = useShallowEqualSelector(state =>
getPage(state).getFilter(pageName),
);
Expand Down Expand Up @@ -113,12 +115,14 @@ const usePageFilter = (

useEffect(() => {
if (hasValue(locationQueryFilterString)) {
dispatch(
setPageFilter(pageName, Filter.fromString(locationQueryFilterString)),
);
const filter = Filter.fromString(locationQueryFilterString);
if (!filter.has('rows') && isDefined(rowsPerPage)) {
filter.set('rows', rowsPerPage);
}
dispatch(setPageFilter(pageName, filter));
}
setLocationQueryFilter(undefined);
}, []); // eslint-disable-line react-hooks/exhaustive-deps
}, [locationQueryFilterString, rowsPerPage, dispatch, pageName]);

if (hasValue(locationQueryFilter)) {
returnedFilter = locationQueryFilter;
Expand All @@ -140,7 +144,11 @@ const usePageFilter = (
rowsPerPage = DEFAULT_ROWS_PER_PAGE;
}

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

Expand Down

0 comments on commit e0da414

Please sign in to comment.