saleor-dashboard/src/hooks/useLocalPaginator.ts

127 lines
3 KiB
TypeScript
Raw Normal View History

2021-06-08 08:54:13 +00:00
import { useEffect, useState } from "react";
export interface PageInfo {
endCursor: string;
hasNextPage: boolean;
hasPreviousPage: boolean;
startCursor: string;
}
export interface PaginationState {
after?: string;
before?: string;
first?: number;
last?: number;
}
2021-12-06 14:41:18 +00:00
/**
* Local pagination state.
* @param paginateBy Number of items per page.
* @returns Pagination state and setter.
*/
export function useLocalPaginationState(
paginateBy: number,
): [PaginationState, (paginationState: PaginationState) => void] {
const [state, setState] = useState<PaginationState>({
first: paginateBy,
});
const setPaginationState = (paginationState: PaginationState) => {
if (paginationState.after) {
setState({
after: paginationState.after,
first: paginateBy,
});
} else if (paginationState.before) {
setState({
before: paginationState.before,
last: paginateBy,
});
} else {
setState({
first: paginateBy,
});
}
};
2021-06-08 08:54:13 +00:00
useEffect(() => {
setState({ first: paginateBy });
2021-06-08 08:54:13 +00:00
}, [paginateBy]);
return [state, setPaginationState];
}
2021-12-06 14:41:18 +00:00
/**
* Local pagination state persisted as long as section is not changed.
* @param paginateBy Number of items per page.
* @param section Section name. When changed, pagination state is reset.
* @returns Pagination state and setter.
*/
export function useSectionLocalPaginationState(
paginateBy: number,
section: string,
2021-12-06 14:41:18 +00:00
): [PaginationState, (paginationState: PaginationState) => void] {
const [paginationSection, setPaginationSection] = useState(section);
const [paginationState, setPaginationState] = useLocalPaginationState(
paginateBy,
2021-12-06 14:41:18 +00:00
);
const fallbackPaginationState = {
first: paginateBy,
};
2021-12-06 14:41:18 +00:00
useEffect(() => {
if (section !== paginationSection) {
setPaginationState({});
}
}, [section]);
useEffect(() => {
if (section !== paginationSection) {
setPaginationSection(section);
}
}, [paginationState]);
return [
section === paginationSection ? paginationState : fallbackPaginationState,
setPaginationState,
2021-12-06 14:41:18 +00:00
];
}
function useLocalPaginator(
setPaginationState: (paginationState: PaginationState) => void,
) {
function paginate(pageInfo: PageInfo, paginationState: PaginationState) {
const loadNextPage = () =>
setPaginationState({
...paginationState,
after: pageInfo.endCursor,
before: undefined,
});
const loadPreviousPage = () =>
setPaginationState({
...paginationState,
after: undefined,
before: pageInfo.startCursor,
});
const newPageInfo = pageInfo
? {
...pageInfo,
hasNextPage: !!paginationState.before || pageInfo.hasNextPage,
hasPreviousPage: !!paginationState.after || pageInfo.hasPreviousPage,
}
: undefined;
return {
loadNextPage,
loadPreviousPage,
Enhancements to pagination navigation (#2063) * Update macaw to include Paginator changes * Add link support to TablePagination component * Rewrite usePaginator to use context and links instead of onClick * Refactor ProductList to use new usePaginator hook * Add decorator for PaginatorContext in ProductList stories * Refactor AppList to use new usePaginator hook * Refactor AttributeList to use new usePaginator hook * Add missing pagination props for local pagination to AttributeValues * Refactor CategoryList to use new usePaginator hook * Refactor CategoryDetails to use useLocalPaginator and context * Refactor CollectionList to use new usePaginator hook * Refactor CollectionProducts to use new usePaginator hook * Refactor CustomerList to use new usePaginator hook * Refactor VoucherDetailsPage to use PaginationContext * Refactor SaleDetails to use PaginatorContext * Refactor SaleList to use new usePaginator hook * Refactor VoucherList to use new usePaginator hook * Fix type error in paginatorContextValues fixture * Refactor GitfCardList to use new usePaginator hook * Remove unused imports * Refactor MenuList to use new usePaginator hook * Refactor OrderDraftList to use new usePaginator hook * Refactor OrderListPage to use new usePaginator hook * Refactor PageList to use new usePaginator hook * Refactor PageTypeList to use new usePaginator hook * Refactor PermissionGroupList to use new usePaginator hook * Refactor PluginsList to use new usePaginator hook * Refactor ProductTypeList to use new usePaginator hook * Refactor ShippingMethodProducts to use PaginationContext * Refactor ShippingZonesList to use new usePaginator hook * Refactor StaffList to use new usePaginator hook * Fix TS errors * Update TranslationEntities and TranslationFields to use new usePaginator * Refactor WarehouseList to use new usePaginator hook * Fix errors in stories that didn't use PaginationContextDecorator * Mention changes in changelog * Update to latest macaw version, update snapshots
2022-05-31 12:53:16 +00:00
paginatorType: "click" as const,
pageInfo: newPageInfo,
};
}
return paginate;
}
export default useLocalPaginator;