Merge pull request #1036 from automatisch/update-full-name-in-profile-page

feat(ProfileSettings): add capability to update user full name
This commit is contained in:
Ali BARIN
2023-04-06 15:37:51 +02:00
committed by GitHub
6 changed files with 50 additions and 78 deletions

View File

@@ -4,6 +4,7 @@ type Params = {
input: { input: {
email: string; email: string;
password: string; password: string;
fullName: string;
}; };
}; };
@@ -15,6 +16,7 @@ const updateUser = async (
const user = await context.currentUser.$query().patchAndFetch({ const user = await context.currentUser.$query().patchAndFetch({
email: params.input.email, email: params.input.email,
password: params.input.password, password: params.input.password,
fullName: params.input.fullName,
}); });
return user; return user;

View File

@@ -351,6 +351,7 @@ input CreateUserInput {
input UpdateUserInput { input UpdateUserInput {
email: String email: String
password: String password: String
fullName: String
} }
input ForgotPasswordInput { input ForgotPasswordInput {

View File

@@ -4,8 +4,8 @@ export const UPDATE_USER = gql`
mutation UpdateUser($input: UpdateUserInput) { mutation UpdateUser($input: UpdateUserInput) {
updateUser(input: $input) { updateUser(input: $input) {
id id
fullName
email email
updatedAt
} }
} }
`; `;

View File

@@ -6,8 +6,6 @@ export const GET_CURRENT_USER = gql`
id id
fullName fullName
email email
createdAt
updatedAt
} }
} }
`; `;

View File

@@ -83,13 +83,12 @@
"execution.noDataTitle": "No data", "execution.noDataTitle": "No data",
"execution.noDataMessage": "We successfully ran the execution, but there was no new data to process.", "execution.noDataMessage": "We successfully ran the execution, but there was no new data to process.",
"profileSettings.title": "My Profile", "profileSettings.title": "My Profile",
"profileSettings.fullName": "Full name",
"profileSettings.email": "Email", "profileSettings.email": "Email",
"profileSettings.updateEmail": "Update email", "profileSettings.updateProfile": "Update your profile",
"profileSettings.updatedProfile": "Your profile has been updated.",
"profileSettings.newPassword": "New password", "profileSettings.newPassword": "New password",
"profileSettings.confirmNewPassword": "Confirm new password", "profileSettings.confirmNewPassword": "Confirm new password",
"profileSettings.updatedEmail": "Your email has been updated.",
"profileSettings.updatedPassword": "Your password has been updated.",
"profileSettings.updatePassword": "Update password",
"profileSettings.deleteMyAccount": "Delete my account", "profileSettings.deleteMyAccount": "Delete my account",
"profileSettings.deleteAccount": "Delete account", "profileSettings.deleteAccount": "Delete account",
"profileSettings.deleteAccountSubtitle": "This will permanently delete...", "profileSettings.deleteAccountSubtitle": "This will permanently delete...",
@@ -154,4 +153,4 @@
"invoices.amount": "Amount", "invoices.amount": "Amount",
"invoices.invoice": "Invoice", "invoices.invoice": "Invoice",
"invoices.link": "Link" "invoices.link": "Link"
} }

View File

@@ -19,18 +19,19 @@ import { UPDATE_USER } from 'graphql/mutations/update-user';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import useCurrentUser from 'hooks/useCurrentUser'; import useCurrentUser from 'hooks/useCurrentUser';
const emailValidationSchema = yup type TMutationInput = {
.object({ fullName: string;
email: yup.string().email().required(), email: string;
}) password?: string;
.required(); }
const passwordValidationSchema = yup const validationSchema = yup
.object({ .object({
password: yup.string().required(), fullName: yup.string().required(),
email: yup.string().email().required(),
password: yup.string(),
confirmPassword: yup confirmPassword: yup
.string() .string()
.required()
.oneOf([yup.ref('password')], 'Passwords must match'), .oneOf([yup.ref('password')], 'Passwords must match'),
}) })
.required(); .required();
@@ -43,54 +44,42 @@ const StyledForm = styled(Form)`
function ProfileSettings() { function ProfileSettings() {
const [showDeleteAccountConfirmation, setShowDeleteAccountConfirmation] = React.useState(false); const [showDeleteAccountConfirmation, setShowDeleteAccountConfirmation] = React.useState(false);
const [passwordDefaultValues, setPasswordDefaultValues] = React.useState({});
const { enqueueSnackbar } = useSnackbar(); const { enqueueSnackbar } = useSnackbar();
const currentUser = useCurrentUser(); const currentUser = useCurrentUser();
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [updateUser] = useMutation(UPDATE_USER); const [updateUser] = useMutation(UPDATE_USER);
const handleEmailUpdate = async (data: any) => { const handleProfileSettingsUpdate = async (data: any) => {
const email = data.email; const { fullName, password, email } = data;
const mutationInput: TMutationInput = {
fullName,
email,
}
if (password) {
mutationInput.password = password;
}
await updateUser({ await updateUser({
variables: { variables: {
input: { input: mutationInput,
email,
},
}, },
optimisticResponse: { optimisticResponse: {
updateUser: { updateUser: {
__typename: 'User', __typename: 'User',
id: currentUser.id,
fullName,
email, email,
}, },
}, },
}); });
enqueueSnackbar(formatMessage('profileSettings.updatedEmail'), { enqueueSnackbar(formatMessage('profileSettings.updatedProfile'), {
variant: 'success', variant: 'success',
}); });
}; };
const handlePasswordUpdate = async (data: any) => {
const password = data.password;
setPasswordDefaultValues({
password,
confirmPassword: data.confirmPassword,
});
await updateUser({
variables: {
input: {
password,
},
},
});
enqueueSnackbar(formatMessage('profileSettings.updatedPassword'), {
variant: 'success',
});
};
return ( return (
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}> <Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
@@ -100,10 +89,10 @@ function ProfileSettings() {
</Grid> </Grid>
<Grid item xs={12} justifyContent="flex-end"> <Grid item xs={12} justifyContent="flex-end">
<StyledForm <StyledForm
defaultValues={{ ...currentUser}} defaultValues={{ ...currentUser, password: '', confirmPassword: '' }}
onSubmit={handleEmailUpdate} onSubmit={handleProfileSettingsUpdate}
resolver={yupResolver(emailValidationSchema)} resolver={yupResolver(validationSchema)}
mode="onChange" mode="onChange"
sx={{ mb: 2 }} sx={{ mb: 2 }}
render={({ render={({
@@ -116,50 +105,33 @@ function ProfileSettings() {
}, },
}) => ( }) => (
<> <>
<TextField
fullWidth
name="fullName"
label={formatMessage('profileSettings.fullName')}
margin='dense'
error={touchedFields.fullName && !!errors?.fullName}
helperText={errors?.fullName?.message || ' '}
/>
<TextField <TextField
fullWidth fullWidth
name="email" name="email"
label={formatMessage('profileSettings.email')} label={formatMessage('profileSettings.email')}
margin="normal" margin='dense'
error={touchedFields.email && !!errors?.email} error={touchedFields.email && !!errors?.email}
helperText={errors?.email?.message || ' '} helperText={errors?.email?.message || ' '}
/> />
<Button
variant="contained"
type="submit"
disabled={!isDirty || !isValid || isSubmitting}
>
{formatMessage('profileSettings.updateEmail')}
</Button>
</>
)}
/>
<StyledForm
defaultValues={passwordDefaultValues}
onSubmit={handlePasswordUpdate}
resolver={yupResolver(passwordValidationSchema)}
mode="onChange"
render={({
formState: {
errors,
touchedFields,
isDirty,
isValid,
isSubmitting,
},
}) => (
<>
<TextField <TextField
fullWidth fullWidth
name="password" name="password"
label={formatMessage('profileSettings.newPassword')} label={formatMessage('profileSettings.newPassword')}
margin="normal" margin='dense'
type="password" type="password"
error={touchedFields.password && !!errors?.password} error={touchedFields.password && !!errors?.password}
helperText={ helperText={
(touchedFields.password && errors?.password?.message) || '' (touchedFields.password && errors?.password?.message) || ' '
} }
/> />
@@ -167,7 +139,7 @@ function ProfileSettings() {
fullWidth fullWidth
name="confirmPassword" name="confirmPassword"
label={formatMessage('profileSettings.confirmNewPassword')} label={formatMessage('profileSettings.confirmNewPassword')}
margin="normal" margin='dense'
type="password" type="password"
error={ error={
touchedFields.confirmPassword && !!errors?.confirmPassword touchedFields.confirmPassword && !!errors?.confirmPassword
@@ -184,7 +156,7 @@ function ProfileSettings() {
type="submit" type="submit"
disabled={!isDirty || !isValid || isSubmitting} disabled={!isDirty || !isValid || isSubmitting}
> >
{formatMessage('profileSettings.updatePassword')} {formatMessage('profileSettings.updateProfile')}
</Button> </Button>
</> </>
)} )}