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"

View File

@@ -1,7 +1,8 @@
import { z } from 'zod';
import Isikukood from 'isikukood';
import parsePhoneNumber from 'libphonenumber-js/min';
export const UpdateAccountSchema = z.object({
const updateAccountSchema = {
firstName: z
.string({
error: 'First name is required',
@@ -12,11 +13,18 @@ export const UpdateAccountSchema = z.object({
error: 'Last name is required',
})
.nonempty(),
personalCode: z
.string({
error: 'Personal code is required',
})
.nonempty(),
personalCode: z.string().refine(
(val) => {
try {
return new Isikukood(val).validate();
} catch {
return false;
}
},
{
message: 'common:formFieldError.invalidPersonalCode',
},
),
email: z.string().email({
message: 'Email is required',
}),
@@ -59,4 +67,26 @@ export const UpdateAccountSchema = z.object({
userConsent: z.boolean().refine((val) => val === true, {
message: 'Must be true',
}),
} as const;
export const UpdateAccountSchemaServer = z.object({
firstName: updateAccountSchema.firstName,
lastName: updateAccountSchema.lastName,
personalCode: updateAccountSchema.personalCode,
email: updateAccountSchema.email,
phone: updateAccountSchema.phone,
city: updateAccountSchema.city,
weight: updateAccountSchema.weight,
height: updateAccountSchema.height,
userConsent: updateAccountSchema.userConsent,
});
export const UpdateAccountSchemaClient = z.object({
firstName: updateAccountSchema.firstName,
lastName: updateAccountSchema.lastName,
personalCode: updateAccountSchema.personalCode,
email: updateAccountSchema.email,
phone: updateAccountSchema.phone,
city: updateAccountSchema.city,
weight: updateAccountSchema.weight.gt(-1).gte(0).nullable(),
height: updateAccountSchema.height.gt(-1).gte(0).nullable(),
userConsent: updateAccountSchema.userConsent,
});

View File

@@ -10,7 +10,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { pathsConfig } from '@kit/shared/config';
import { UpdateAccountSchema } from '../schemas/update-account.schema';
import { UpdateAccountSchemaServer } from '../schemas/update-account.schema';
export const onUpdateAccount = enhanceAction(
async (params: AccountSubmitData) => {
@@ -47,6 +47,6 @@ export const onUpdateAccount = enhanceAction(
}
},
{
schema: UpdateAccountSchema,
schema: UpdateAccountSchemaServer,
},
);

View File

@@ -17,6 +17,7 @@ async function UpdateAccount() {
const { account, user } = await loadCurrentUserAccount();
const isKeycloakUser = user?.app_metadata?.provider === 'keycloak';
const isEmailUser = user?.app_metadata?.provider === 'email';
if (!user) {
redirect(pathsConfig.auth.signIn);
@@ -50,7 +51,7 @@ async function UpdateAccount() {
<p className="text-muted-foreground pt-1 text-sm">
<Trans i18nKey={'account:updateAccount:description'} />
</p>
<UpdateAccountForm defaultValues={defaultValues} />
<UpdateAccountForm defaultValues={defaultValues} isEmailUser={isEmailUser} />
</div>
<div className="hidden w-1/2 min-w-[460px] bg-[url(/assets/med-report-logo-big.png)] bg-cover bg-center bg-no-repeat md:block"></div>
</div>