From f036fe0684c7cc2f12724814a6a6803504a571a5 Mon Sep 17 00:00:00 2001 From: Midhun G S Date: Sat, 11 Dec 2021 04:40:19 +0530 Subject: [PATCH] [Bug Fix #1521] Home Page Pagination (#1558) * Bug Fix #1521 - Pagination * bugFix-1521 changed pagination design --- frontend/src/HomePage/HomePage.jsx | 14 ++-- frontend/src/_components/Pagination.jsx | 95 ++++++++++++++++++++----- 2 files changed, 88 insertions(+), 21 deletions(-) diff --git a/frontend/src/HomePage/HomePage.jsx b/frontend/src/HomePage/HomePage.jsx index 2ed7f658bc..2abcd88271 100644 --- a/frontend/src/HomePage/HomePage.jsx +++ b/frontend/src/HomePage/HomePage.jsx @@ -36,10 +36,11 @@ class HomePage extends React.Component { this.fetchFolders(); } - fetchApps = (page, folder) => { + fetchApps = (page = 1, folder) => { this.setState({ apps: [], isLoading: true, + currentPage: page, }); appService.getAll(page, folder).then((data) => @@ -65,7 +66,6 @@ class HomePage extends React.Component { }; pageChanged = (page) => { - this.setState({ currentPage: page }); this.fetchApps(page, this.state.currentFolder.id); }; @@ -274,7 +274,14 @@ class HomePage extends React.Component { hideProgressBar: true, position: 'top-center', }); - this.fetchApps(this.state.currentPage || 1, this.state.currentFolder.id); + this.fetchApps( + this.state.currentPage + ? this.state.apps?.length === 1 + ? this.state.currentPage - 1 + : this.state.currentPage + : 1, + this.state.currentFolder.id + ); this.fetchFolders(); }) .catch(({ error }) => { @@ -396,7 +403,6 @@ class HomePage extends React.Component { )} diff --git a/frontend/src/_components/Pagination.jsx b/frontend/src/_components/Pagination.jsx index f24f09e435..1f8344fd6b 100644 --- a/frontend/src/_components/Pagination.jsx +++ b/frontend/src/_components/Pagination.jsx @@ -1,18 +1,38 @@ -import React from 'react'; +import React, { useMemo } from 'react'; -export const Pagination = function Pagination({ currentPage, count, totalPages, pageChanged }) { - function renderPageItem(i) { - return ( -
  • gotoPage(i + 1)} className={`page-item ${currentPage === i + 1 ? 'active' : ''}`}> - {i + 1} -
  • - ); - } +export const Pagination = function Pagination({ currentPage, count, pageChanged, itemsPerPage = 10 }) { + const totalPages = useMemo(() => { + return Math.floor((count - 1) / itemsPerPage) + 1; + }, [count, itemsPerPage]); + + const getPageLinks = (index) => { + if (index < 1 || index > totalPages) { + return; + } else { + return ( +
  • gotoPage(index)} + className={`page-item ${currentPage === index ? 'active' : ''}`} + > + {index} +
  • + ); + } + }; function gotoPage(page) { pageChanged(page); } + function gotoFirstPage() { + pageChanged(1); + } + + function gotoLastPage() { + pageChanged(totalPages); + } + function gotoNextPage() { gotoPage(currentPage + 1); } @@ -21,23 +41,42 @@ export const Pagination = function Pagination({ currentPage, count, totalPages, gotoPage(currentPage - 1); } - function startingAppCount(currentPage) { - return (currentPage - 1) * 10 + 1; + function startingAppCount() { + return (currentPage - 1) * itemsPerPage + 1; } - function endingAppCount(currentPage, totalCount) { - const num = currentPage * 10; + function endingAppCount() { + const num = currentPage * itemsPerPage; - return num > totalCount ? totalCount : num; + return num > count ? count : num; } return (

    - Showing {startingAppCount(currentPage)} to {endingAppCount(currentPage, count)} of{' '} - {count} + Showing {startingAppCount()} to {endingAppCount()} of {count}

      +
    • + + + + + + + +
    • - {Array.from(Array(totalPages).keys()).map((i) => renderPageItem(i))} + {getPageLinks(currentPage - 1)} + {getPageLinks(currentPage)} + {getPageLinks(currentPage + 1)}
    • +
    • + + + + + + + +
    );