post
This commit is contained in:
@@ -2,16 +2,36 @@
|
||||
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
|
||||
// 📌 نوع دادههای فرم
|
||||
interface ProjectFormData {
|
||||
export type ProjectRoleForm = {
|
||||
id: string;
|
||||
expertise: string;
|
||||
subExpertise: string[];
|
||||
numberOfPerson: string;
|
||||
heightMin: string;
|
||||
heightMax: string;
|
||||
weightMin: string;
|
||||
weightMax: string;
|
||||
sizeMin: string;
|
||||
sizeMax: string;
|
||||
eyeColor: string;
|
||||
hairColor: string;
|
||||
portfolioRequired: boolean;
|
||||
};
|
||||
|
||||
export interface ProjectFormData {
|
||||
userId: string | null;
|
||||
expertise: string | null;
|
||||
subExpertise: string[];
|
||||
gender: string;
|
||||
age: string;
|
||||
ageMin: string;
|
||||
ageMax: string;
|
||||
publicType: string;
|
||||
stateId: string;
|
||||
cityId: string;
|
||||
neighborhood: string;
|
||||
address: string;
|
||||
markerCoordinate: number[];
|
||||
projTitle: string;
|
||||
projectTime: string;
|
||||
offerPrice: string;
|
||||
@@ -19,18 +39,29 @@ interface ProjectFormData {
|
||||
numberOfPerson: string;
|
||||
selectedType: string;
|
||||
price2: string;
|
||||
paymentMethod: string;
|
||||
startDate: string;
|
||||
workHoursStart: string;
|
||||
workHoursEnd: string;
|
||||
collaborationType: string;
|
||||
experience: string;
|
||||
roles: ProjectRoleForm[];
|
||||
_id: string;
|
||||
}
|
||||
|
||||
// 📌 مقدار اولیه فرم
|
||||
const initialFormData: ProjectFormData = {
|
||||
export const initialProjectFormData: ProjectFormData = {
|
||||
expertise: null,
|
||||
subExpertise: [],
|
||||
gender: "",
|
||||
age: "",
|
||||
ageMin: "",
|
||||
ageMax: "",
|
||||
publicType: "",
|
||||
stateId: "",
|
||||
cityId: "",
|
||||
neighborhood: "",
|
||||
address: "",
|
||||
markerCoordinate: [],
|
||||
projTitle: "",
|
||||
projectTime: "",
|
||||
offerPrice: "",
|
||||
@@ -38,26 +69,34 @@ const initialFormData: ProjectFormData = {
|
||||
numberOfPerson: "",
|
||||
selectedType: "normal",
|
||||
price2: "",
|
||||
paymentMethod: "",
|
||||
startDate: "",
|
||||
workHoursStart: "",
|
||||
workHoursEnd: "",
|
||||
collaborationType: "",
|
||||
experience: "",
|
||||
roles: [],
|
||||
userId: null,
|
||||
_id: "",
|
||||
};
|
||||
|
||||
// 📌 ایجاد Context
|
||||
const ProjectFormContext = createContext<{
|
||||
formData: ProjectFormData;
|
||||
updateForm: (data: Partial<ProjectFormData>) => void;
|
||||
resetForm: () => void;
|
||||
setIsEditing: (editing: boolean) => void;
|
||||
isEditing: boolean;
|
||||
draftId: string | null;
|
||||
setDraftId: (id: string | null) => void;
|
||||
}>(null!);
|
||||
|
||||
// 📌 Provider برای فرم
|
||||
export const ProjectFormProvider = ({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [draftId, setDraftId] = useState<string | null>(null);
|
||||
const [formData, setFormData] = useState<ProjectFormData>(() => {
|
||||
if (typeof window !== "undefined" && !isEditing) {
|
||||
const storedData = localStorage.getItem("projectForm");
|
||||
@@ -65,16 +104,18 @@ export const ProjectFormProvider = ({
|
||||
const { data, timestamp } = JSON.parse(storedData);
|
||||
const now = new Date().getTime();
|
||||
if (now - timestamp < 10 * 60 * 1000) {
|
||||
return data;
|
||||
} else {
|
||||
localStorage.removeItem("projectForm");
|
||||
return {
|
||||
...initialProjectFormData,
|
||||
...data,
|
||||
roles: data.roles || [],
|
||||
};
|
||||
}
|
||||
localStorage.removeItem("projectForm");
|
||||
}
|
||||
}
|
||||
return initialFormData;
|
||||
return initialProjectFormData;
|
||||
});
|
||||
|
||||
// 📌 ذخیره فرم در localStorage
|
||||
useEffect(() => {
|
||||
if (!isEditing) {
|
||||
const dataToStore = {
|
||||
@@ -85,27 +126,33 @@ export const ProjectFormProvider = ({
|
||||
}
|
||||
}, [formData, isEditing]);
|
||||
|
||||
// 📌 تابع برای آپدیت فرم
|
||||
const updateForm = (data: Partial<ProjectFormData>) => {
|
||||
setFormData((prev) => ({ ...prev, ...data }));
|
||||
};
|
||||
|
||||
// 📌 ریست فرم
|
||||
const resetForm = () => {
|
||||
setFormData(initialFormData);
|
||||
setFormData(initialProjectFormData);
|
||||
setDraftId(null);
|
||||
localStorage.removeItem("projectForm");
|
||||
};
|
||||
|
||||
return (
|
||||
<ProjectFormContext.Provider
|
||||
value={{ formData, updateForm, resetForm, setIsEditing, isEditing }}
|
||||
value={{
|
||||
formData,
|
||||
updateForm,
|
||||
resetForm,
|
||||
setIsEditing,
|
||||
isEditing,
|
||||
draftId,
|
||||
setDraftId,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ProjectFormContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// 📌 هوک برای استفاده از فرم
|
||||
export const useProjectForm = () => {
|
||||
return useContext(ProjectFormContext);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user