mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
08c4b60cef
* feat: update useProjectApi hook * fix: refactor to hooks * fix: remove some ts errors * fix: set message if error exists directly on response * fix: remove console logs * fix: typo * delete: context2 * feat: filter added user from user add list * fix: cleanup PR based on feedback * fix: handle undefined roles in ProjectRoleSelect * fix: use target value * fix: type event * fix: conflict * fix: add appropriate types * fix conflicts * fix: explicit query * fix: refactor list * refactor: permission icon button * fix: conflict * fix: ts errors * refactor: break list into its own component * fix: use stringifed deps * fix: explicit export * fix: update pr according to comments Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { paginate } from '../utils/paginate';
|
|
|
|
const usePagination = (
|
|
data: any[],
|
|
limit: number,
|
|
filterFunc?: (item: any) => boolean
|
|
) => {
|
|
const [paginatedData, setPaginatedData] = useState([[]]);
|
|
const [pageIndex, setPageIndex] = useState(0);
|
|
|
|
useEffect(() => {
|
|
let dataToPaginate = data;
|
|
|
|
if (filterFunc) {
|
|
dataToPaginate = data.filter(filterFunc);
|
|
}
|
|
|
|
const result = paginate(dataToPaginate, limit);
|
|
setPaginatedData(result);
|
|
/* eslint-disable-next-line */
|
|
}, [JSON.stringify(data), limit]);
|
|
|
|
const nextPage = () => {
|
|
if (pageIndex < paginatedData.length - 1) {
|
|
setPageIndex(prev => prev + 1);
|
|
}
|
|
};
|
|
|
|
const prevPage = () => {
|
|
if (pageIndex > 0) {
|
|
setPageIndex(prev => prev - 1);
|
|
}
|
|
};
|
|
|
|
const lastPage = () => {
|
|
setPageIndex(paginatedData.length - 1);
|
|
};
|
|
|
|
const firstPage = () => {
|
|
setPageIndex(0);
|
|
};
|
|
|
|
return {
|
|
page: paginatedData[pageIndex] || [],
|
|
pages: paginatedData,
|
|
nextPage,
|
|
prevPage,
|
|
lastPage,
|
|
firstPage,
|
|
setPageIndex,
|
|
pageIndex,
|
|
};
|
|
};
|
|
|
|
export default usePagination;
|