summaryrefslogtreecommitdiff
path: root/src-migrate/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src-migrate/modules')
-rw-r--r--src-migrate/modules/register/stores/usePengajuanTempoStore.ts93
1 files changed, 93 insertions, 0 deletions
diff --git a/src-migrate/modules/register/stores/usePengajuanTempoStore.ts b/src-migrate/modules/register/stores/usePengajuanTempoStore.ts
new file mode 100644
index 00000000..6f3bc13d
--- /dev/null
+++ b/src-migrate/modules/register/stores/usePengajuanTempoStore.ts
@@ -0,0 +1,93 @@
+import { create } from 'zustand';
+import { TempoProps } from '~/types/tempo';
+import { TempoSchema } 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<State & Action>((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: '',
+ },
+ }),
+}));