show less update-account fields on email login

This commit is contained in:
2025-09-10 06:33:56 +03:00
parent 312027b9ed
commit e3cdba6a7c
4 changed files with 127 additions and 90 deletions

View File

@@ -19,19 +19,21 @@ import {
import { Input } from '@kit/ui/input';
import { Trans } from '@kit/ui/trans';
import { UpdateAccountSchema } from '../_lib/schemas/update-account.schema';
import { UpdateAccountSchemaClient } from '../_lib/schemas/update-account.schema';
import { onUpdateAccount } from '../_lib/server/update-account';
import { z } from 'zod';
type UpdateAccountFormValues = z.infer<typeof UpdateAccountSchema>;
type UpdateAccountFormValues = z.infer<typeof UpdateAccountSchemaClient>;
export function UpdateAccountForm({
defaultValues,
isEmailUser,
}: {
defaultValues: UpdateAccountFormValues,
isEmailUser: boolean,
}) {
const form = useForm({
resolver: zodResolver(UpdateAccountSchema),
resolver: zodResolver(UpdateAccountSchemaClient),
mode: 'onChange',
defaultValues,
});
@@ -44,18 +46,18 @@ export function UpdateAccountForm({
const hasEmail = !!email;
const hasWeight = !!weight;
const hasHeight = !!height;
const hasUserConsent = !!userConsent;
const onUpdateAccountOptions = async (values: UpdateAccountFormValues) =>
onUpdateAccount({
...values,
...(hasFirstName && { firstName }),
...(hasLastName && { lastName }),
...(hasPersonalCode && { personalCode }),
...(hasEmail && { email }),
...(hasWeight && { weight: values.weight ?? weight }),
...(hasHeight && { height: values.height ?? height }),
...(hasUserConsent && { userConsent: values.userConsent ?? userConsent }),
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,
});
return (
@@ -66,14 +68,14 @@ export function UpdateAccountForm({
>
<FormField
name="firstName"
disabled={hasFirstName}
disabled={hasFirstName && !isEmailUser}
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:firstName'} />
</FormLabel>
<FormControl>
<Input {...field} />
<Input {...field} autoFocus={!hasFirstName} />
</FormControl>
<FormMessage />
</FormItem>
@@ -82,14 +84,14 @@ export function UpdateAccountForm({
<FormField
name="lastName"
disabled={hasLastName}
disabled={hasLastName && !isEmailUser}
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:lastName'} />
</FormLabel>
<FormControl>
<Input {...field} />
<Input {...field} autoFocus={hasFirstName && !hasLastName} />
</FormControl>
<FormMessage />
</FormItem>
@@ -98,7 +100,7 @@ export function UpdateAccountForm({
<FormField
name="personalCode"
disabled={hasPersonalCode}
disabled={hasPersonalCode && !isEmailUser}
render={({ field }) => (
<FormItem>
<FormLabel>
@@ -143,72 +145,76 @@ export function UpdateAccountForm({
)}
/>
<FormField
name="city"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:city'} />
</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{!isEmailUser && (
<>
<FormField
name="city"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:city'} />
</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex flex-row justify-between gap-4">
<FormField
name="weight"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:weight'} />
</FormLabel>
<FormControl>
<Input
type="number"
placeholder="kg"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === '' ? null : Number(e.target.value),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex flex-row justify-between gap-4">
<FormField
name="weight"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:weight'} />
</FormLabel>
<FormControl>
<Input
type="number"
placeholder="kg"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === '' ? null : Number(e.target.value),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
name="height"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:height'} />
</FormLabel>
<FormControl>
<Input
placeholder="cm"
type="number"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === '' ? null : Number(e.target.value),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField
name="height"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:height'} />
</FormLabel>
<FormControl>
<Input
placeholder="cm"
type="number"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === '' ? null : Number(e.target.value),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</>
)}
<FormField
name="userConsent"