summaryrefslogtreecommitdiff
path: root/src-migrate/modules/register
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2024-01-13 10:35:22 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2024-01-13 10:35:22 +0700
commitf62b2345f463695ef0f8f79830cd76b6e0332821 (patch)
treec06ff12a8312e3a02b0203f588db0f4da044c911 /src-migrate/modules/register
parentee0b5893ac039ab05fe8247647364a923d707da3 (diff)
Refactor src migrate folder
Diffstat (limited to 'src-migrate/modules/register')
-rw-r--r--src-migrate/modules/register/components/Form.tsx4
-rw-r--r--src-migrate/modules/register/components/FormCaptcha.tsx4
-rw-r--r--src-migrate/modules/register/components/TermCondition.tsx4
-rw-r--r--src-migrate/modules/register/stores/useRegisterStore.ts60
4 files changed, 66 insertions, 6 deletions
diff --git a/src-migrate/modules/register/components/Form.tsx b/src-migrate/modules/register/components/Form.tsx
index dc9107b2..b834f97a 100644
--- a/src-migrate/modules/register/components/Form.tsx
+++ b/src-migrate/modules/register/components/Form.tsx
@@ -1,7 +1,7 @@
import { ChangeEvent, useMemo } from "react";
import { useMutation } from "react-query";
-import { useRegisterStore } from "~/common/stores/useRegisterStore";
-import { RegisterProps } from "~/common/types/auth";
+import { useRegisterStore } from "../stores/useRegisterStore";
+import { RegisterProps } from "~/types/auth";
import { registerUser } from "~/services/auth";
import TermCondition from "./TermCondition";
import FormCaptcha from "./FormCaptcha";
diff --git a/src-migrate/modules/register/components/FormCaptcha.tsx b/src-migrate/modules/register/components/FormCaptcha.tsx
index 967be017..fbea2b10 100644
--- a/src-migrate/modules/register/components/FormCaptcha.tsx
+++ b/src-migrate/modules/register/components/FormCaptcha.tsx
@@ -1,5 +1,5 @@
-import ReCaptcha from '~/common/components/elements/ReCaptcha'
-import { useRegisterStore } from '~/common/stores/useRegisterStore'
+import { ReCaptcha } from '~/components/ui/re-captcha'
+import { useRegisterStore } from "../stores/useRegisterStore";
const FormCaptcha = () => {
const { updateValidCaptcha } = useRegisterStore()
diff --git a/src-migrate/modules/register/components/TermCondition.tsx b/src-migrate/modules/register/components/TermCondition.tsx
index 6b95ba19..b7729deb 100644
--- a/src-migrate/modules/register/components/TermCondition.tsx
+++ b/src-migrate/modules/register/components/TermCondition.tsx
@@ -1,7 +1,7 @@
import { Checkbox } from '@chakra-ui/react'
import React from 'react'
-import Modal from '~/common/components/elements/Modal'
-import { useRegisterStore } from '~/common/stores/useRegisterStore'
+import { Modal } from '~/components/ui/modal'
+import { useRegisterStore } from "../stores/useRegisterStore";
import PageContent from '~/modules/page-content'
const TermCondition = () => {
diff --git a/src-migrate/modules/register/stores/useRegisterStore.ts b/src-migrate/modules/register/stores/useRegisterStore.ts
new file mode 100644
index 00000000..d8abf52b
--- /dev/null
+++ b/src-migrate/modules/register/stores/useRegisterStore.ts
@@ -0,0 +1,60 @@
+import { create } from 'zustand';
+import { RegisterProps } from '~/types/auth';
+import { registerSchema } from '~/validations/auth';
+import { ZodError } from 'zod';
+
+type State = {
+ form: RegisterProps;
+ errors: {
+ [key in keyof RegisterProps]?: 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;
+};
+
+export const useRegisterStore = create<State & Action>((set, get) => ({
+ form: {
+ company: '',
+ name: '',
+ email: '',
+ password: '',
+ phone: '',
+ },
+ updateForm: (name, value) =>
+ set((state) => ({ form: { ...state.form, [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 });
+ }
+ }
+ },
+ 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 })),
+}));