import { create } from 'zustand'; import { TempoProps, TempoPropsKontakPerson } from '~/types/tempo'; import { TempoSchema, TempoSchemaKontakPerson } from '~/validations/tempo'; import { boolean, ZodError } from 'zod'; type State = { form: TempoProps; errors: { [key in keyof TempoProps]?: string; }; isCheckedTNC: boolean; isOpenTNC: boolean; isValidCaptcha: boolean; }; type Action = { updateForm: (name: string, value: string) => void; updateValidCaptcha: (value: boolean) => void; toggleCheckTNC: () => void; openTNC: () => void; closeTNC: () => void; validate: () => void; resetForm: () => void; }; export const usePengajuanTempoStore = create((set, get) => ({ form: { name: '', industry_id: '', street: '', state: '', city: '', zip: '', mobile: '', bankName: '', accountName: '', accountNumber: '', estimasi: '', tempoDuration: '', bersedia: '', categoryProduk: '', tempoLimit: '', }, updateForm: (name, value) => set((state) => ({ form: { ...state.form, [name]: value } })), errors: {}, validate: () => { try { TempoSchema.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 TempoProps] = 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 })), resetForm: () => set({ form: { name: '', industry_id: '', street: '', state: '', city: '', zip: '', mobile: '', bankName: '', accountName: '', accountNumber: '', estimasi: '', tempoDuration: '', bersedia: '', categoryProduk: '', tempoLimit: '', }, }), })); type StateKontakPerson = { formKontakPerson: TempoPropsKontakPerson; errorsKontakPerson: { [key in keyof TempoPropsKontakPerson]?: string; }; }; type ActionKontakPerson = { updateFormKontakPerson: (name: string, value: string) => void; validateKontakPerson: () => void; resetFormKontakPerson: () => void; }; export const usePengajuanTempoStoreKontakPerson = create< StateKontakPerson & ActionKontakPerson >((set, get) => ({ formKontakPerson: { direkturName: '', direkturMobile: '', direkturEmail: '', purchasingName: '', purchasingEmail: '', financeMobile: '', financeName: '', financeEmail: '', purchasingMobile: '', }, updateFormKontakPerson: (name, value) => set((state) => ({ formKontakPerson: { ...state.formKontakPerson, [name]: value }, })), errorsKontakPerson: {}, validateKontakPerson: () => { try { TempoSchemaKontakPerson.parse(get().formKontakPerson); set({ errorsKontakPerson: {} }); } catch (error) { if (error instanceof ZodError) { const errorsKontakPerson: StateKontakPerson['errorsKontakPerson'] = {}; error.errors.forEach( (e) => (errorsKontakPerson[e.path[0] as keyof TempoPropsKontakPerson] = e.message) ); set({ errorsKontakPerson }); } } }, resetFormKontakPerson: () => set({ formKontakPerson: { direkturName: '', direkturMobile: '', direkturEmail: '', purchasingName: '', purchasingEmail: '', financeName: '', financeMobile: '', financeEmail: '', purchasingMobile: '', }, }), }));