From 6b221cccd58710682c99db7afbc29322da042880 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 31 Oct 2023 10:44:25 +0700 Subject: - Add redirect after activation - Add register form validation --- src-migrate/common/stores/useRegisterStore.ts | 28 ++++++++++++++++++++++++++- src-migrate/common/types/auth.ts | 10 +++------- src-migrate/common/validations/auth.ts | 13 +++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src-migrate/common/validations/auth.ts (limited to 'src-migrate/common') diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts index 725bbfda..d6c7db2a 100644 --- a/src-migrate/common/stores/useRegisterStore.ts +++ b/src-migrate/common/stores/useRegisterStore.ts @@ -1,8 +1,13 @@ import { create } from 'zustand'; import { RegisterProps } from '../types/auth'; +import { registerSchema } from '../validations/auth'; +import { ValidationError } from 'yup'; type State = { form: RegisterProps; + errors: { + [key in keyof RegisterProps]?: string; + }; isValid: boolean; isCheckedTNC: boolean; isOpenTNC: boolean; @@ -15,9 +20,10 @@ type Action = { toggleCheckTNC: () => void; openTNC: () => void; closeTNC: () => void; + validate: () => void; }; -export const useRegisterStore = create((set) => ({ +export const useRegisterStore = create((set, get) => ({ form: { company: '', name: '', @@ -25,6 +31,26 @@ export const useRegisterStore = create((set) => ({ password: '', phone: '', }, + errors: {}, + validate: () => + registerSchema + .validate(get().form, { abortEarly: false }) + .then(() => { + set({ + errors: {}, + isValid: false, + }); + }) + .catch((err: ValidationError) => { + const validationErrors: State['errors'] = {}; + err.inner.forEach( + (e) => (validationErrors[e.path as keyof RegisterProps] = e.message) + ); + set({ + errors: validationErrors, + isValid: false, + }); + }), isValid: false, isCheckedTNC: false, isOpenTNC: false, diff --git a/src-migrate/common/types/auth.ts b/src-migrate/common/types/auth.ts index ca7b562a..5909584a 100644 --- a/src-migrate/common/types/auth.ts +++ b/src-migrate/common/types/auth.ts @@ -1,4 +1,6 @@ +import { registerSchema } from '../validations/auth'; import { OdooApiProps } from './odoo'; +import * as yup from 'yup'; export type AuthProps = { id: number; @@ -17,13 +19,7 @@ export type AuthProps = { export type AuthApiProps = OdooApiProps & { result: AuthProps }; -export type RegisterProps = { - name: string; - email: string; - password: string; - company: string; - phone: string; -}; +export type RegisterProps = yup.InferType; export type RegisterResApiProps = { register: boolean; diff --git a/src-migrate/common/validations/auth.ts b/src-migrate/common/validations/auth.ts new file mode 100644 index 00000000..94b40849 --- /dev/null +++ b/src-migrate/common/validations/auth.ts @@ -0,0 +1,13 @@ +import { object, string } from 'yup'; + +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'), +}); -- cgit v1.2.3