Created
April 17, 2023 09:23
-
-
Save alexanderbluhm/009b1663567afa52350a52c2b22c11b2 to your computer and use it in GitHub Desktop.
proud code sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const upload = async (e) => { | |
e.preventDefault(); | |
setLoading(true) | |
// ... | |
try { | |
const [res] = await Promise.allSettled([ | |
fetch(`${process.env.REACT_APP_API_URL}/upload`, { | |
method: "POST", | |
body: data, | |
credentials: "include", | |
headers: { | |
"X-CSRF-TOKEN": Cookies.get("csrf_access_token"), | |
}, | |
}), | |
new Promise((resolve) => setTimeout(resolve, 900)), | |
]); | |
if (res.value?.ok) { | |
const body = await res.value?.json(); | |
onNext(body); | |
} else if (res.value?.status === 401) { | |
navigate("/login"); | |
} else { | |
const body = await res.value?.json(); | |
setError(body.message || "An unexpected error occured. Please try again later or reach out to a contact person."); | |
} | |
} catch (error) { | |
setError("An unexpected error occured. Please try again later or reach out to a contact person."); | |
} | |
setLoading(false); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment