From 5bb1ed18c439e923e910c9702830a0ebd957025d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczyk?= Date: Fri, 25 Mar 2022 13:39:57 +0100 Subject: [PATCH] chore(dashboard-v2): handle reloadUser error --- .../components/forms/AccountSettingsForm.js | 16 +++++++----- .../dashboard-v2/src/pages/settings/index.js | 26 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/dashboard-v2/src/components/forms/AccountSettingsForm.js b/packages/dashboard-v2/src/components/forms/AccountSettingsForm.js index ad29b85c..2c382c1b 100644 --- a/packages/dashboard-v2/src/components/forms/AccountSettingsForm.js +++ b/packages/dashboard-v2/src/components/forms/AccountSettingsForm.js @@ -37,15 +37,17 @@ export const AccountSettingsForm = ({ user, onSuccess, onFailure }) => { validationSchema={emailUpdateSchema} onSubmit={async ({ email, password }, { resetForm }) => { try { - await accountsService.put("user", { - json: { - email: email || undefined, - password: password || undefined, - }, - }); + const user = await accountsService + .put("user", { + json: { + email: email || undefined, + password: password || undefined, + }, + }) + .json(); resetForm(); - onSuccess(); + await onSuccess(user); } catch { onFailure(); } diff --git a/packages/dashboard-v2/src/pages/settings/index.js b/packages/dashboard-v2/src/pages/settings/index.js index ad7a34d8..358a4b28 100644 --- a/packages/dashboard-v2/src/pages/settings/index.js +++ b/packages/dashboard-v2/src/pages/settings/index.js @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useCallback, useState } from "react"; import { navigate } from "gatsby"; import { useUser } from "../../contexts/user"; @@ -22,6 +22,21 @@ const AccountPage = () => { const prompt = () => setRemovalInitiated(true); 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 ( <>
@@ -39,14 +54,7 @@ const AccountPage = () => { There was an error processing your request. Please try again later. )} {state === State.Success && Changes saved successfully.} - { - await reloadUser(); - setState(State.Success); - }} - onFailure={() => setState(State.Failure)} - /> + setState(State.Failure)} />