diff --git a/app/auth/update-account/_components/update-account-form.tsx b/app/auth/update-account/_components/update-account-form.tsx index bdda351..0741489 100644 --- a/app/auth/update-account/_components/update-account-form.tsx +++ b/app/auth/update-account/_components/update-account-form.tsx @@ -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; +type UpdateAccountFormValues = z.infer; 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({ > ( - + @@ -82,14 +84,14 @@ export function UpdateAccountForm({ ( - + @@ -98,7 +100,7 @@ export function UpdateAccountForm({ ( @@ -143,72 +145,76 @@ export function UpdateAccountForm({ )} /> - ( - - - - - - - - - - )} - /> + {!isEmailUser && ( + <> + ( + + + + + + + + + + )} + /> -
- ( - - - - - - - field.onChange( - e.target.value === '' ? null : Number(e.target.value), - ) - } - /> - - - - )} - /> +
+ ( + + + + + + + field.onChange( + e.target.value === '' ? null : Number(e.target.value), + ) + } + /> + + + + )} + /> - ( - - - - - - - field.onChange( - e.target.value === '' ? null : Number(e.target.value), - ) - } - /> - - - - )} - /> -
+ ( + + + + + + + field.onChange( + e.target.value === '' ? null : Number(e.target.value), + ) + } + /> + + + + )} + /> +
+ + )} { + 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, }); diff --git a/app/auth/update-account/_lib/server/update-account.ts b/app/auth/update-account/_lib/server/update-account.ts index ebead4c..7e70139 100644 --- a/app/auth/update-account/_lib/server/update-account.ts +++ b/app/auth/update-account/_lib/server/update-account.ts @@ -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, }, ); diff --git a/app/auth/update-account/page.tsx b/app/auth/update-account/page.tsx index 6c1030f..031120c 100644 --- a/app/auth/update-account/page.tsx +++ b/app/auth/update-account/page.tsx @@ -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() {

- +