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((set, get) => ({ form: { business_name: '', company_type_id: 0, name: '', email: '', email_partner: '', password: '', phone: '', sppkp_document: '', npwp_document: '', nameWajibPajak: '', industry: { value: '', label: '' }, badanUsaha: '', jenisUsaha: '', npwp: '', nama_wajib_pajak : '', is_pkp: '', }, formBisnis: { company_type_id: 0, business_name: '', name: '', nama_wajib_pajak : '', email: '', email_partner: '', password: '', phone: '', sppkp_document: '', npwp_document: '', nameWajibPajak: '', industry: { value: '', label: '' }, 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 })), }));