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

Conversation

kevin-pierce
Copy link
Contributor

@kevin-pierce kevin-pierce commented Jun 21, 2023

GitHub Issue link

Pagination Component - Prevent redundant / unintentional fetches

Implementation description

  • Created fetchRecords function to actually perform the get
  • Added onKeyUp event handler to trigger fetch
  • Modified handleNumberInputChange function to only update the input value

Steps to test

  1. Seed log records (sufficient number such that we end up with some # of pages > 10)
  2. Verify that changing the page number (without clicking outside of the text field / hitting enter) will NOT cause a refetch of log records
  3. Verify that changing the page number and unfocusing the text field (whether through tab or clicking outside of it) will refetch log records
  4. Verify that changing the page number and hitting enter will refetch the log records
  5. Verify that hitting enter OR unfocusing the text field without changing the page number will NOT refetch log records

What should reviewers focus on?

  • The structure / call stack (might be a little confusing / messy) + there was some discussion around making this a reusable component in the future ; perhaps we might want to restructure the component within this PR itself?

Checklist

  • My PR name is descriptive and in imperative tense
  • My commit messages are descriptive and in imperative tense. My commits are atomic and trivial commits are squashed or fixup'd into non-trivial commits
  • I have run the appropriate linter(s)
  • I have requested a review from the PL, as well as other devs who have background knowledge on this PR or who will be building on top of this PR
  • If I have made API changes, I have updated the REST API Docs
  • IF I have made changes to the db/models, I have updated the Data Models Page
  • I have updated other Docs as needed

@kevin-pierce kevin-pierce marked this pull request as ready for review June 21, 2023 05:35
@kevin-pierce kevin-pierce changed the title Implemented fix for pagination component [FIX] - Pagination Component redundant fetches Jun 21, 2023
@kevin-pierce kevin-pierce added front-end involves front-end work fix involves fixing old code Homepage Features related to the homepage labels Jun 21, 2023
Copy link
Collaborator

@connor-bechthold connor-bechthold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's crazy how much better of a user experience this is 🧑‍🍳

Couple of things:

  • I think the flow would be even better if when you click enter, you're deselected from the box
  • Not sure if this is out of the scope of this ticket, but do we also want to consider disabling the inputs while the records are being fetched? cc @Safewaan

frontend/src/components/common/Pagination.tsx Show resolved Hide resolved
Comment on lines 55 to 75
// 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);
}
}

const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
// 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 = () => {
if (Number.isNaN(userPageNum)) {
setUserPageNum(pageNum);
return;
}
fetchRecords()
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This control flow is a little weird, so I thought I'd comment it to explain:

Two ways to trigger a fetch:

  • Exiting focus from the textbox (Clicking outside of it) ; handleBlur
  • Hitting the enter key ; handleKeyUp

handleBlur:

  • Checks if number is present ; if not, reset to the previous number and terminate
  • If a valid number is present, initiate a fetch

handleKeyUp:

  • Checks if key-up (triggered once a user "stops" pressing a key) is == 13 (the key code for the ENTER key) ; if true, then this will force a blur event (triggering the handleBlur function)

@Safewaan
Copy link
Collaborator

* Not sure if this is out of the scope of this ticket, but do we also want to consider disabling the inputs while the records are being fetched? cc @Safewaan

Let's see what the performance of pagination looks like after the improvements and discuss with the designers

Copy link
Collaborator

@Safewaan Safewaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM THIS IS 🔥 (fire)

Copy link
Collaborator

@connor-bechthold connor-bechthold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sickkkkk lets goo 👨‍🍳

@kevin-pierce kevin-pierce merged commit b4f0f77 into main Jun 24, 2023
1 check passed
@kevin-pierce kevin-pierce deleted the pagination-onblur-fix branch June 24, 2023 15:47
@Safewaan Safewaan linked an issue Jul 12, 2023 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix involves fixing old code front-end involves front-end work Homepage Features related to the homepage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pagination Component - Prevent redundant / unintentional fetches
3 participants