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/stores | |
| parent | 1602cff06e13bb03e5c48e8369abf5c803426e4d (diff) | |
- Add redirect after activation
- Add register form validation
Diffstat (limited to 'src-migrate/common/stores')
| -rw-r--r-- | src-migrate/common/stores/useRegisterStore.ts | 28 |
1 files changed, 27 insertions, 1 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, |
