summaryrefslogtreecommitdiff
path: root/src-migrate/common/stores/useRegisterStore.ts
blob: 725bbfda0e22184da975d2d49d50f5f39442c20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { create } from 'zustand';
import { RegisterProps } from '../types/auth';

type State = {
  form: RegisterProps;
  isValid: boolean;
  isCheckedTNC: boolean;
  isOpenTNC: boolean;
  isValidCaptcha: boolean;
};

type Action = {
  updateForm: (name: string, value: string) => void;
  updateValidCaptcha: (value: boolean) => void;
  toggleCheckTNC: () => void;
  openTNC: () => void;
  closeTNC: () => void;
};

export const useRegisterStore = create<State & Action>((set) => ({
  form: {
    company: '',
    name: '',
    email: '',
    password: '',
    phone: '',
  },
  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,
      };
    }),
  toggleCheckTNC: () => set((state) => ({ isCheckedTNC: !state.isCheckedTNC })),
  openTNC: () => set(() => ({ isOpenTNC: true })),
  closeTNC: () => set(() => ({ isOpenTNC: false })),
  updateValidCaptcha: (value) => set(() => ({ isValidCaptcha: value })),
}));