From 21da49f79df942ca7c748bba1e4333cff59d731e Mon Sep 17 00:00:00 2001 From: "kasia.oczkowska" Date: Thu, 17 Oct 2024 08:37:21 +0100 Subject: [PATCH] feat: add error snackbar for errors originating from registerUser function --- .../src/components/SignUpForm/index.ee.jsx | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/web/src/components/SignUpForm/index.ee.jsx b/packages/web/src/components/SignUpForm/index.ee.jsx index 33ab7ab8..e931d394 100644 --- a/packages/web/src/components/SignUpForm/index.ee.jsx +++ b/packages/web/src/components/SignUpForm/index.ee.jsx @@ -53,15 +53,13 @@ function SignUpForm() { }, [authentication.isAuthenticated]); const handleSubmit = async (values) => { - const { fullName, email, password } = values; - - await registerUser({ - fullName, - email, - password, - }); - try { + const { fullName, email, password } = values; + await registerUser({ + fullName, + email, + password, + }); const { data } = await createAccessToken({ email, password, @@ -69,9 +67,27 @@ function SignUpForm() { const { token } = data; authentication.updateToken(token); } catch (error) { - enqueueSnackbar(error?.message || formatMessage('signupForm.error'), { - variant: 'error', - }); + const errors = error?.response?.data?.errors + ? Object.values(error.response.data.errors) + : []; + + if (errors.length) { + for (const [error] of errors) { + enqueueSnackbar(error, { + variant: 'error', + SnackbarProps: { + 'data-test': 'snackbar-sign-up-error', + }, + }); + } + } else { + enqueueSnackbar(error?.message || formatMessage('signupForm.error'), { + variant: 'error', + SnackbarProps: { + 'data-test': 'snackbar-sign-up-error', + }, + }); + } } };