From f60c9e4e67f659ec02252dd0c5befdb89637fb09 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Mon, 7 Nov 2022 20:55:58 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Refactored=20the=20T2Table=20com?= =?UTF-8?q?ponent=20with=20the=20new=20pagination=20controls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/components/Library/Library.tsx | 247 ++++++---------------- src/client/components/shared/T2Table.tsx | 116 +++++++--- 2 files changed, 153 insertions(+), 210 deletions(-) diff --git a/src/client/components/Library/Library.tsx b/src/client/components/Library/Library.tsx index 94edcd9..e165e09 100644 --- a/src/client/components/Library/Library.tsx +++ b/src/client/components/Library/Library.tsx @@ -3,22 +3,11 @@ import PropTypes from "prop-types"; import { useNavigate } from "react-router-dom"; import { isEmpty, isNil, isUndefined } from "lodash"; import MetadataPanel from "../shared/MetadataPanel"; -import SearchBar from "./SearchBar"; -import { Link } from "react-router-dom"; +import T2Table from "../shared/T2Table"; import { useDispatch, useSelector } from "react-redux"; import { searchIssue } from "../../actions/fileops.actions"; import ellipsize from "ellipsize"; -import { - ColumnDef, - flexRender, - getCoreRowModel, - getFilteredRowModel, - useReactTable, - PaginationState, -} from "@tanstack/react-table"; - - export const Library = (): ReactElement => { const searchResults = useSelector( (state: RootState) => state.fileOps.libraryComics, @@ -45,173 +34,6 @@ export const Library = (): ReactElement => { ); }, []); - const [{ pageIndex, pageSize }, setPagination] = - React.useState({ - pageIndex: 1, - pageSize: 15, - }); - - - const pagination = React.useMemo( - () => ({ - pageIndex, - pageSize, - }), - [pageIndex, pageSize] - ); - - const T2Table = (tableOptions): ReactElement => { - const { columns, totalPages, rowClickHandler } = - tableOptions; - - // pagination methods - const goToNextPage = useCallback(() => { - setPagination({ - pageIndex: pageIndex + 1, - pageSize, - }); - dispatch( - searchIssue( - { - query: {}, - }, - { - pagination: { - size: pageSize, - from: pageSize * pageIndex + 1, - }, - type: "all", - trigger: "libraryPage", - }, - ), - ); - }, []); - - - const goToPreviousPage = useCallback(() => { - setPagination({ - pageIndex: pageIndex - 1, - pageSize, - }); - let from = 0; - if (pageIndex === 2) { - from = (pageIndex - 1) * pageSize + 2 - 17; - } else { - from = (pageIndex - 1) * pageSize + 2 - 16; - } - dispatch( - searchIssue( - { - query: {}, - }, - { - pagination: { - size: pageSize, - from, - }, - type: "all", - trigger: "libraryPage" - }, - ), - ); - }, []); - - const table = useReactTable({ - data: searchResults.hits.hits, - columns, - manualPagination: true, - getCoreRowModel: getCoreRowModel(), - pageCount: searchResults?.hits?.hits?.length ?? -1, - state: { - pagination, - }, - onPaginationChange: setPagination, - }); - - return ( - <> -
- {/* Search bar */} -
- -
- {/* pagination controls */} - -
- - - {table.getHeaderGroups().map((headerGroup, idx) => ( - - {headerGroup.headers.map((header, idx) => ( - - ))} - - ))} - - - - {table.getRowModel().rows.map((row, idx) => { - return ( - rowClickHandler(row)} - > - {row.getVisibleCells().map(cell => ( - - ))} - - ); - })} - -
- {header.isPlaceholder - ? null - : flexRender( - header.column.columnDef.header, - header.getContext() - )} -
- {flexRender(cell.column.columnDef.cell, cell.getContext())} -
- - ); - }; - // programatically navigate to comic detail const navigate = useNavigate(); const navigateToComicDetail = (row) => { @@ -316,11 +138,69 @@ export const Library = (): ReactElement => { } ], []); + + /** + * Pagination control that fetches the next x (pageSize) items + * based on the y (pageIndex) offset from the Elasticsearch index + * @param {number} pageIndex + * @param {number} pageSize + * @returns void + * + **/ + const nextPage = useCallback((pageIndex: number, pageSize: number) => { + dispatch( + searchIssue( + { + query: {}, + }, + { + pagination: { + size: pageSize, + from: pageSize * pageIndex + 1, + }, + type: "all", + trigger: "libraryPage", + }, + ), + ); + }, []); + + + /** + * Pagination control that fetches the previous x (pageSize) items + * based on the y (pageIndex) offset from the Elasticsearch index + * @param {number} pageIndex + * @param {number} pageSize + * @returns void + **/ + const previousPage = useCallback((pageIndex: number, pageSize: number) => { + let from = 0; + if (pageIndex === 2) { + from = (pageIndex - 1) * pageSize + 2 - 17; + } else { + from = (pageIndex - 1) * pageSize + 2 - 16; + } + dispatch( + searchIssue( + { + query: {}, + }, + { + pagination: { + size: pageSize, + from, + }, + type: "all", + trigger: "libraryPage" + }, + ), + ); + }, []); + // ImportStatus.propTypes = { // value: PropTypes.bool.isRequired, // }; - return (
@@ -333,10 +213,13 @@ export const Library = (): ReactElement => { - {/* pagination controls */}
)} diff --git a/src/client/components/shared/T2Table.tsx b/src/client/components/shared/T2Table.tsx index 9b30746..e08106e 100644 --- a/src/client/components/shared/T2Table.tsx +++ b/src/client/components/shared/T2Table.tsx @@ -1,5 +1,7 @@ -import React, { ReactElement, useState } from "react"; +import React, { ReactElement, useMemo, useState } from "react"; import PropTypes from "prop-types"; +import SearchBar from "../Library/SearchBar"; +import { Link } from "react-router-dom"; import { ColumnDef, flexRender, @@ -10,35 +12,102 @@ import { } from "@tanstack/react-table"; export const T2Table = (tableOptions): ReactElement => { - const { rowData, columns, paginationHandlers: { nextPage, previousPage }, totalPages, rowClickHandler } = + const { sourceData, columns, paginationHandlers: { nextPage, previousPage }, totalPages, rowClickHandler } = tableOptions; - - const table = useReactTable({ - data: rowData, - columns, - manualPagination: true, - getCoreRowModel: getCoreRowModel(), - getFilteredRowModel: getFilteredRowModel(), - pageCount: totalPages, - // getPaginationRowModel: getPaginationRowModel(), - }); const [{ pageIndex, pageSize }, setPagination] = - React.useState({ - pageIndex: 0, - pageSize: 10, - }) + useState({ + pageIndex: 1, + pageSize: 15, + }); - const pagination = React.useMemo( + + const pagination = useMemo( () => ({ pageIndex, pageSize, }), [pageIndex, pageSize] - ) + ); + + /** + * Pagination control to move forward one page + * @returns void + */ + const goToNextPage = () => { + setPagination({ + pageIndex: pageIndex + 1, + pageSize, + }); + nextPage(pageIndex, pageSize); + } + + /** + * Pagination control to move backward one page + * @returns void + **/ + const goToPreviousPage = () => { + setPagination({ + pageIndex: pageIndex - 1, + pageSize, + }); + previousPage(pageIndex, pageSize); + } + + const table = useReactTable({ + data: sourceData, + columns, + manualPagination: true, + getCoreRowModel: getCoreRowModel(), + pageCount: sourceData.length ?? -1, + state: { + pagination, + }, + onPaginationChange: setPagination, + }); return ( <> +
+ {/* Search bar */} +
+ +
+ {/* pagination controls */} + +
{table.getHeaderGroups().map((headerGroup, idx) => ( @@ -77,21 +146,12 @@ export const T2Table = (tableOptions): ReactElement => { })}
- - {/* pagination control */} - - ); }; T2Table.propTypes = { - rowData: PropTypes.array, + sourceData: PropTypes.array, totalPages: PropTypes.number, columns: PropTypes.array, paginationHandlers: PropTypes.shape({