diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-11-10 14:47:52 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-11-10 14:47:52 +0700 |
| commit | 4bed066c03dc55a8f811ff2e23e019d8adc64495 (patch) | |
| tree | 7c8d9335015ec7cad07c7e2385de3bf19cf3ce42 /src-migrate/common/stores/useRegisterStore.ts | |
| parent | 94c1fba12e054165e7447e9cf778dd7de8922304 (diff) | |
Refactor register form
Diffstat (limited to 'src-migrate/common/stores/useRegisterStore.ts')
| -rw-r--r-- | src-migrate/common/stores/useRegisterStore.ts | 45 |
1 files changed, 10 insertions, 35 deletions
diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts index d6c7db2a..ab2d7410 100644 --- a/src-migrate/common/stores/useRegisterStore.ts +++ b/src-migrate/common/stores/useRegisterStore.ts @@ -8,7 +8,6 @@ type State = { errors: { [key in keyof RegisterProps]?: string; }; - isValid: boolean; isCheckedTNC: boolean; isOpenTNC: boolean; isValidCaptcha: boolean; @@ -31,53 +30,29 @@ export const useRegisterStore = create<State & Action>((set, get) => ({ password: '', phone: '', }, + updateForm: (name, value) => + set((state) => ({ form: { ...state.form, [name]: value } })), + errors: {}, validate: () => registerSchema .validate(get().form, { abortEarly: false }) - .then(() => { - set({ - errors: {}, - isValid: false, - }); - }) + .then(() => set({ errors: {} })) .catch((err: ValidationError) => { const validationErrors: State['errors'] = {}; err.inner.forEach( (e) => (validationErrors[e.path as keyof RegisterProps] = e.message) ); - set({ - errors: validationErrors, - isValid: false, - }); + set({ errors: validationErrors }); }), - isValid: false, - isCheckedTNC: false, - isOpenTNC: false, - isValidCaptcha: false, - updateForm: (name, value) => - set((state) => { - const updatedForm = { ...state.form, [name]: value }; - - const fieldKeys = Object.keys( - updatedForm - ) as (keyof typeof updatedForm)[]; - - const allFieldsValid = fieldKeys.every((key) => { - const value = updatedForm[key]; - if (key === 'company') return true; - - return value !== ''; - }); - - return { - form: updatedForm, - isValid: allFieldsValid, - }; - }), + isCheckedTNC: false, toggleCheckTNC: () => set((state) => ({ isCheckedTNC: !state.isCheckedTNC })), + + isOpenTNC: false, openTNC: () => set(() => ({ isOpenTNC: true })), closeTNC: () => set(() => ({ isOpenTNC: false })), + + isValidCaptcha: false, updateValidCaptcha: (value) => set(() => ({ isValidCaptcha: value })), })); |
