From fc870c2181dbce1b76e341328d137b39f53e5468 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 4 Nov 2024 15:04:53 +0100 Subject: [PATCH] Add profile display name in account section --- pages/account/info.tsx | 116 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/pages/account/info.tsx b/pages/account/info.tsx index 5f6d5fcf..618f102e 100644 --- a/pages/account/info.tsx +++ b/pages/account/info.tsx @@ -1,10 +1,13 @@ +import { useMutation } from '@tanstack/react-query'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { FunctionComponent, useEffect } from 'react'; +import { useForm } from 'react-hook-form'; import AccountHeader from '../../components/account-header'; import AccountMenu from '../../components/account-menu'; import LoadingPage from '../../components/loading-page'; import Meta from '../../components/meta'; +import Spinner from '../../components/spinner'; import { planNames } from '../../constants/plans'; import Layout from '../../layout/layout'; import { useAuth } from '../../utils/auth'; @@ -22,9 +25,18 @@ const providerNames = { }; const AccountInfo: FunctionComponent = function () { - const { isAuth, user, isLoading: isAuthLoading } = useAuth(); + const { isAuth, user, isLoading: isAuthLoading, getIdToken } = useAuth(); const router = useRouter(); const { data: userData } = useCurrentUser(); + const { + register: registerFormField, + getValues, + handleSubmit, + reset, + setError, + clearErrors, + formState: { isSubmitting, isSubmitSuccessful, errors } + } = useForm(); useEffect(() => { if (!isAuthLoading) { @@ -36,6 +48,49 @@ const AccountInfo: FunctionComponent = function () { } }, [isAuthLoading, user, isAuth]); + useEffect(() => { + if (userData) { + reset({ displayName: userData?.['displayName'] }); + } + }, [userData]); + + const { + mutate: updateProfile, + isError: isUpdateProfileError, + isPending: isUpdatingProfile + } = useMutation({ + mutationFn: async () => { + clearErrors(); + + const response = await fetch( + `${process.env.NEXT_PUBLIC_BACKEND_URL}/user/profile`, + { + body: JSON.stringify(getValues()), + method: 'PUT', + headers: { + Authorization: `Bearer ${await getIdToken()}`, + 'Content-Type': 'application/json' + } + } + ); + + if (response.status !== 204) { + const data = await response.json(); + + throw new Error(data.message); + } + }, + onError: (error) => { + setError('root.error', { + message: 'Something went wrong', + type: 'custom' + }); + }, + onSuccess: (data) => { + clearErrors(); + } + }); + return ( @@ -114,6 +169,65 @@ const AccountInfo: FunctionComponent = function () { +
+
+
+
+

Profile

+ + The display name will be used in the desktop + application when using team collaboration + +
+
+
+
+
{ + clearErrors(); + handleSubmit(() => { + updateProfile(); + })(e); + }} + > +
+ + +
+ +
+ + {isSubmitting && } + {!isUpdatingProfile && errors.root?.error && ( +
+ Something went wrong, please try again later +
+ )} + {!isUpdatingProfile && + isSubmitSuccessful && + !isUpdateProfileError && ( +
+ Profile updated +
+ )} +
+
+
+
+