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

Add a usePagination hooks #4065

Merged
merged 1 commit into from
Jun 13, 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
108 changes: 108 additions & 0 deletions src/web/hooks/__tests__/usePagination.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/* eslint-disable react/prop-types */

import {describe, test, expect, testing} from '@gsa/testing';

import {fireEvent, render, screen} from 'web/utils/testing';

import Filter from 'gmp/models/filter';

import usePagination from '../usePagination';

const TestComponent = ({filter, counts, changeFilter}) => {
const [first, last, next, previous] = usePagination(
filter,
counts,
changeFilter,
);
return (
<>
<button data-testid="first" onClick={first} />
<button data-testid="last" onClick={last} />
<button data-testid="next" onClick={next} />
<button data-testid="previous" onClick={previous} />
</>
);
};

describe('usePageFilter', () => {
test('should change the filter for the first page', () => {
const filter = Filter.fromString('first=10');
const counts = {filtered: 100, rows: 10};
const changeFilter = testing.fn();

render(
<TestComponent
filter={filter}
counts={counts}
changeFilter={changeFilter}
/>,
);

fireEvent.click(screen.getByTestId('first'));

expect(changeFilter).toHaveBeenCalledWith(Filter.fromString('first=1'));
});

test('should change the filter for the last page', () => {
const filter = Filter.fromString('first=10');
const counts = {filtered: 100, rows: 10};
const changeFilter = testing.fn();

render(
<TestComponent
filter={filter}
counts={counts}
changeFilter={changeFilter}
/>,
);

fireEvent.click(screen.getByTestId('last'));

expect(changeFilter).toHaveBeenCalledWith(Filter.fromString('first=91'));
});

test('should change the filter for the next page', () => {
const filter = Filter.fromString('first=10');
const counts = {filtered: 100, rows: 10};
const changeFilter = testing.fn();

render(
<TestComponent
filter={filter}
counts={counts}
changeFilter={changeFilter}
/>,
);

fireEvent.click(screen.getByTestId('next'));

expect(changeFilter).toHaveBeenCalledWith(
Filter.fromString('first=20 rows=10'),
);
});

test('should change the filter for the previous page', () => {
const filter = Filter.fromString('first=10');
const counts = {filtered: 100, rows: 10};
const changeFilter = testing.fn();

render(
<TestComponent
filter={filter}
counts={counts}
changeFilter={changeFilter}
/>,
);

fireEvent.click(screen.getByTestId('previous'));

expect(changeFilter).toHaveBeenCalledWith(
Filter.fromString('first=1 rows=10'),
);
});
});
54 changes: 54 additions & 0 deletions src/web/hooks/usePagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {useCallback} from 'react';

/**
* Hook to to get the filter for the next, previous, last and first page for a
* list of entities
*
* @example
*
* const gmp = useGmp();
* const [filter, setFilter] = useState(new Filter());
* const [entities, setEntities] = useState([]);
* const [counts, setCounts] = useState({});
* const updateFilter = useCallback(filter => {
* setFilter(filter);
* gmp.tasks.get(filter).then(response => {setEntities(response.data);setCounts(response.meta.counts)});
* }, [gmp.tasks]);
* const [first, last, next, previous] = usePagination(filter, counts, updateFilter)
*
* @param {Filter} filter Current applied filter
* @param {Object} counts Current entities counts. Required for calculating the
* last page.
* @param {Function} changeFilter Function to call when the new filter is applied
* @returns {Array} Tuple of functions to update the filter for the first, last,
* next and previous page.
*/
const usePagination = (filter, counts, changeFilter) => {
const getNext = useCallback(() => {
changeFilter(filter.next());
daniele-mng marked this conversation as resolved.
Show resolved Hide resolved
}, [filter, changeFilter]);

const getPrevious = useCallback(() => {
changeFilter(filter.previous());
}, [filter, changeFilter]);

const getFirst = useCallback(() => {
changeFilter(filter.first());
}, [filter, changeFilter]);

const getLast = useCallback(() => {
const last =
Math.floor((counts.filtered - 1) / counts.rows) * counts.rows + 1;
const newFilter = filter.first(last);
changeFilter(newFilter);
}, [filter, counts, changeFilter]);

return [getFirst, getLast, getNext, getPrevious];
};

export default usePagination;
Loading