finish form extraction
This commit is contained in:
1 parent
6dcfb67246
commit
18408a6bf6
15 files changed
+65
-64
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { profileSchema } from "@/validation/zod";
|
import { profileSchema } from "@/validation";
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import { recoverySchema } from "@/validation/zod";
|
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
|
import type { FormValues } from "@/pages/recovery/RecoveryForm";
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
export const useInitForm = ({
|
export const useInitForm = ({
|
||||||
initialData,
|
initialData,
|
||||||
}: {
|
}: {
|
||||||
initialData: Partial<z.infer<typeof recoverySchema>> | undefined;
|
initialData: FormValues | undefined;
|
||||||
}) => {
|
}) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
const defaults: z.infer<typeof recoverySchema> = {
|
const defaults: FormValues = {
|
||||||
email: initialData?.email || "",
|
email: initialData?.email || "",
|
||||||
};
|
};
|
||||||
return { defaults };
|
return { defaults };
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { resetSchema } from "@/validation/zod";
|
import { resetSchema } from "@/validation";
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { BasePage, ConfigBox } from "@/components/design-elements";
|
|||||||
import { useEffect, useState, useMemo } from "react";
|
import { useEffect, useState, useMemo } from "react";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { inviteSchema } from "@/validation/zod";
|
import { inviteSchema } from "@/validation";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { useForm, Controller } from "react-hook-form";
|
import { useForm, Controller } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
|||||||
@@ -5,12 +5,10 @@ import { useState, useEffect } from "react";
|
|||||||
import type { IUser } from "@/types/user";
|
import type { IUser } from "@/types/user";
|
||||||
import type { ApiResponse } from "@/types/api";
|
import type { ApiResponse } from "@/types/api";
|
||||||
import { useGet, usePatch, useGetOnDemand } from "@/hooks/UseApi";
|
import { useGet, usePatch, useGetOnDemand } from "@/hooks/UseApi";
|
||||||
import { z } from "zod";
|
|
||||||
import { profileSchema } from "@/validation/zod";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useAppDispatch } from "@/hooks/AppHooks";
|
import { useAppDispatch } from "@/hooks/AppHooks";
|
||||||
import { setUser as setUserGlobal } from "@/features/authSlice";
|
import { setUser as setUserGlobal } from "@/features/authSlice";
|
||||||
type FormValues = z.infer<typeof profileSchema>;
|
import type { FormValues } from "@/pages/profile/ProfileForm";
|
||||||
|
|
||||||
const Profile = () => {
|
const Profile = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import { useTheme } from "@mui/material/styles";
|
|||||||
import { useInitForm } from "@/hooks/forms/UseInitProfileForm";
|
import { useInitForm } from "@/hooks/forms/UseInitProfileForm";
|
||||||
import type { IUser } from "@/types/user";
|
import type { IUser } from "@/types/user";
|
||||||
|
|
||||||
import { profileSchema } from "@/validation/zod";
|
import { profileSchema } from "@/validation";
|
||||||
type FormData = z.infer<typeof profileSchema>;
|
export type FormValues = z.infer<typeof profileSchema>;
|
||||||
|
|
||||||
export const ProfileForm = ({
|
export const ProfileForm = ({
|
||||||
user,
|
user,
|
||||||
@@ -23,21 +23,21 @@ export const ProfileForm = ({
|
|||||||
}: {
|
}: {
|
||||||
user: Partial<IUser> | null;
|
user: Partial<IUser> | null;
|
||||||
hasUser?: boolean;
|
hasUser?: boolean;
|
||||||
initialData?: Partial<FormData>;
|
initialData?: Partial<FormValues>;
|
||||||
onSubmit: SubmitHandler<FormData>;
|
onSubmit: SubmitHandler<FormValues>;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { defaults } = useInitForm({ initialData: initialData });
|
const { defaults } = useInitForm({ initialData: initialData });
|
||||||
const resolver = zodResolver(profileSchema) as unknown as Resolver<FormData>;
|
const resolver = zodResolver(profileSchema) as Resolver<FormValues>;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
control,
|
control,
|
||||||
reset,
|
reset,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
} = useForm<FormData>({
|
} = useForm<FormValues>({
|
||||||
resolver,
|
resolver,
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
firstName: "",
|
firstName: "",
|
||||||
|
|||||||
@@ -2,19 +2,16 @@ import { AuthBasePage } from "@/components/auth";
|
|||||||
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { RecoveryForm } from "./RecoveryForm";
|
import { RecoveryForm } from "./RecoveryForm";
|
||||||
import { recoverySchema } from "@/validation/zod";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { usePost } from "@/hooks/UseApi";
|
import { usePost } from "@/hooks/UseApi";
|
||||||
import { useToast } from "@/hooks/UseToast";
|
import { useToast } from "@/hooks/UseToast";
|
||||||
|
import type { FormValues } from "@/pages/recovery/RecoveryForm";
|
||||||
type FormData = z.infer<typeof recoverySchema>;
|
|
||||||
|
|
||||||
const Recovery = () => {
|
const Recovery = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { post, loading } = usePost<FormData>();
|
const { post, loading } = usePost<FormValues>();
|
||||||
const { showToast } = useToast();
|
const { showToast } = useToast();
|
||||||
|
|
||||||
const onSubmit = async (data: FormData) => {
|
const onSubmit = async (data: FormValues) => {
|
||||||
await post("/recovery", data);
|
await post("/recovery", data);
|
||||||
showToast(t("common.toasts.recoveryEmailSent"));
|
showToast(t("common.toasts.recoveryEmailSent"));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,18 +9,18 @@ import { useEffect } from "react";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import { useInitForm } from "@/hooks/forms/UseInitRegisterForm";
|
import { useInitForm } from "@/hooks/forms/UseInitRecoveryForm";
|
||||||
|
|
||||||
import { recoverySchema } from "@/validation/zod";
|
import { recoverySchema } from "@/validation";
|
||||||
type FormData = z.infer<typeof recoverySchema>;
|
export type FormValues = z.infer<typeof recoverySchema>;
|
||||||
|
|
||||||
export const RecoveryForm = ({
|
export const RecoveryForm = ({
|
||||||
initialData,
|
initialData,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
loading,
|
loading,
|
||||||
}: {
|
}: {
|
||||||
initialData?: Partial<FormData>;
|
initialData?: FormValues;
|
||||||
onSubmit: SubmitHandler<FormData>;
|
onSubmit: SubmitHandler<FormValues>;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -32,7 +32,7 @@ export const RecoveryForm = ({
|
|||||||
control,
|
control,
|
||||||
reset,
|
reset,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
} = useForm<FormData>({
|
} = useForm<FormValues>({
|
||||||
resolver: zodResolver(recoverySchema),
|
resolver: zodResolver(recoverySchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
email: "",
|
email: "",
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import { AuthBasePage } from "@/components/auth";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useParams } from "react-router";
|
import { useParams } from "react-router";
|
||||||
import { ResetForm } from "@/pages/recovery/ResetForm";
|
import { ResetForm } from "@/pages/recovery/ResetForm";
|
||||||
import { resetSchema } from "@/validation/zod";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { usePost } from "@/hooks/UseApi";
|
import { usePost } from "@/hooks/UseApi";
|
||||||
import { useToast } from "@/hooks/UseToast";
|
import { useToast } from "@/hooks/UseToast";
|
||||||
import { useNavigate } from "react-router";
|
import { useNavigate } from "react-router";
|
||||||
@@ -15,8 +13,7 @@ import {
|
|||||||
setSelectedTeamId,
|
setSelectedTeamId,
|
||||||
logout,
|
logout,
|
||||||
} from "@/features/authSlice";
|
} from "@/features/authSlice";
|
||||||
|
import type { FormValues } from "@/pages/recovery/ResetForm";
|
||||||
type FormData = z.infer<typeof resetSchema>;
|
|
||||||
|
|
||||||
const Reset = () => {
|
const Reset = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -26,7 +23,7 @@ const Reset = () => {
|
|||||||
const { toastSuccess } = useToast();
|
const { toastSuccess } = useToast();
|
||||||
const { id: token } = useParams<{ id: string }>();
|
const { id: token } = useParams<{ id: string }>();
|
||||||
|
|
||||||
const onSubmit = async (data: FormData) => {
|
const onSubmit = async (data: FormValues) => {
|
||||||
const result = await post(`/recovery/reset`, {
|
const result = await post(`/recovery/reset`, {
|
||||||
password: data.password,
|
password: data.password,
|
||||||
token: token || "",
|
token: token || "",
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import { useInitForm } from "@/hooks/forms/UseInitResetForm";
|
import { useInitForm } from "@/hooks/forms/UseInitResetForm";
|
||||||
|
|
||||||
import { resetSchema } from "@/validation/zod";
|
import { resetSchema } from "@/validation";
|
||||||
type FormData = z.infer<typeof resetSchema>;
|
export type FormValues = z.infer<typeof resetSchema>;
|
||||||
|
|
||||||
export const ResetForm = ({
|
export const ResetForm = ({
|
||||||
initialData,
|
initialData,
|
||||||
@@ -20,8 +20,8 @@ export const ResetForm = ({
|
|||||||
loading,
|
loading,
|
||||||
}: {
|
}: {
|
||||||
hasUser?: boolean;
|
hasUser?: boolean;
|
||||||
initialData?: Partial<FormData>;
|
initialData?: Partial<FormValues>;
|
||||||
onSubmit: SubmitHandler<FormData>;
|
onSubmit: SubmitHandler<FormValues>;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -33,7 +33,7 @@ export const ResetForm = ({
|
|||||||
control,
|
control,
|
||||||
reset,
|
reset,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
} = useForm<FormData>({
|
} = useForm<FormValues>({
|
||||||
resolver: zodResolver(resetSchema),
|
resolver: zodResolver(resetSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
password: "",
|
password: "",
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
export * from "./zod";
|
|
||||||
export * from "./login";
|
export * from "./login";
|
||||||
export * from "./register";
|
export * from "./register";
|
||||||
export * from "./monitor";
|
export * from "./monitor";
|
||||||
@@ -10,3 +9,7 @@ export * from "./maintenance";
|
|||||||
export * from "./team";
|
export * from "./team";
|
||||||
export * from "./system-settings";
|
export * from "./system-settings";
|
||||||
export * from "./status-page";
|
export * from "./status-page";
|
||||||
|
export * from "./invite";
|
||||||
|
export * from "./recovery";
|
||||||
|
export * from "./reset";
|
||||||
|
export * from "./profile";
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const inviteSchema = z.object({
|
||||||
|
email: z.email("Invalid email address"),
|
||||||
|
teamId: z.string().min(1, "Team is required"),
|
||||||
|
teamRoleId: z.string().min(1, "Role is required"),
|
||||||
|
orgRoleId: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
@@ -1,30 +1,5 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
export const inviteSchema = z.object({
|
|
||||||
email: z.email("Invalid email address"),
|
|
||||||
teamId: z.string().min(1, "Team is required"),
|
|
||||||
teamRoleId: z.string().min(1, "Role is required"),
|
|
||||||
orgRoleId: z.string().optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const recoverySchema = z.object({
|
|
||||||
email: z.email({ message: "Invalid email address" }).trim().toLowerCase(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const resetSchema = z
|
|
||||||
.object({
|
|
||||||
password: z
|
|
||||||
.string()
|
|
||||||
.min(6, { message: "Password must be at least 6 characters" }),
|
|
||||||
confirmPassword: z
|
|
||||||
.string()
|
|
||||||
.min(6, { message: "Confirm Password must be at least 6 characters" }),
|
|
||||||
})
|
|
||||||
.refine((data) => data.password === data.confirmPassword, {
|
|
||||||
path: ["confirmPassword"],
|
|
||||||
message: "Passwords must match",
|
|
||||||
});
|
|
||||||
|
|
||||||
export const profileSchema = z
|
export const profileSchema = z
|
||||||
.object({
|
.object({
|
||||||
firstName: z
|
firstName: z
|
||||||
@@ -63,3 +38,4 @@ export const profileSchema = z
|
|||||||
path: ["confirmPassword"],
|
path: ["confirmPassword"],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const recoverySchema = z.object({
|
||||||
|
email: z.email({ message: "Invalid email address" }).trim().toLowerCase(),
|
||||||
|
});
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const resetSchema = z
|
||||||
|
.object({
|
||||||
|
password: z
|
||||||
|
.string()
|
||||||
|
.min(6, { message: "Password must be at least 6 characters" }),
|
||||||
|
confirmPassword: z
|
||||||
|
.string()
|
||||||
|
.min(6, { message: "Confirm Password must be at least 6 characters" }),
|
||||||
|
})
|
||||||
|
.refine((data) => data.password === data.confirmPassword, {
|
||||||
|
path: ["confirmPassword"],
|
||||||
|
message: "Passwords must match",
|
||||||
|
});
|
||||||
|
|
||||||
Reference in new issue
Block a user