Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination to eCR Viewer homepage #1889

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions containers/ecr-viewer/src/app/ListEcrViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"use client";

import { Table } from "@trussworks/react-uswds";
import { ListEcr } from "@/app/api/services/listEcrDataService";
import { useState } from "react";
import { Pagination } from "@trussworks/react-uswds";

interface ListEcrViewerProps {
listFhirData: ListEcr;
Expand All @@ -15,8 +19,24 @@ export default function ListECRViewer({
listFhirData,
}: ListEcrViewerProps): JSX.Element {
const header = ["eCR ID", "Stored Date"];
const [currentPage, setCurrentPage] = useState(1);
const itemsPerPage = 25;
const totalPages = Math.ceil(listFhirData.length / itemsPerPage);

const handlePageChange = (pageNumber: number) => {
setCurrentPage(pageNumber);
renderPage(pageNumber);
};

const renderPage = (pageNumber: number) => {
const startIndex = (pageNumber - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const pageData = listFhirData.slice(startIndex, endIndex);
return renderListEcrTableData(pageData);
};

return (
<div className="content-wrapper">
<div className="homepage-wrapper">
<Table
bordered={false}
fullWidth={true}
Expand All @@ -32,8 +52,21 @@ export default function ListECRViewer({
))}
</tr>
</thead>
<tbody>{renderListEcrTableData(listFhirData)}</tbody>
<tbody>{renderPage(currentPage)}</tbody>
</Table>
<Pagination
currentPage={currentPage}
totalPages={totalPages}
pathname={""}
onClickNext={() => handlePageChange(currentPage + 1)}
onClickPrevious={() => handlePageChange(currentPage - 1)}
onClickPageNumber={(
_event: React.MouseEvent<HTMLButtonElement>,
page: number,
) => {
handlePageChange(page);
}}
/>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import { axe } from "jest-axe";
import ListECRViewer from "@/app/ListEcrViewer";

Expand Down Expand Up @@ -28,3 +28,50 @@ describe("Home Page, ListECRViewer", () => {
expect(await axe(container)).toHaveNoViolations();
});
});

describe("Pagination for home page", () => {
const listFhirData = Array.from({ length: 51 }, (_, i) => ({
ecrId: `id-${i + 1}`,
dateModified: `2021-01-0${(i % 9) + 1}`,
}));
beforeEach(() => {
render(<ListECRViewer listFhirData={listFhirData} />);
});

it("should render first page correctly", () => {
const rows = screen.getAllByRole("row");
expect(rows).toHaveLength(26); // 25 data rows + 1 header row
});

it("should navigate to the next page correctly using the Next button", () => {
const nextButton = screen.getByTestId("pagination-next");
fireEvent.click(nextButton);

const rows = screen.getAllByRole("row");
expect(rows).toHaveLength(26);
expect(screen.getByText("id-26")).toBeInTheDocument();
expect(screen.getByText("id-50")).toBeInTheDocument();
});

it("should navigate to the previous page correctly using the Previous button", () => {
const nextButton = screen.getByTestId("pagination-next");
fireEvent.click(nextButton); // Must navigate past 1st page so Previous button can display

const previousButton = screen.getByTestId("pagination-previous");
fireEvent.click(previousButton);

const rows = screen.getAllByRole("row");
expect(rows).toHaveLength(26);
expect(screen.getByText("id-1")).toBeInTheDocument();
expect(screen.getByText("id-25")).toBeInTheDocument();
});

it("should navigate to a specific page correctly when clicking page button", () => {
const page3Button = screen.getByText("3", { selector: "button" });
fireEvent.click(page3Button);

const rows = screen.getAllByRole("row");
expect(rows).toHaveLength(2);
expect(screen.getByText("id-51")).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Home Page, ListECRViewer should match snapshot 1`] = `
<div>
<div
class="content-wrapper"
class="homepage-wrapper"
>
<table
class="usa-table usa-table--borderless width-full table-homepage-list"
Expand Down Expand Up @@ -62,6 +62,28 @@ exports[`Home Page, ListECRViewer should match snapshot 1`] = `
</tr>
</tbody>
</table>
<nav
aria-label="Pagination"
class="usa-pagination"
>
<ul
class="usa-pagination__list"
>
<li
class="usa-pagination__item usa-pagination__page-no"
>
<button
aria-current="page"
aria-label="Page 1"
class="usa-button usa-button--unstyled usa-pagination__button usa-current"
data-testid="pagination-page-number"
type="button"
>
1
</button>
</li>
</ul>
</nav>
</div>
</div>
`;
18 changes: 18 additions & 0 deletions containers/ecr-viewer/src/styles/custom-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ td {
font-weight: bold;
}

.homepage-wrapper {
display: flex;
max-width: 1440px;
gap: 48px;
overflow: visible;
align-items: center;
flex-wrap: wrap;
flex-direction: column;

.usa-table {
margin-bottom: 0 !important;
}

.usa-pagination {
margin-top: 0 !important;
}
}

.main-container {
display: flex;
justify-content: center;
Expand Down
Loading