diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-23 17:11:33 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-10-23 17:11:33 +0700 |
| commit | 90710579ba1c12060877f6ec2d26103f9c31058d (patch) | |
| tree | 307032cfb8cd13b790c569bc443258b00b07684e /src-migrate/common/stores/useRegisterStore.ts | |
| parent | a001da95b9c03167656aec8a573cf60c12164b3f (diff) | |
Refactor and migrate register page
Diffstat (limited to 'src-migrate/common/stores/useRegisterStore.ts')
| -rw-r--r-- | src-migrate/common/stores/useRegisterStore.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts new file mode 100644 index 00000000..fcd2cd8b --- /dev/null +++ b/src-migrate/common/stores/useRegisterStore.ts @@ -0,0 +1,52 @@ +import { create } from 'zustand'; +import { RegisterProps } from '../types/auth'; + +type State = { + form: RegisterProps; + isValid: boolean; + isCheckedTNC: boolean; + isOpenTNC: boolean; +}; + +type Action = { + updateForm: (name: string, value: string) => void; + toggleCheckTNC: () => void; + openTNC: () => void; + closeTNC: () => void; +}; + +export const useRegisterStore = create<State & Action>((set) => ({ + form: { + company: '', + name: '', + email: '', + password: '', + }, + isValid: false, + isCheckedTNC: false, + isOpenTNC: 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, + }; + }), + toggleCheckTNC: () => set((state) => ({ isCheckedTNC: !state.isCheckedTNC })), + openTNC: () => set(() => ({ isOpenTNC: true })), + closeTNC: () => set(() => ({ isOpenTNC: false })), +})); |
