feat(profile): combine forms

This commit is contained in:
Rıdvan Akca
2023-04-05 15:07:28 +03:00
parent 2791dca412
commit 2840fce856
2 changed files with 18 additions and 128 deletions

View File

@@ -84,15 +84,11 @@
"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.fullName": "Full name",
"profileSettings.updateFullName": "Update full name",
"profileSettings.email": "Email", "profileSettings.email": "Email",
"profileSettings.updateEmail": "Update email", "profileSettings.updateProfile": "Update your profile",
"profileSettings.updatedFullName": "Your full name has been updated.", "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...",

View File

@@ -19,24 +19,13 @@ 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 fullNameValidationSchema = yup const validationSchema = yup
.object({ .object({
fullName: yup.string().required(), fullName: yup.string().required(),
})
.required();
const emailValidationSchema = yup
.object({
email: yup.string().email().required(), email: yup.string().email().required(),
}) password: yup.string(),
.required();
const passwordValidationSchema = yup
.object({
password: yup.string().required(),
confirmPassword: yup confirmPassword: yup
.string() .string()
.required()
.oneOf([yup.ref('password')], 'Passwords must match'), .oneOf([yup.ref('password')], 'Passwords must match'),
}) })
.required(); .required();
@@ -49,71 +38,29 @@ 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 handleFullNameUpdate = async (data: any) => { const handleProfileSettingsUpdate = async (data: any) => {
const fullName = data.fullName; const { fullName , password, email } = data;
await updateUser({ await updateUser({
variables: { variables: {
input: { input: {
fullName, fullName,
},
},
});
enqueueSnackbar(formatMessage('profileSettings.updatedFullName'), {
variant: 'success',
});
};
const handleEmailUpdate = async (data: any) => {
const email = data.email;
await updateUser({
variables: {
input: {
email,
},
},
optimisticResponse: {
updateUser: {
__typename: 'User',
email,
},
},
});
enqueueSnackbar(formatMessage('profileSettings.updatedEmail'), {
variant: 'success',
});
};
const handlePasswordUpdate = async (data: any) => {
const password = data.password;
setPasswordDefaultValues({
password,
confirmPassword: data.confirmPassword,
});
await updateUser({
variables: {
input: {
password, password,
email,
}, },
}, },
}); });
enqueueSnackbar(formatMessage('profileSettings.updatedPassword'), { enqueueSnackbar(formatMessage('profileSettings.updatedProfile'), {
variant: 'success', variant: 'success',
}); });
}; };
return ( return (
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}> <Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
<Grid container item xs={12} sm={9} md={8} lg={6}> <Grid container item xs={12} sm={9} md={8} lg={6}>
@@ -123,9 +70,9 @@ function ProfileSettings() {
<Grid item xs={12} justifyContent="flex-end"> <Grid item xs={12} justifyContent="flex-end">
<StyledForm <StyledForm
defaultValues={{ ...currentUser }} defaultValues={{ ...currentUser, password: '', confirmPassword: '' }}
onSubmit={handleFullNameUpdate} onSubmit={handleProfileSettingsUpdate}
resolver={yupResolver(fullNameValidationSchema)} resolver={yupResolver(validationSchema)}
mode="onChange" mode="onChange"
sx={{ mb: 2 }} sx={{ mb: 2 }}
render={({ render={({
@@ -142,82 +89,29 @@ function ProfileSettings() {
fullWidth fullWidth
name="fullName" name="fullName"
label={formatMessage('profileSettings.fullName')} label={formatMessage('profileSettings.fullName')}
margin="normal" margin='dense'
error={touchedFields.fullName && !!errors?.fullName} error={touchedFields.fullName && !!errors?.fullName}
helperText={errors?.fullName?.message || ' '} helperText={errors?.fullName?.message || ' '}
/> />
<Button
variant="contained"
type="submit"
disabled={!isDirty || !isValid || isSubmitting}
>
{formatMessage('profileSettings.updateFullName')}
</Button>
</>
)}
/>
<StyledForm
defaultValues={{ ...currentUser}}
onSubmit={handleEmailUpdate}
resolver={yupResolver(emailValidationSchema)}
mode="onChange"
sx={{ mb: 2 }}
render={({
formState: {
errors,
touchedFields,
isDirty,
isValid,
isSubmitting,
},
}) => (
<>
<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) || ' '
} }
/> />
@@ -225,7 +119,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
@@ -242,7 +136,7 @@ function ProfileSettings() {
type="submit" type="submit"
disabled={!isDirty || !isValid || isSubmitting} disabled={!isDirty || !isValid || isSubmitting}
> >
{formatMessage('profileSettings.updatePassword')} {formatMessage('profileSettings.updateProfile')}
</Button> </Button>
</> </>
)} )}