diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-31 10:44:25 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-31 10:44:25 +0700 |
| commit | 6b221cccd58710682c99db7afbc29322da042880 (patch) | |
| tree | ef659fa4400b045a21d91d60e2eecea3ec17537e /src-migrate/common | |
| parent | 1602cff06e13bb03e5c48e8369abf5c803426e4d (diff) | |
- Add redirect after activation
- Add register form validation
Diffstat (limited to 'src-migrate/common')
| -rw-r--r-- | src-migrate/common/stores/useRegisterStore.ts | 28 | ||||
| -rw-r--r-- | src-migrate/common/types/auth.ts | 10 | ||||
| -rw-r--r-- | src-migrate/common/validations/auth.ts | 13 |
3 files changed, 43 insertions, 8 deletions
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<State & Action>((set) => ({ +export const useRegisterStore = create<State & Action>((set, get) => ({ form: { company: '', name: '', @@ -25,6 +31,26 @@ export const useRegisterStore = create<State & Action>((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<typeof registerSchema>; 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'), +}); |
