update account form for email login
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import Link from 'next/link';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { ExternalLink } from '@/public/assets/external-link';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
@@ -21,9 +24,10 @@ import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { UpdateAccountSchemaClient } from '../_lib/schemas/update-account.schema';
|
||||
import { onUpdateAccount } from '../_lib/server/update-account';
|
||||
import { z } from 'zod';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import { pathsConfig } from '@/packages/shared/src/config';
|
||||
|
||||
type UpdateAccountFormValues = z.infer<typeof UpdateAccountSchemaClient>;
|
||||
type UpdateAccountFormValues = z.infer<ReturnType<typeof UpdateAccountSchemaClient>>;
|
||||
|
||||
export function UpdateAccountForm({
|
||||
defaultValues,
|
||||
@@ -32,33 +36,56 @@ export function UpdateAccountForm({
|
||||
defaultValues: UpdateAccountFormValues,
|
||||
isEmailUser: boolean,
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const { t } = useTranslation('account');
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(UpdateAccountSchemaClient),
|
||||
resolver: zodResolver(UpdateAccountSchemaClient({ isEmailUser })),
|
||||
mode: 'onChange',
|
||||
defaultValues,
|
||||
});
|
||||
|
||||
const { firstName, lastName, personalCode, email, weight, height, userConsent } = defaultValues;
|
||||
const { firstName, lastName, personalCode, email, userConsent } = defaultValues;
|
||||
|
||||
const defaultValues_weight = "weight" in defaultValues ? defaultValues.weight : null;
|
||||
const defaultValues_height = "height" in defaultValues ? defaultValues.height : null;
|
||||
|
||||
const hasFirstName = !!firstName;
|
||||
const hasLastName = !!lastName;
|
||||
const hasPersonalCode = !!personalCode;
|
||||
const hasEmail = !!email;
|
||||
const hasWeight = !!weight;
|
||||
const hasHeight = !!height;
|
||||
|
||||
const onUpdateAccountOptions = async (values: UpdateAccountFormValues) =>
|
||||
onUpdateAccount({
|
||||
firstName: hasFirstName ? firstName : values.firstName,
|
||||
lastName: hasLastName ? lastName : values.lastName,
|
||||
personalCode: hasPersonalCode ? personalCode : values.personalCode,
|
||||
email: hasEmail ? email : values.email,
|
||||
phone: values.phone,
|
||||
weight: (hasWeight ? weight : values.weight) as number,
|
||||
height: (hasHeight ? height : values.height) as number,
|
||||
userConsent: values.userConsent ?? userConsent,
|
||||
city: values.city,
|
||||
});
|
||||
const onUpdateAccountOptions = async (values: UpdateAccountFormValues) => {
|
||||
const loading = toast.loading(t('updateAccount.updateAccountLoading'));
|
||||
try {
|
||||
const response = await onUpdateAccount({
|
||||
firstName: hasFirstName ? firstName : values.firstName,
|
||||
lastName: hasLastName ? lastName : values.lastName,
|
||||
personalCode: hasPersonalCode ? personalCode : values.personalCode,
|
||||
email: hasEmail ? email : values.email,
|
||||
phone: values.phone,
|
||||
weight: ((("weight" in values && values.weight) ?? defaultValues_weight) || null) as number,
|
||||
height: ((("height" in values && values.height) ?? defaultValues_height) || null) as number,
|
||||
userConsent: values.userConsent ?? userConsent,
|
||||
city: values.city,
|
||||
});
|
||||
if (!response) {
|
||||
throw new Error('Failed to update account');
|
||||
}
|
||||
toast.dismiss(loading);
|
||||
toast.success(t('updateAccount.updateAccountSuccess'));
|
||||
|
||||
if (response.hasUnseenMembershipConfirmation) {
|
||||
router.push(pathsConfig.auth.membershipConfirmation);
|
||||
} else {
|
||||
router.push(pathsConfig.app.selectPackage);
|
||||
}
|
||||
} catch (error) {
|
||||
console.info("promiseresult error", error);
|
||||
toast.error(t('updateAccount.updateAccountError'));
|
||||
toast.dismiss(loading);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
|
||||
Reference in New Issue
Block a user