Skip to content

Commit

Permalink
[FIX] - Pagination Component redundant fetches (#115)
Browse files Browse the repository at this point in the history
* Implemented fix for pagination component

* Added onKeyUp event handler to input

* Cleared up redundant param in handleBlur fn

* Fixed issue involving exiting focus without valid pagenum input

* Hitting enter key now removes focus

* Ran linter
  • Loading branch information
kevin-pierce authored Jun 24, 2023
1 parent 0962752 commit b4f0f77
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions frontend/src/components/common/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,28 @@ const Pagination = ({
return;
}
setUserPageNum(newUserPageNum);
if (!Number.isNaN(newUserPageNum) && newUserPageNum !== pageNum) {
getRecords(newUserPageNum);
};

// Only fetch records if a valid page num is present AND the page num has changed
const fetchRecords = () => {
if (!Number.isNaN(userPageNum) && userPageNum !== pageNum) {
getRecords(userPageNum);
}
};

// Treat the enter key as an alt method of triggering onBlur (lose focus)
const handleKeyUp = (event: any) => {
if (event.keyCode === 13) {
event.target.blur();
}
};

const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
const handleBlur = () => {
if (Number.isNaN(userPageNum)) {
setUserPageNum(pageNum);
return;
}
fetchRecords();
};

const handlePageArrowPress = (newUserPageNum: number) => {
Expand Down Expand Up @@ -98,9 +111,12 @@ const Pagination = ({
max={numPages}
size="sm"
onChange={handleNumberInputChange}
onBlur={(e) => handleBlur(e)}
onBlur={() => handleBlur()}
>
<NumberInputField fontWeight="700" />
<NumberInputField
fontWeight="700"
onKeyUp={(e) => handleKeyUp(e)}
/>
</NumberInput>
<Text>of {numPages}</Text>
</Flex>
Expand Down

0 comments on commit b4f0f77

Please sign in to comment.