From df2f69e85fab05e02b01ce6657d0ed43af756564 Mon Sep 17 00:00:00 2001 From: fadingNA Date: Wed, 6 Nov 2024 22:33:26 -0500 Subject: [PATCH] adjust table of document with mx auto, add pagination to Documents Component --- frontend/src/index.css | 4 +- frontend/src/settings/Documents.tsx | 65 ++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/frontend/src/index.css b/frontend/src/index.css index 4319403e..090a31ca 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -50,11 +50,11 @@ body.dark { @layer components { .table-default { - @apply block w-max table-auto content-center justify-center rounded-xl border border-silver dark:border-silver/40 text-center dark:text-bright-gray; + @apply block w-max mx-auto table-fixed content-center justify-center rounded-xl border border-silver dark:border-silver/40 text-center dark:text-bright-gray; } .table-default th { - @apply p-4 w-[244px] font-normal text-gray-400; /* Remove border-r */ + @apply p-4 w-[250px] font-normal text-gray-400; /* Remove border-r */ } .table-default th:last-child { diff --git a/frontend/src/settings/Documents.tsx b/frontend/src/settings/Documents.tsx index f94d1a87..cbb1048c 100644 --- a/frontend/src/settings/Documents.tsx +++ b/frontend/src/settings/Documents.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { useDispatch } from 'react-redux'; @@ -14,6 +14,7 @@ import { getDocs } from '../preferences/preferenceApi'; import { setSourceDocs } from '../preferences/preferenceSlice'; import Input from '../components/Input'; import Upload from '../upload/Upload'; // Import the Upload component +import Pagination from '../components/DocumentPagination'; // Utility function to format numbers const formatTokens = (tokens: number): string => { @@ -47,22 +48,55 @@ const Documents: React.FC = ({ const [loading, setLoading] = useState(false); const [sortField, setSortField] = useState<'date' | 'tokens'>('date'); const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc'); + // Pagination + const [currentPage, setCurrentPage] = useState(1); + const [rowsPerPage, setRowsPerPage] = useState(10); + const [totalPages, setTotalPages] = useState(0); + const [fetchedDocuments, setFetchedDocuments] = useState([]); + // Filter documents based on the search term + const filteredDocuments = documents?.filter((document) => + document.name.toLowerCase().includes(searchTerm.toLowerCase()), + ); + // State for documents + const startIndex = (currentPage - 1) * rowsPerPage; + const endIndex = startIndex + rowsPerPage; + const currentDocuments = filteredDocuments + ? filteredDocuments.slice(startIndex, endIndex) + : []; + const syncOptions = [ { label: 'Never', value: 'never' }, { label: 'Daily', value: 'daily' }, { label: 'Weekly', value: 'weekly' }, { label: 'Monthly', value: 'monthly' }, ]; - const refreshDocs = (field: 'date' | 'tokens') => { + + useEffect(() => { + if (fetchedDocuments && rowsPerPage > 0) { + setTotalPages( + Math.max(1, Math.ceil(fetchedDocuments.length / rowsPerPage)), + ); + } else { + setTotalPages(0); + } + }, [fetchedDocuments, rowsPerPage]); + + const refreshDocs = ( + field: 'date' | 'tokens', + pageNumber = currentPage, + rows = rowsPerPage, + ) => { if (field === sortField) { setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc'); } else { setSortOrder('desc'); setSortField(field); } - getDocs(sortField, sortOrder) + console.log('refreshDocs', field, sortOrder, pageNumber, rows); + getDocs(sortField, sortOrder, pageNumber, rows) .then((data) => { dispatch(setSourceDocs(data)); + setFetchedDocuments(data ? data : []); }) .catch((error) => console.error(error)) .finally(() => { @@ -85,11 +119,6 @@ const Documents: React.FC = ({ }); }; - // Filter documents based on the search term - const filteredDocuments = documents?.filter((document) => - document.name.toLowerCase().includes(searchTerm.toLowerCase()), - ); - return (
@@ -154,15 +183,15 @@ const Documents: React.FC = ({ - {!filteredDocuments?.length && ( + {!currentDocuments?.length && ( {t('settings.documents.noData')} )} - {filteredDocuments && - filteredDocuments.map((document, index) => ( + {Array.isArray(currentDocuments) && + currentDocuments.map((document, index) => ( {document.name} {document.date} @@ -221,6 +250,20 @@ const Documents: React.FC = ({
)}
+ { + setCurrentPage(page); + refreshDocs(sortField, page, rowsPerPage); + }} + onRowsPerPageChange={(rows) => { + setRowsPerPage(rows); + setCurrentPage(1); + refreshDocs(sortField, 1, rows); + }} + /> ); };