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

fetch rewards on every submit to avoid stale values #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/components/UserInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function UserInput() {
const [state, setState] = useState("initial");
const [isLoaded, setIsLoaded] = useState(false);
const [error, setError] = useState("");
const [usersReward] = useFetch();
const [usersReward, setUsersReward] = useState([]);
let [userAprInfo, setUserAprInfo] = useState(null);

async function getUserInfo(userAddr) {
Expand All @@ -49,8 +49,11 @@ export function UserInput() {
setError(false);
setState("submitting");

getUserInfo(ethAddress)
.then((info) => {
useFetch().then(r => {
setUsersReward(r);
return getUserInfo(ethAddress);
})
.then(info => {
setState("success");
setIsLoaded(true);
setUserAprInfo({ ...info });
Expand Down
23 changes: 4 additions & 19 deletions src/utils/useFetch.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { useEffect, useState } from "react";
import { rewardsUrl } from "./addresses";

function useFetch(url = rewardsUrl) {
const [response, setResponse] = useState(null);
const [error, setError] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const res = await fetch(url);

const allRewards = await res.json();
setResponse(allRewards.rewards);
} catch (error) {
setError(error);
}
};
fetchData();
}, []);

return [response, error];
async function useFetch(url = rewardsUrl) {
const res = await fetch(url);
const allRewards = await res.json();
return allRewards.rewards;
}

export default useFetch;