Skip to content

Commit

Permalink
Merge pull request #3540 from HHS/OPS-3524/can_list_pagination_fix
Browse files Browse the repository at this point in the history
fix: fixed can list table pagination bug
  • Loading branch information
weimiao67 authored Feb 27, 2025
2 parents da184de + 4d8635b commit a80244c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions frontend/cypress/e2e/canList.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ describe("CAN List", () => {
.find("svg")
.should("have.attr", "aria-hidden", "true");

// switch fiscal year to 2025
cy.get("#fiscal-year-select").select("2025");
cy.wait(500);
cy.get("tbody").find("tr").should("have.length", 4);

// switch fiscal year to 2023
cy.get("#fiscal-year-select").select("2023");
cy.wait(500);
cy.get("li").should("have.class", "usa-pagination__item").contains("2").click();

// go back to the first page
cy.get("li").should("have.class", "usa-pagination__item").contains("1").click();
cy.get("button").should("have.class", "usa-current").contains("1");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect } from "react";
import { calculatePercent, formatDateNeeded } from "../../../helpers/utils";
import Table from "../../UI/Table";
import { CAN_HEADERS, PORTFOLIO_HEADERS } from "./CABBudgetLineTable.constants";
Expand All @@ -12,6 +12,7 @@ import PaginationNav from "../../UI/PaginationNav";
* @typedef {Object} CANBudgetLineTableProps
* @property {BudgetLine[]} budgetLines
* @property {number} totalFunding
* @property {number} fiscalYear
* @property {'portfolio' | 'can'} [tableType]
*/

Expand All @@ -20,12 +21,16 @@ import PaginationNav from "../../UI/PaginationNav";
* @param {CANBudgetLineTableProps} props
* @returns {JSX.Element} - The component JSX.
*/
const CANBudgetLineTable = ({ budgetLines, totalFunding, tableType = "can" }) => {
const CANBudgetLineTable = ({ budgetLines, totalFunding, fiscalYear, tableType = "can" }) => {
const ITEMS_PER_PAGE = import.meta.env.MODE === "production" ? 25 : 3;
const [currentPage, setCurrentPage] = React.useState(1);
let visibleBudgetLines = [...budgetLines];
visibleBudgetLines = visibleBudgetLines.slice((currentPage - 1) * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE);

useEffect(() => {
setCurrentPage(1);
}, [fiscalYear]);

if (budgetLines.length === 0) {
return <p className="text-center">No budget lines have been added to this CAN.</p>;
}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/CANs/CANTable/CANTable.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from "prop-types";
import React from "react";
import React, { useEffect } from "react";
import { NO_DATA } from "../../../constants";
import PaginationNav from "../../UI/PaginationNav";
import { findFundingBudgetBudgetByFiscalYear, formatObligateBy } from "./CANTable.helpers";
Expand All @@ -22,6 +22,10 @@ const CANTable = ({ cans, fiscalYear }) => {
let cansPerPage = [...cans];
cansPerPage = cansPerPage.slice((currentPage - 1) * CANS_PER_PAGE, currentPage * CANS_PER_PAGE);

useEffect(() => {
setCurrentPage(1);
}, [fiscalYear]);

if (cans.length === 0) {
return <p className="text-center">No CANs found</p>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const PortfolioSpending = () => {
<CANBudgetLineTable
budgetLines={budgetLineItems}
totalFunding={totalFunding}
fiscalYear={fiscalYear}
tableType="portfolio"
/>
</>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/cans/detail/CanSpending.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const CanSpending = ({
<CANBudgetLineTable
budgetLines={budgetLines}
totalFunding={totalFunding}
fiscalYear={fiscalYear}
/>
</article>
);
Expand Down

0 comments on commit a80244c

Please sign in to comment.