Initial commit - modstagram-next
This commit is contained in:
113
src/contexts/BillboardFormContext.tsx
Normal file
113
src/contexts/BillboardFormContext.tsx
Normal file
@@ -0,0 +1,113 @@
|
||||
"use client";
|
||||
|
||||
import { IFeature, Service } from "@/types/types";
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
|
||||
interface BillboardFormData {
|
||||
stateId: string | null;
|
||||
cityId: string | null;
|
||||
categoryId: string;
|
||||
adsTitle: string;
|
||||
description: string | null;
|
||||
neighbourhood: string | null;
|
||||
address: string | null;
|
||||
markerCoordinate: string[] | [] | null;
|
||||
images: string[];
|
||||
services: Service[];
|
||||
selectedFeatures: IFeature[];
|
||||
landline: string | null;
|
||||
mobile: string | null;
|
||||
telegram: string | null;
|
||||
whatsapp: string | null;
|
||||
instagram: string | null;
|
||||
saveData: boolean | null;
|
||||
selectedType: string;
|
||||
showDiscount: null | boolean;
|
||||
_id?: string;
|
||||
}
|
||||
|
||||
const initialFormData: BillboardFormData = {
|
||||
stateId: "",
|
||||
cityId: "",
|
||||
categoryId: "",
|
||||
adsTitle: "",
|
||||
description: "",
|
||||
neighbourhood: "",
|
||||
address: "",
|
||||
markerCoordinate: [],
|
||||
images: [],
|
||||
services: [],
|
||||
selectedFeatures: [],
|
||||
landline: "",
|
||||
mobile: "",
|
||||
telegram: "",
|
||||
whatsapp: "",
|
||||
instagram: "",
|
||||
saveData: null,
|
||||
selectedType: "",
|
||||
showDiscount: false,
|
||||
_id: "",
|
||||
};
|
||||
|
||||
// 📌 ایجاد Context
|
||||
const BillboardFormContext = createContext<{
|
||||
formData: BillboardFormData;
|
||||
updateForm: (data: Partial<BillboardFormData>) => void;
|
||||
resetForm: () => void;
|
||||
setIsEditing: (editing: boolean) => void;
|
||||
isEditing: boolean;
|
||||
}>(null!);
|
||||
|
||||
export const BillboardFormProvider = ({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [formData, setFormData] = useState<BillboardFormData>(() => {
|
||||
if (typeof window !== "undefined" && !isEditing) {
|
||||
const storedData = localStorage.getItem("billboardForm");
|
||||
if (storedData) {
|
||||
const { data, timestamp } = JSON.parse(storedData);
|
||||
const now = new Date().getTime();
|
||||
if (now - timestamp < 3 * 60 * 1000) {
|
||||
return data;
|
||||
} else {
|
||||
localStorage.removeItem("billboardForm");
|
||||
}
|
||||
}
|
||||
}
|
||||
return initialFormData;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!isEditing) {
|
||||
const dataToStore = {
|
||||
data: formData,
|
||||
timestamp: new Date().getTime(),
|
||||
};
|
||||
localStorage.setItem("billboardForm", JSON.stringify(dataToStore));
|
||||
}
|
||||
}, [formData, isEditing]);
|
||||
|
||||
const updateForm = (data: Partial<BillboardFormData>) => {
|
||||
setFormData((prev) => ({ ...prev, ...data }));
|
||||
};
|
||||
|
||||
const resetForm = () => {
|
||||
setFormData(initialFormData);
|
||||
localStorage.removeItem("billboardForm");
|
||||
};
|
||||
|
||||
return (
|
||||
<BillboardFormContext.Provider
|
||||
value={{ formData, updateForm, resetForm, setIsEditing, isEditing }}
|
||||
>
|
||||
{children}
|
||||
</BillboardFormContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useBillboardForm = () => {
|
||||
return useContext(BillboardFormContext);
|
||||
};
|
||||
111
src/contexts/ProjectFormContext.tsx
Normal file
111
src/contexts/ProjectFormContext.tsx
Normal file
@@ -0,0 +1,111 @@
|
||||
"use client";
|
||||
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
|
||||
// 📌 نوع دادههای فرم
|
||||
interface ProjectFormData {
|
||||
userId: string | null;
|
||||
expertise: string | null;
|
||||
subExpertise: string[];
|
||||
gender: string;
|
||||
age: string;
|
||||
publicType: string;
|
||||
stateId: string;
|
||||
cityId: string;
|
||||
projTitle: string;
|
||||
projectTime: string;
|
||||
offerPrice: string;
|
||||
description: string;
|
||||
numberOfPerson: string;
|
||||
selectedType: string;
|
||||
price2: string;
|
||||
_id: string;
|
||||
}
|
||||
|
||||
// 📌 مقدار اولیه فرم
|
||||
const initialFormData: ProjectFormData = {
|
||||
expertise: null,
|
||||
subExpertise: [],
|
||||
gender: "",
|
||||
age: "",
|
||||
publicType: "",
|
||||
stateId: "",
|
||||
cityId: "",
|
||||
projTitle: "",
|
||||
projectTime: "",
|
||||
offerPrice: "",
|
||||
description: "",
|
||||
numberOfPerson: "",
|
||||
selectedType: "normal",
|
||||
price2: "",
|
||||
userId: null,
|
||||
_id: "",
|
||||
};
|
||||
|
||||
// 📌 ایجاد Context
|
||||
const ProjectFormContext = createContext<{
|
||||
formData: ProjectFormData;
|
||||
updateForm: (data: Partial<ProjectFormData>) => void;
|
||||
resetForm: () => void;
|
||||
setIsEditing: (editing: boolean) => void;
|
||||
isEditing: boolean;
|
||||
}>(null!);
|
||||
|
||||
// 📌 Provider برای فرم
|
||||
export const ProjectFormProvider = ({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [formData, setFormData] = useState<ProjectFormData>(() => {
|
||||
if (typeof window !== "undefined" && !isEditing) {
|
||||
const storedData = localStorage.getItem("projectForm");
|
||||
if (storedData) {
|
||||
const { data, timestamp } = JSON.parse(storedData);
|
||||
const now = new Date().getTime();
|
||||
if (now - timestamp < 10 * 60 * 1000) {
|
||||
return data;
|
||||
} else {
|
||||
localStorage.removeItem("projectForm");
|
||||
}
|
||||
}
|
||||
}
|
||||
return initialFormData;
|
||||
});
|
||||
|
||||
// 📌 ذخیره فرم در localStorage
|
||||
useEffect(() => {
|
||||
if (!isEditing) {
|
||||
const dataToStore = {
|
||||
data: formData,
|
||||
timestamp: new Date().getTime(),
|
||||
};
|
||||
localStorage.setItem("projectForm", JSON.stringify(dataToStore));
|
||||
}
|
||||
}, [formData, isEditing]);
|
||||
|
||||
// 📌 تابع برای آپدیت فرم
|
||||
const updateForm = (data: Partial<ProjectFormData>) => {
|
||||
setFormData((prev) => ({ ...prev, ...data }));
|
||||
};
|
||||
|
||||
// 📌 ریست فرم
|
||||
const resetForm = () => {
|
||||
setFormData(initialFormData);
|
||||
localStorage.removeItem("projectForm");
|
||||
};
|
||||
|
||||
return (
|
||||
<ProjectFormContext.Provider
|
||||
value={{ formData, updateForm, resetForm, setIsEditing, isEditing }}
|
||||
>
|
||||
{children}
|
||||
</ProjectFormContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// 📌 هوک برای استفاده از فرم
|
||||
export const useProjectForm = () => {
|
||||
return useContext(ProjectFormContext);
|
||||
};
|
||||
55
src/contexts/ThemeContext.tsx
Normal file
55
src/contexts/ThemeContext.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
|
||||
interface ThemeContextType {
|
||||
isDark: boolean;
|
||||
toggleTheme: () => void;
|
||||
}
|
||||
|
||||
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
||||
|
||||
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
||||
const [isDark, setIsDark] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const savedTheme = localStorage.getItem('theme');
|
||||
if (savedTheme === 'dark') {
|
||||
document.documentElement.classList.add('dark');
|
||||
setIsDark(true);
|
||||
} else if (savedTheme === 'light') {
|
||||
document.documentElement.classList.remove('dark');
|
||||
setIsDark(false);
|
||||
} else {
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
if (prefersDark) {
|
||||
document.documentElement.classList.add('dark');
|
||||
setIsDark(true);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const toggleTheme = () => {
|
||||
if (document.documentElement.classList.contains('dark')) {
|
||||
document.documentElement.classList.remove('dark');
|
||||
localStorage.setItem('theme', 'light');
|
||||
setIsDark(false);
|
||||
} else {
|
||||
document.documentElement.classList.add('dark');
|
||||
localStorage.setItem('theme', 'dark');
|
||||
setIsDark(true);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ isDark, toggleTheme }}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
const context = useContext(ThemeContext);
|
||||
if (!context) throw new Error('useTheme must be used within ThemeProvider');
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user