chore(dashboard-v2): handle reloadUser error

This commit is contained in:
Michał Leszczyk 2022-03-25 13:39:57 +01:00
parent 2aa3437ab6
commit 5bb1ed18c4
No known key found for this signature in database
GPG Key ID: FA123CA8BAA2FBF4
2 changed files with 26 additions and 16 deletions

View File

@ -37,15 +37,17 @@ export const AccountSettingsForm = ({ user, onSuccess, onFailure }) => {
validationSchema={emailUpdateSchema} validationSchema={emailUpdateSchema}
onSubmit={async ({ email, password }, { resetForm }) => { onSubmit={async ({ email, password }, { resetForm }) => {
try { try {
await accountsService.put("user", { const user = await accountsService
.put("user", {
json: { json: {
email: email || undefined, email: email || undefined,
password: password || undefined, password: password || undefined,
}, },
}); })
.json();
resetForm(); resetForm();
onSuccess(); await onSuccess(user);
} catch { } catch {
onFailure(); onFailure();
} }

View File

@ -1,4 +1,4 @@
import { useState } from "react"; import { useCallback, useState } from "react";
import { navigate } from "gatsby"; import { navigate } from "gatsby";
import { useUser } from "../../contexts/user"; import { useUser } from "../../contexts/user";
@ -22,6 +22,21 @@ const AccountPage = () => {
const prompt = () => setRemovalInitiated(true); const prompt = () => setRemovalInitiated(true);
const abort = () => setRemovalInitiated(false); const abort = () => setRemovalInitiated(false);
const onSettingsUpdated = useCallback(
async (updatedState) => {
try {
// Update state locally and request revalidation.
await reloadUser(updatedState);
} finally {
// If revalidation fails, we can't really do much. Request
// will be auto-retried by SWR, so we'll just show a message
// about the update request being successful.
setState(State.Success);
}
},
[reloadUser]
);
return ( return (
<> <>
<div className="flex flex-col xl:flex-row"> <div className="flex flex-col xl:flex-row">
@ -39,14 +54,7 @@ const AccountPage = () => {
<Alert $variant="error">There was an error processing your request. Please try again later.</Alert> <Alert $variant="error">There was an error processing your request. Please try again later.</Alert>
)} )}
{state === State.Success && <Alert $variant="success">Changes saved successfully.</Alert>} {state === State.Success && <Alert $variant="success">Changes saved successfully.</Alert>}
<AccountSettingsForm <AccountSettingsForm user={user} onSuccess={onSettingsUpdated} onFailure={() => setState(State.Failure)} />
user={user}
onSuccess={async () => {
await reloadUser();
setState(State.Success);
}}
onFailure={() => setState(State.Failure)}
/>
</section> </section>
<hr /> <hr />
<section> <section>