generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into revise-postgresdb-and-add-middleware-connection
- Loading branch information
Showing
9 changed files
with
284 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
frontend/src/components/TemplatesIndex/TemplatesIndex.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.pagination-text { | ||
color: #71767A; | ||
font-size: 14px; | ||
font-style: normal; | ||
font-weight: 400; | ||
line-height: normal; | ||
margin-left: 40px; | ||
} | ||
|
||
.pagination-container { | ||
justify-content: space-between; | ||
} | ||
|
||
.pagination-button-group { | ||
margin-right: 40px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useState, useMemo } from "react"; | ||
|
||
|
||
const usePagination = <T>(items: T[] = [], itemsPerPage = 10, initialPage = 1) => { | ||
const [currentPage, setCurrentPage] = useState(initialPage); | ||
|
||
// Calculate total number of pages | ||
const totalPages = useMemo(() => | ||
Math.ceil(items.length / itemsPerPage), | ||
[items.length, itemsPerPage] | ||
); | ||
|
||
// Ensure current page stays within bounds | ||
useMemo(() => { | ||
if (currentPage > totalPages) { | ||
setCurrentPage(totalPages || 1); | ||
} | ||
}, [currentPage, totalPages]); | ||
|
||
// Get current page items | ||
const currentItems = useMemo(() => { | ||
const startIndex = (currentPage - 1) * itemsPerPage; | ||
const endIndex = startIndex + itemsPerPage; | ||
return items.slice(startIndex, endIndex); | ||
}, [items, currentPage, itemsPerPage]); | ||
|
||
// Navigation functions | ||
const goToPage = (pageNumber: number) => { | ||
const page = Math.max(1, Math.min(pageNumber, totalPages)); | ||
setCurrentPage(page); | ||
}; | ||
|
||
const nextPage = () => { | ||
if (currentPage < totalPages) { | ||
setCurrentPage(prev => prev + 1); | ||
} | ||
}; | ||
|
||
const previousPage = () => { | ||
if (currentPage > 1) { | ||
setCurrentPage(prev => prev - 1); | ||
} | ||
}; | ||
|
||
const firstPage = () => { | ||
setCurrentPage(1); | ||
}; | ||
|
||
const lastPage = () => { | ||
setCurrentPage(totalPages); | ||
}; | ||
|
||
// Generate page numbers for pagination display | ||
const getPageNumbers = (maxVisible = 5) => { | ||
const pages = []; | ||
let startPage = Math.max(1, currentPage - Math.floor(maxVisible / 2)); | ||
const endPage = Math.min(totalPages, startPage + maxVisible - 1); | ||
|
||
// Adjust start page if end page is maxed out | ||
if (endPage - startPage + 1 < maxVisible) { | ||
startPage = Math.max(1, endPage - maxVisible + 1); | ||
} | ||
|
||
for (let i = startPage; i <= endPage; i++) { | ||
pages.push(i); | ||
} | ||
|
||
return pages; | ||
}; | ||
|
||
return { | ||
currentPage, | ||
currentItems, | ||
totalPages, | ||
itemsPerPage, | ||
goToPage, | ||
nextPage, | ||
previousPage, | ||
firstPage, | ||
lastPage, | ||
getPageNumbers, | ||
hasNextPage: currentPage < totalPages, | ||
hasPreviousPage: currentPage > 1 | ||
}; | ||
}; | ||
|
||
export default usePagination; |
Oops, something went wrong.