All files / src/utils/hooks usePagination.ts

71.42% Statements 5/7
45.45% Branches 5/11
100% Functions 2/2
71.42% Lines 5/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34      66x 8x             66x                 1x       1x                  
import { TableModeDefaultPageSizeMapping } from "@config/base";
import { PaginationProps, TableMode } from "@props/RecordProps";
 
export const initPagination = (tableMode: TableMode): PaginationProps => {
  return {
    pageSize: TableModeDefaultPageSizeMapping[tableMode],
    current: 1,
    total: 0
  };
};
 
export const paginationReducer = (state: PaginationProps, action: {
  type: 'init' | 'set';
  payload: {
    tableMode?: TableMode,
    current?: number;
    total?: number;
    pageSize?: number;
  };
}): PaginationProps => {
  switch (action.type) {
    case 'init':
      return initPagination(action.payload.tableMode ?? "table-list");
    case 'set':
      return {
        total: action.payload.total ?? state.total,
        current: action.payload.current ?? state.current,
        pageSize: action.payload.pageSize ?? state.pageSize,
      };
    default:
      throw new Error("Not support action ", action.type);
  }
};