summaryrefslogtreecommitdiff
path: root/src-migrate/common
diff options
context:
space:
mode:
Diffstat (limited to 'src-migrate/common')
-rw-r--r--src-migrate/common/stores/useRegisterStore.ts28
-rw-r--r--src-migrate/common/types/auth.ts10
-rw-r--r--src-migrate/common/validations/auth.ts13
3 files changed, 43 insertions, 8 deletions
diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts
index 725bbfda..d6c7db2a 100644
--- a/src-migrate/common/stores/useRegisterStore.ts
+++ b/src-migrate/common/stores/useRegisterStore.ts
@@ -1,8 +1,13 @@
import { create } from 'zustand';
import { RegisterProps } from '../types/auth';
+import { registerSchema } from '../validations/auth';
+import { ValidationError } from 'yup';
type State = {
form: RegisterProps;
+ errors: {
+ [key in keyof RegisterProps]?: string;
+ };
isValid: boolean;
isCheckedTNC: boolean;
isOpenTNC: boolean;
@@ -15,9 +20,10 @@ type Action = {
toggleCheckTNC: () => void;
openTNC: () => void;
closeTNC: () => void;
+ validate: () => void;
};
-export const useRegisterStore = create<State & Action>((set) => ({
+export const useRegisterStore = create<State & Action>((set, get) => ({
form: {
company: '',
name: '',
@@ -25,6 +31,26 @@ export const useRegisterStore = create<State & Action>((set) => ({
password: '',
phone: '',
},
+ errors: {},
+ validate: () =>
+ registerSchema
+ .validate(get().form, { abortEarly: false })
+ .then(() => {
+ set({
+ errors: {},
+ isValid: false,
+ });
+ })
+ .catch((err: ValidationError) => {
+ const validationErrors: State['errors'] = {};
+ err.inner.forEach(
+ (e) => (validationErrors[e.path as keyof RegisterProps] = e.message)
+ );
+ set({
+ errors: validationErrors,
+ isValid: false,
+ });
+ }),
isValid: false,
isCheckedTNC: false,
isOpenTNC: false,
diff --git a/src-migrate/common/types/auth.ts b/src-migrate/common/types/auth.ts
index ca7b562a..5909584a 100644
--- a/src-migrate/common/types/auth.ts
+++ b/src-migrate/common/types/auth.ts
@@ -1,4 +1,6 @@
+import { registerSchema } from '../validations/auth';
import { OdooApiProps } from './odoo';
+import * as yup from 'yup';
export type AuthProps = {
id: number;
@@ -17,13 +19,7 @@ export type AuthProps = {
export type AuthApiProps = OdooApiProps & { result: AuthProps };
-export type RegisterProps = {
- name: string;
- email: string;
- password: string;
- company: string;
- phone: string;
-};
+export type RegisterProps = yup.InferType<typeof registerSchema>;
export type RegisterResApiProps = {
register: boolean;
diff --git a/src-migrate/common/validations/auth.ts b/src-migrate/common/validations/auth.ts
new file mode 100644
index 00000000..94b40849
--- /dev/null
+++ b/src-migrate/common/validations/auth.ts
@@ -0,0 +1,13 @@
+import { object, string } from 'yup';
+
+export const registerSchema = object({
+ name: string().required('Nama harus diisi'),
+ email: string()
+ .email('Email harus menggunakan format example@mail.com')
+ .required('Email harus diisi'),
+ password: string()
+ .min(6, 'Password minimal 6 karakter')
+ .required('Password harus diisi'),
+ company: string().optional(),
+ phone: string().required('Nomor telepon harus diisi'),
+});