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

Filterprovider improvements #1653

Merged
merged 3 commits into from
Oct 1, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[#1466](https://github.com/greenbone/gsa/pull/1466) [#1467](https://github.com/greenbone/gsa/pull/1467)

### Changed
- Determine the to be applied filter of a list page in GSA and don't rely on the
backend [#1631](https://github.com/greenbone/gsa/pull/1631), [#1653](https://github.com/greenbone/gsa/pull/1653)
- Change gmpname for vulnerability and secinfo pages [#1652](https://github.com/greenbone/gsa/pull/1652)
- Use "lean" reports by default when requesting a single report [#1635](https://github.com/greenbone/gsa/pull/1635)
- Use uuid to get greenbone compliance report format [#1643](https://github.com/greenbone/gsa/pull/1643)
Expand Down
2 changes: 2 additions & 0 deletions gsa/src/gmp/models/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ export const DEFAULT_FALLBACK_FILTER = Filter.fromString('sort=name first=1');

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

export const DEFAULT_ROWS_PER_PAGE = 50;

export default Filter;

// vim: set ts=2 sw=2 tw=80:
72 changes: 72 additions & 0 deletions gsa/src/web/entities/__tests__/filterprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,44 @@ describe('FilterProvider component tests', () => {
expect(renderFunc).toHaveBeenCalledWith({filter: resultingFilter});
});

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

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

const getSetting = jest.fn().mockResolvedValue({});
const gmp = {
user: {
getSetting,
},
};

const {render, store} = rendererWith({
gmp,
store: true,
router: true,
});

store.dispatch(loadingActions.success({rowsperpage: {value: '42'}}));
store.dispatch(
defaultFilterLoadingActions.error('task', new Error('an error')),
);

const renderFunc = jest
.fn()
.mockReturnValue(<span data-testid="awaiting-span" />);

const {getByTestId} = render(
<FilterProvider gmpname="task" fallbackFilter={fallbackFilter}>
{renderFunc}
</FilterProvider>,
);

await waitForElement(() => getByTestId('awaiting-span'));

expect(renderFunc).toHaveBeenCalledWith({filter: resultingFilter});
});

test('should use fallbackFilter if given', async () => {
// if no usersettings defaultFilter exists use the given fallbackFilter
const resultingFilter = Filter.fromString('fall=back rows=42');
Expand Down Expand Up @@ -257,4 +295,38 @@ describe('FilterProvider component tests', () => {

expect(renderFunc).toHaveBeenCalledWith({filter: resultingFilter});
});

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 fallbackFilter = Filter.fromString('fall=back');

const getSetting = jest.fn().mockRejectedValue(new Error('an error'));
const gmp = {
user: {
getSetting,
},
};

const {render, store} = rendererWith({
gmp,
store: true,
router: true,
});

store.dispatch(loadingActions.error(new Error('an error')));

const renderFunc = jest
.fn()
.mockReturnValue(<span data-testid="awaiting-span" />);

const {getByTestId} = render(
<FilterProvider fallbackFilter={fallbackFilter} gmpname="task">
{renderFunc}
</FilterProvider>,
);

await waitForElement(() => getByTestId('awaiting-span'));

expect(renderFunc).toHaveBeenCalledWith({filter: resultingFilter});
});
});
50 changes: 39 additions & 11 deletions gsa/src/web/entities/filterprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {withRouter} from 'react-router-dom';

import {ROWS_PER_PAGE_SETTING_ID} from 'gmp/commands/users';

import Filter, {DEFAULT_FALLBACK_FILTER} from 'gmp/models/filter';
import Filter, {
DEFAULT_FALLBACK_FILTER,
DEFAULT_ROWS_PER_PAGE,
} from 'gmp/models/filter';

import {isDefined} from 'gmp/utils/identity';

Expand Down Expand Up @@ -53,19 +56,35 @@ const FilterProvider = ({

let returnedFilter;

const defaultSettingFilter = useSelector(state =>
getUserSettingsDefaultFilter(state, gmpname).getFilter(),
const [defaultSettingFilter, defaultSettingsFilterError] = useSelector(
state => {
const defaultFilterSel = getUserSettingsDefaultFilter(state, gmpname);
return [defaultFilterSel.getFilter(), defaultFilterSel.getError()];
},
);

useEffect(() => {
if (!isDefined(defaultSettingFilter)) {
if (
!isDefined(defaultSettingFilter) &&
!isDefined(defaultSettingsFilterError)
) {
dispatch(loadUserSettingsDefaultFilter(gmp)(gmpname));
}
}, [defaultSettingFilter, dispatch, gmp, gmpname]);

const rowsPerPage = useSelector(state =>
getUserSettingsDefaults(state).getValueByName('rowsperpage'),
);
}, [
defaultSettingFilter,
defaultSettingsFilterError,
dispatch,
gmp,
gmpname,
]);

let [rowsPerPage, rowsPerPageError] = useSelector(state => {
const userSettingDefaultSel = getUserSettingsDefaults(state);
return [
userSettingDefaultSel.getValueByName('rowsperpage'),
userSettingDefaultSel.getError(),
];
});

useEffect(() => {
if (!isDefined(rowsPerPage)) {
Expand All @@ -90,21 +109,30 @@ const FilterProvider = ({
returnedFilter = locationQueryFilter;
} else if (isDefined(pageFilter)) {
returnedFilter = pageFilter;
} else if (isDefined(defaultSettingFilter) && defaultSettingFilter !== null) {
} else if (
isDefined(defaultSettingFilter) &&
!isDefined(defaultSettingsFilterError) &&
defaultSettingFilter !== null
) {
returnedFilter = defaultSettingFilter;
} else if (isDefined(fallbackFilter)) {
returnedFilter = fallbackFilter;
} else {
returnedFilter = DEFAULT_FALLBACK_FILTER;
}

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

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

const showChildren =
isDefined(returnedFilter) &&
isDefined(defaultSettingFilter) &&
(isDefined(defaultSettingFilter) ||
isDefined(defaultSettingsFilterError)) &&
isDefined(rowsPerPage);

return (
Expand Down