summaryrefslogtreecommitdiff
path: root/src-migrate/modules/register/stores/useRegisterStore.ts
blob: 7b414ec69b59bfd22d9093cb2f264bec53b850ea (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { create } from 'zustand';
import { RegisterProps } from '~/types/auth';
import { registerSchema } from '~/validations/auth';
import { boolean, ZodError } from 'zod';

type State = {
  form: RegisterProps;
  formBisnis: RegisterProps;
  errors: {
    [key in keyof RegisterProps]?: string;
  };
  isCheckedTNC: boolean;
  isOpenTNC: boolean;
  isValidCaptcha: boolean;
};

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

export const useRegisterStore = create<State & Action>((set, get) => ({
  form: {
    business_name: '',
    companyType: '',
    name: '',
    email: '',
    email_partner: '',
    password: '',
    phone: '',
    sppkp_document: '',
    npwp_document: '',
    nameWajibPajak: '',
    industry: '',
    badanUsaha: '',
    jenisUsaha: '',
    npwp: '',
    nama_wajib_pajak : '',
    is_pkp: '',
  },
  formBisnis: {
    companyType: '',
    business_name: '',
    name: '',
    nama_wajib_pajak : '',
    email: '',
    email_partner: '',
    password: '',
    phone: '',
    sppkp_document: '',
    npwp_document: '',
    nameWajibPajak: '',
    industry: '',
    badanUsaha: '',
    jenisUsaha: '',
    npwp: '',
    is_pkp:'',
  },
  updateForm: (name, value) =>
    set((state) => ({ form: { ...state.form, [name]: value } })),
  
  updateFormBisnis: (name, value) =>
    set((state) => ({ formBisnis: { ...state.formBisnis, [name]: value } })),

  errors: {},
  validate: () => {
    try {
      registerSchema.parse(get().form);
      set({ errors: {} });
    } catch (error) {
      if (error instanceof ZodError) {
        const errors: State['errors'] = {};
        error.errors.forEach(
          (e) => (errors[e.path[0] as keyof RegisterProps] = e.message)
        );
        set({ errors });
      }
    }
  },
  
  validateFormBisnis: () => {
    try {
      registerSchema.parse(get().formBisnis);
      set({ errors: {} });
    } catch (error) {
      if (error instanceof ZodError) {
        const errors: State['errors'] = {};
        error.errors.forEach(
          (e) => (errors[e.path[0] as keyof RegisterProps] = e.message)
        );
        set({ errors });
      }
    }
  },

  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 })),
}));