From 5a9210e560ca47d8f4d8c43b06fc93d1d064a6f6 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 10 Nov 2023 16:23:33 +0700 Subject: Add close on modal otp activation, change validation register schema, fix term and condition checkbox --- src-migrate/common/stores/useRegisterStore.ts | 26 ++++++++++++++------------ src-migrate/common/types/auth.ts | 4 ++-- src-migrate/common/validations/auth.ts | 26 +++++++++++++++----------- 3 files changed, 31 insertions(+), 25 deletions(-) (limited to 'src-migrate/common') diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts index ab2d7410..90ce8a2b 100644 --- a/src-migrate/common/stores/useRegisterStore.ts +++ b/src-migrate/common/stores/useRegisterStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { RegisterProps } from '../types/auth'; import { registerSchema } from '../validations/auth'; -import { ValidationError } from 'yup'; +import { ZodError } from 'zod'; type State = { form: RegisterProps; @@ -34,18 +34,20 @@ export const useRegisterStore = create((set, get) => ({ set((state) => ({ form: { ...state.form, [name]: value } })), errors: {}, - validate: () => - registerSchema - .validate(get().form, { abortEarly: false }) - .then(() => set({ errors: {} })) - .catch((err: ValidationError) => { - const validationErrors: State['errors'] = {}; - err.inner.forEach( - (e) => (validationErrors[e.path as keyof RegisterProps] = e.message) + validate: () => { + try { + registerSchema.parse(get().form); + set({ errors: {} }); + } catch (error) { + if (error instanceof ZodError) { + const errors: State['errors'] = {}; + error.errors.forEach( + (e) => (errors[e.path[0] as keyof RegisterProps] = e.message) ); - set({ errors: validationErrors }); - }), - + set({ errors }); + } + } + }, isCheckedTNC: false, toggleCheckTNC: () => set((state) => ({ isCheckedTNC: !state.isCheckedTNC })), diff --git a/src-migrate/common/types/auth.ts b/src-migrate/common/types/auth.ts index 5909584a..65fd06c7 100644 --- a/src-migrate/common/types/auth.ts +++ b/src-migrate/common/types/auth.ts @@ -1,6 +1,6 @@ import { registerSchema } from '../validations/auth'; import { OdooApiProps } from './odoo'; -import * as yup from 'yup'; +import { z } from 'zod'; export type AuthProps = { id: number; @@ -19,7 +19,7 @@ export type AuthProps = { export type AuthApiProps = OdooApiProps & { result: AuthProps }; -export type RegisterProps = yup.InferType; +export type RegisterProps = z.infer; export type RegisterResApiProps = { register: boolean; diff --git a/src-migrate/common/validations/auth.ts b/src-migrate/common/validations/auth.ts index 94b40849..78fc5e71 100644 --- a/src-migrate/common/validations/auth.ts +++ b/src-migrate/common/validations/auth.ts @@ -1,13 +1,17 @@ -import { object, string } from 'yup'; +import { z } from 'zod'; -export const registerSchema = object({ - name: string().required('Nama harus diisi'), - email: string() - .email('Email harus menggunakan format example@mail.com') - .required('Email harus diisi'), - password: string() - .min(6, 'Password minimal 6 karakter') - .required('Password harus diisi'), - company: string().optional(), - phone: string().required('Nomor telepon harus diisi'), +export const registerSchema = z.object({ + name: z.string().min(1, { message: 'Nama harus diisi' }), + email: z + .string() + .min(1, { message: 'Email harus diisi' }) + .email({ message: 'Email harus menggunakan format example@mail.com' }), + password: z.string().min(6, { message: 'Password minimal 6 karakter' }), + company: z.string().optional(), + phone: z + .string() + .min(1, { message: 'Nomor telepon harus diisi' }) + .refine((val) => /^\d{10,12}$/.test(val), { + message: 'Format nomor telepon tidak valid, contoh: 081234567890', + }), }); -- cgit v1.2.3