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

Throttle API calls #150

Merged
merged 2 commits into from
Apr 11, 2024
Merged

Throttle API calls #150

merged 2 commits into from
Apr 11, 2024

Conversation

nickbar01234
Copy link
Collaborator

Description

Add throttling to prevent multiple API calls.

Issues

Resolve #147.

Copy link

vercel bot commented Apr 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
the-legacy-project2 🔄 Building (Inspect) Visit Preview Apr 10, 2024 3:01am


type MembersHomePageProps = {
members: User[];
};

const EBOARD_POSITIONS = [
"Social Coordinator",
"Senior Outreach Coordinator",
"Outreach Coordinator",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Text is too long

</div>
{dropdownComponent}
{student ? (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved the styling around a bit, since it looks weird for long text.

if (res.code === "SUCCESS") {
router.refresh();
} else {
alert("Please refresh the page and try again");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we opt out of the alert when the request encounters an an error, maybe it's better to keep so that users get that feedback?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I was lazy to handle the cases. But you're probably right that we should handle errors.

Copy link
Collaborator

@johnny-t06 johnny-t06 left a comment

Choose a reason for hiding this comment

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

LGTM, tested all the pages with the new Throttle hook. Spinner looks good!

callback: (res) => {
if (res.code === "SUCCESS") {
setShowAddFilePopUp(false);
router.refresh();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed that not all the states are reset, but I tested the AddFile component by completing and cancelling the form and it works well currently.

Copy link

@jason-shoe jason-shoe left a comment

Choose a reason for hiding this comment

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

throttled callback fns typically also come with some duration in milliseconds if you wanna do that as well :)

return;
}
setFetching(true);
await props.fn(...args).then(props.callback);

Choose a reason for hiding this comment

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

can callback be combined into fn? that way you're just passing in a single function into useApiThrottle

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed with @johnny-t06. We think having callback separately make it nice to use the hooks directly using a client-side API without the need to wrap it.

await props.fn(...args).then(props.callback);
setFetching(false);
},
[props, fetching]

Choose a reason for hiding this comment

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

would recommend destructuring props immediately in this hook! looks like the consumer of useApiThrottle is actually creating a new props object on each render, so memoization here isn't actually doing anything

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it, thanks for the catch!

* Prevent additional API call until the most recent one has completed.
*/
const useApiThrottle = <T extends ApiCall>(props: UseApiThrottleProps<T>) => {
const [fetching, setFetching] = React.useState(false);

Choose a reason for hiding this comment

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

could also useRef here in case the callback gets called twice before state updates

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is useRef safe we use current to display the loading indicator?

@nickbar01234
Copy link
Collaborator Author

nickbar01234 commented Apr 11, 2024

throttled callback fns typically also come with some duration in milliseconds if you wanna do that as well :)

Ah got it. I think we will stick with the current options for now, given our use case! Thank you for the suggestion!!

Base automatically changed from nickbar01234/deprecate-uid to main April 11, 2024 04:38
@nickbar01234
Copy link
Collaborator Author

Merging in ahead for client meeting!

@nickbar01234 nickbar01234 merged commit 9a1d274 into main Apr 11, 2024
3 of 4 checks passed
@nickbar01234 nickbar01234 deleted the nickbar01234/throttle-api branch April 11, 2024 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Multiple Form Submission
3 participants