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

[FIX] - Pagination Component redundant fetches #115

Merged
merged 9 commits into from
Jun 24, 2023
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>) => {
kevin-pierce marked this conversation as resolved.
Show resolved Hide resolved
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