diff --git a/src/App.jsx b/src/App.jsx
index 7aba479..161a802 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1,11 +1,9 @@
-import "./styles/App.css";
-import Layout from "./components/layout/layout";
-import Login from "./pages/Login/Login";
-import Dashboard from "./pages/Dashboard/Dashboard";
-import CafeManagement from "./pages/CafeManagement/CafeManagement";
-import EditCafe from "./pages/CafeManagement/EditCafe";
-import Stats from "./pages/Stats/Stats";
-import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
+import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
+import Login, { ProtectedRoute } from './pages/Login/Login';
+import Layout from './components/Layout/Layout';
+import Dashboard from './pages/Dashboard/Dashboard';
+import CafeManagement from './pages/CafeManagement/CafeManagement';
+import EditCafe from './pages/CafeManagement/EditCafe';
function App() {
return (
@@ -13,17 +11,23 @@ function App() {
} />
- }>
- } />
-
+
+
+
+ }
+ >
} />
-
} />
-
} />
+ صفحه آمار و تحلیل} />
- } />
+ } />
+
+ } />
);
diff --git a/src/components/layout/layout.jsx b/src/components/layout/layout.jsx
index b081eea..b1b7466 100644
--- a/src/components/layout/layout.jsx
+++ b/src/components/layout/layout.jsx
@@ -1,41 +1,47 @@
import { Outlet, useLocation } from "react-router-dom";
-import Sidebar from "./sidebar";
-import Header from "./header";
+import Sidebar from "./Sidebar";
+import Header from "./Header";
export default function Layout() {
- const location = useLocation();
- const path = location.pathname;
+ const location = useLocation();
+ const path = location.pathname;
- // لیست الگوهایی که باید هدر نشون داده بشه
- const headerPaths = [
- "/management",
- "/management/",
- "/management/cafes",
- "/edit-cafe",
- "/edit-cafe/",
- "/cafe",
- "/cafes",
- "/admin",
- ];
+ // لیست الگوهایی که باید هدر نشون داده بشه
+ const headerPaths = [
+ "/management",
+ "/management/",
+ "/management/cafes",
+ "/edit-cafe",
+ "/edit-cafe/",
+ "/cafe",
+ "/cafes",
+ "/admin",
+ ];
- // بررسی: اگر هرکدوم از الگوها داخل path بود، هدر نمایش داده میشه
- const showHeader = headerPaths.some(
- (p) => path.startsWith(p) || path.includes(p)
- );
+ // بررسی: اگر هرکدوم از الگوها داخل path بود، هدر نمایش داده میشه
+ const showHeader = headerPaths.some(
+ (p) => path.startsWith(p) || path.includes(p)
+ );
- return (
-
-
-
-
+ return (
+
+ {/* Sidebar - در موبایل overlay، در دسکتاپ ثابت */}
+
-
- {showHeader &&
}
+ {/* Main Content */}
+
+ {showHeader && (
+
+
+
+ )}
-
-
-
-
-
- );
-}
+
+
+
+
+
+
+
+ );
+}
\ No newline at end of file
diff --git a/src/components/layout/sidebar.jsx b/src/components/layout/sidebar.jsx
index fe28a40..3152a90 100644
--- a/src/components/layout/sidebar.jsx
+++ b/src/components/layout/sidebar.jsx
@@ -1,109 +1,172 @@
-import React from "react";
+import React, { useState } from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import LogoDM from "../../assets/icons/LogoDM.svg";
import { BiBarChartAlt2 } from "react-icons/bi";
import { AiOutlinePieChart } from "react-icons/ai";
import { PiCoffee } from "react-icons/pi";
import { HiOutlineLogout } from "react-icons/hi";
+import { FiMenu, FiX } from "react-icons/fi";
const Sidebar = () => {
const location = useLocation();
const navigate = useNavigate();
+ const [isOpen, setIsOpen] = useState(false);
- // چک کردن صفحه فعال
const isActive = (path) => location.pathname === path;
- // تابع خروج
const handleLogout = () => {
- localStorage.removeItem("token");
- navigate("/login");
+ try {
+ localStorage.removeItem('accessToken');
+ localStorage.removeItem('refreshToken');
+ localStorage.removeItem('token');
+ localStorage.removeItem('adminInfo');
+
+ console.log('Tokens cleared from localStorage');
+ console.log('Current token:', localStorage.getItem('token'));
+
+ window.location.href = '/login';
+ } catch (error) {
+ console.error('Logout error:', error);
+ window.location.href = '/login';
+ }
};
+ const closeSidebar = () => setIsOpen(false);
+
return (
-
-
-
-
+ <>
+ setIsOpen(!isOpen)}
+ className="sm:hidden fixed top-3 right-3 z-[70] bg-[#7F4629] text-white p-2.5 rounded-lg shadow-lg"
+ aria-label="Toggle menu"
+ >
+ {isOpen ? : }
+
-
- {/* داشبورد */}
-
-
-
+ )}
+
+
+
+
+
+
+
+
- داشبورد
-
-
+
+
+ داشبورد
+
+
- {/* مدیریت کافهها */}
-
-
-
- مدیریت کافه ها
-
-
+
+
+ مدیریت کافه ها
+
+
- {/* آمار و تحلیل */}
-
-
-
- آمار و تحلیل
-
-
+
+
+ آمار و تحلیل
+
+
+
- {/* خروج */}
-
-
-
- خروج
-
-
-
-
+
+ {
+ handleLogout();
+ closeSidebar();
+ }}
+ className="group flex items-center gap-2.5 rounded-lg p-2.5 transition-all duration-200 hover:bg-[#7F4629] w-full"
+ >
+
+
+ خروج
+
+
+
+
+ >
);
};
diff --git a/src/pages/CafeManagement/CafeManagement.jsx b/src/pages/CafeManagement/CafeManagement.jsx
index 40ef93b..b536c5d 100644
--- a/src/pages/CafeManagement/CafeManagement.jsx
+++ b/src/pages/CafeManagement/CafeManagement.jsx
@@ -1,209 +1,182 @@
-import React from "react";
+import React, { useState, useEffect } from "react";
import { BiEdit } from "react-icons/bi";
-import Search from "../../assets/icons/search.svg";
-import Arrow from "../../assets/icons/arrow.svg";
-import Vector7 from "../../assets/icons/Vector7.svg";
-import Vector8 from "../../assets/icons/Vector8.svg";
+import axios from "axios";
import Vector9 from "../../assets/icons/Vector9.svg";
import Star1 from "../../assets/icons/Star1.svg";
import Group from "../../assets/icons/Group.svg";
-import Pic from "../../assets/icons/pic.png";
import Pic1 from "../../assets/icons/pic1.svg";
import { Link } from "react-router-dom";
-const cafes = [
- {
- id: 1,
- name: "کافه ترنج",
- location: "اصفهان - خیابان آذر",
- rating: 3.7,
- category: "دسته بندی",
- logo: Pic1,
- },
- {
- id: 2,
- name: "کافه سیب",
- location: "تهران - خیابان ولیعصر",
- rating: 4.2,
- category: "کافه سنتی",
- logo: Pic1,
- },
- {
- id: 3,
- name: "کافه داونتاون",
- location: "شیراز - خیابان زند",
- rating: 4.0,
- category: "کافه فرهنگی",
- logo: Pic1,
- },
-];
-
const CafeManagement = () => {
- return (
-
- {/* ===== Header ===== */}
- {/*
- مدیریت کافه
+ const [cafes, setCafes] = useState([]);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState("");
-
-
-
+ // دریافت لیست کافهها از API
+ useEffect(() => {
+ const fetchCafes = () => {
+ setLoading(true);
+ setError("");
-
-
-
سارا راد
-
-
+ axios
+ .get("https://cafeju.maksiran.ir/api/cafe/v1/get-cafe-list")
+ .then((response) => {
+ console.log("✅ Cafes loaded successfully:", response.data);
-
- */}
+ if (response.data.success && response.data.data) {
+ setCafes(response.data.data);
+ } else {
+ setError("دادههای دریافتی معتبر نیست");
+ }
+ setLoading(false);
+ })
+ .catch((error) => {
+ console.error("❌ Error loading cafes:", error);
+ setLoading(false);
- {/* ===== Add Button ===== */}
-
-
- افزودن شعبه جدید
-
-
+ if (error.response) {
+ setError(error.response.data.message || "خطا در دریافت لیست کافهها");
+ } else if (error.request) {
+ setError("خطا در برقراری ارتباط با سرور");
+ } else {
+ setError("خطای نامشخص رخ داده است");
+ }
+ });
+ };
-
+ fetchCafes();
+ }, []);
- {/* ===== Title ===== */}
- کافه های شما
-
- {/* ===== Table Header ===== */}
-
-
لوگو
- اسم
- لوکیشن
- ریتینگ
- دسته
- ادیت
-
-
- {/* ===== Table Rows ===== */}
-
- {cafes.map((cafe) => (
-
-
- {cafe.name}
- {cafe.location}
-
-
-
-
{cafe.rating}
+ return (
+
+ {/* دکمه افزودن */}
+
+
+ افزودن شعبه جدید
+
+
-
-
-
{cafe.category}
-
+ {/* عنوان */}
+ کافه های شما
- {/* ===== دکمه ادیت ===== */}
-
- ادیت کافه
-
-
-
- ))}
-
-
- );
+ {/* نمایش خطا */}
+ {error && (
+
+ {error}
+
+ )}
+
+ {/* نمایش لودینگ */}
+ {loading ? (
+
+ ) : (
+ <>
+ {/* هدر جدول */}
+
+
لوگو
+ اسم
+ آدرس
+ ریتینگ
+ ساعت کاری
+ ادیت
+
+
+ {/* ردیفهای جدول - دسکتاپ */}
+ {cafes.length > 0 ? (
+ <>
+ {/* نمای دسکتاپ */}
+
+ {cafes.map((cafe) => (
+
+ {/* لوگو */}
+
+
+ {/* نام */}
+ {cafe.Name}
+
+ {/* آدرس */}
+
+ {cafe.address}
+
+
+ {/* ریتینگ */}
+
+
+
{cafe.rating || 0}
+
+
+ {/* ساعت کاری */}
+
+
+
{cafe.openinghour || "نامشخص"}
+
+
+ {/* دکمه ادیت */}
+
+ ادیت کافه
+
+
+
+ ))}
+
+
+ {/* نمای موبایل - کارتها */}
+
+ {cafes.map((cafe) => (
+
+
+
+
+
{cafe.Name}
+
{cafe.address}
+
+
+
+
+
+
+
{cafe.rating || 0}
+
+
+
+
{cafe.openinghour || "نامشخص"}
+
+
+
+
+
ادیت کافه
+
+
+
+ ))}
+
+ >
+ ) : (
+
+ هیچ کافهای یافت نشد
+
+ )}
+ >
+ )}
+
+ );
};
-export default CafeManagement;
-
-// const CafeManagement = () => {
-// return (
-//
-//
-// مدیریت کافه
-//
-//
-//
-
-//
-//
-//
سارا راد
-//
-//
-//
-//
-
-//
-//
-// افزودن شعبه جدید
-//
-//
-//
-//
-
-// کافه های شما
-
-//
-//
لوگو
-// اسم
-// لوکیشن
-// ریتینگ
-// دسته
-// ادیت
-//
-
-//
-//
-//
کافه ترنج
-//
اصفهان - خیابان آذر
-//
-//
-//
3.7
-//
-//
-//
-//
دسته بندی
-//
-//
-//
ادیت کافه
-// {/*
*/}
-//
-//
-//
-//
کافه ترنج
-//
اصفهان - خیابان آذر
-//
-//
-//
3.7
-//
-//
-//
-//
دسته بندی
-//
-//
-//
ادیت کافه
-// {/*
*/}
-//
-//
-//
-//
کافه ترنج
-//
اصفهان - خیابان آذر
-//
-//
-//
3.7
-//
-//
-//
-
-//
دسته بندی
-//
-//
-//
ادیت کافه
-// {/*
*/}
-//
-//
-//
-//
-// );
-// };
-
-// export default CafeManagement;
+export default CafeManagement;
\ No newline at end of file
diff --git a/src/pages/CafeManagement/EditCafe.jsx b/src/pages/CafeManagement/EditCafe.jsx
index 4a48aed..157178d 100644
--- a/src/pages/CafeManagement/EditCafe.jsx
+++ b/src/pages/CafeManagement/EditCafe.jsx
@@ -1,11 +1,10 @@
import React, { useState, useEffect } from "react";
+import { useParams, useNavigate } from "react-router-dom";
+import axios from "axios";
import Bg1 from "../../assets/icons/bg1.svg";
import {GrLocation} from "react-icons/gr";
import {BiEdit} from "react-icons/bi";
import {FaRegStar} from "react-icons/fa";
-import {PiCoffee} from "react-icons/pi";
-import {IoMdTime} from "react-icons/io";
-import {LuCalendar1} from "react-icons/lu";
import Vector11 from "../../assets/icons/Vector11.svg";
import Vector12 from "../../assets/icons/Vector12.svg";
import Vector13 from "../../assets/icons/Vector13.svg";
@@ -18,11 +17,46 @@ import Coffee2 from "../../assets/icons/coffee2.svg";
import Coffee1 from "../../assets/icons/coffee1.svg";
import Coffee3 from "../../assets/icons/coffee3.svg";
import Edit from "../../assets/icons/edit.svg";
-import { MdDelete } from "react-icons/md";
import { IoMdCheckmark, IoMdClose } from "react-icons/io";
const EditCafe = () => {
- // State برای نگهداری عنوانها با localStorage
+ const { id } = useParams();
+ const navigate = useNavigate();
+
+ const [cafeData, setCafeData] = useState(null);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState("");
+
+ useEffect(() => {
+ if (id) {
+ setLoading(true);
+ setError("");
+
+ axios
+ .get(`https://cafeju.maksiran.ir/api/cafe/v1/get-cafe-profile-by-cafe/${id}`)
+ .then((response) => {
+ console.log("✅ Cafe data loaded:", response.data);
+ if (response.data.success && response.data.data) {
+ setCafeData(response.data.data);
+ } else {
+ setError("دادههای کافه معتبر نیست");
+ }
+ setLoading(false);
+ })
+ .catch((error) => {
+ console.error("❌ Error loading cafe:", error);
+ setLoading(false);
+ if (error.response) {
+ setError(error.response.data.message || "خطا در دریافت اطلاعات کافه");
+ } else if (error.request) {
+ setError("خطا در برقراری ارتباط با سرور");
+ } else {
+ setError("خطای نامشخص رخ داده است");
+ }
+ });
+ }
+ }, [id]);
+
const [categories, setCategories] = useState(() => {
const saved = localStorage.getItem('cafeCategories');
return saved ? JSON.parse(saved) : [
@@ -35,52 +69,40 @@ const EditCafe = () => {
];
});
- // ذخیره تغییرات در localStorage
useEffect(() => {
localStorage.setItem('cafeCategories', JSON.stringify(categories));
}, [categories]);
- // State برای فعال کردن حالت ویرایش
const [isEditMode, setIsEditMode] = useState(false);
-
- // State برای مدیریت حالت اضافه کردن
const [isAdding, setIsAdding] = useState(false);
const [newCategory, setNewCategory] = useState("");
-
- // State برای مدیریت حالت ویرایش
const [editingIndex, setEditingIndex] = useState(null);
const [editValue, setEditValue] = useState("");
- // تابع اضافه کردن عنوان جدید
const handleAddCategory = () => {
if (newCategory.trim()) {
setCategories([...categories, newCategory.trim()]);
setNewCategory("");
setIsAdding(false);
- // اسکرول رو به ابتدا (راست برای RTL) برمیگردونیم
setTimeout(() => {
const scrollContainer = document.querySelector('.categories-scroll');
if (scrollContainer) {
- // برای RTL باید scrollLeft رو به حداکثر ببریم
scrollContainer.scrollLeft = scrollContainer.scrollWidth;
}
}, 0);
}
};
- // تابع حذف عنوان
const handleDeleteCategory = (index) => {
const newCategories = categories.filter((_, i) => i !== index);
setCategories(newCategories);
};
- // تابع شروع ویرایش
const handleStartEdit = (index) => {
setEditingIndex(index);
setEditValue(categories[index]);
};
- // تابع ذخیره ویرایش
const handleSaveEdit = () => {
if (editValue.trim()) {
const newCategories = [...categories];
@@ -91,12 +113,37 @@ const EditCafe = () => {
}
};
- // تابع لغو ویرایش
const handleCancelEdit = () => {
setEditingIndex(null);
setEditValue("");
};
+ if (loading) {
+ return (
+
+ );
+ }
+
+ if (error) {
+ return (
+
+ );
+ }
+
+ if (!cafeData) {
+ return (
+
+ اطلاعات کافه یافت نشد
+
+ );
+ }
+
return (
<>
-
- ادیت کافه ترنج
+
+ ادیت {cafeData.Name}
-
-
-
+
+
+
-
-
+
+
+ onClick={() => navigate('/cafe-management')}
+ className="border-2 border-[#bb8f70] w-20 lg:w-25 h-10 rounded-3xl text-sm lg:text-base text-[#402e32] hover:bg-[#7f4629] hover:text-white transition-all duration-300 hover:border-none">
انصراف
+ className="border-2 border-[#bb8f70] bg-[#7f4629] text-white w-20 lg:w-25 h-10 rounded-3xl text-sm lg:text-base hover:bg-[#5f494f] transition-all duration-300 hover:border-none">
تایید
-
-
کافه ترنج
-
+
+
+
{cafeData.Name}
+
+
- اصفهان - خیابان آذر
+ {cafeData.address || "آدرس موجود نیست"}
-
+
- 3.9
+ {cafeData.rating || 0}
-
درباره کافه
-
- کافه ترنج، یک کافی شاپ با محیطی دنج و دلچسب در شهر اصفهان و یکی از
- ممتازترین کافی شاپهای ابن شهر است. از جمله خدمات این کافه میتوان
- به اینترنت رایگان و ارائه کتابهایی برای مطالعه در داخل کافه اشاره
- کرد.
-
-
-
+ درباره کافه
+
+ {cafeData.description || "توضیحاتی برای این کافه وجود ندارد."}
+
+
+
+
ویژگی ها
-
+
+ className="bg-[#e1d5c2] w-full lg:w-[140px] h-[137.6px] flex flex-col items-center gap-4 lg:gap-8 text-center rounded-2xl lg:rounded-4xl transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
-
+
منو کافه:
+ className="flex flex-col gap-3 lg:gap-4 text-[#402E32] font-medium w-full lg:w-[161.5px] h-[137.6px] bg-[#e1d5c2] rounded-2xl lg:rounded-4xl pr-3 text-sm lg:text-base transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
ساعت کاری:
23 - 8
+ className="flex flex-col gap-3 lg:gap-4 text-[#402E32] font-medium w-full lg:w-[161.5px] h-[137.6px] bg-[#e1d5c2] rounded-2xl lg:rounded-4xl pr-3 text-sm lg:text-base transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
رزرو :
رزرو آنلاین
+
+ className="flex flex-col gap-3 lg:gap-4 text-[#402E32] font-medium w-full lg:w-[170.5px] h-[137.6px] bg-[#e1d5c2] rounded-2xl lg:rounded-4xl pr-3 text-sm lg:text-base transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
موسیقی :
موسیقی زنده آخر هفته
+ className="flex flex-col gap-3 lg:gap-4 text-[#402E32] font-medium w-full lg:w-[161.5px] h-[137.6px] bg-[#e1d5c2] rounded-2xl lg:rounded-4xl pr-3 text-sm lg:text-base transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
پارکینگ :
عمومی
+ className="flex flex-col gap-3 lg:gap-4 text-[#402E32] font-medium w-full lg:w-[161.5px] h-[137.6px] bg-[#e1d5c2] rounded-2xl lg:rounded-4xl pr-3 text-sm lg:text-base transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
دسترسی آسان :
مناسب افراد ناتوان
+ className="flex justify-center items-center w-full lg:w-[150.5px] h-[137.6px] bg-[#5e5450] rounded-2xl lg:rounded-4xl transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
+
-
-
-
-
+
+
+
+
{
onClick={() => setIsEditMode(!isEditMode)}
title={isEditMode ? "خروج از حالت ویرایش" : "ویرایش عنوانها"}
/>
-
عنوان
+
عنوان
{!isEditMode ? (
- // حالت عادی - فقط نمایش عنوانها
<>
{categories.map((category, index) => (
-
{category}
+
{category}
))}
>
) : (
- // حالت ویرایش - نمایش دکمههای ویرایش و حذف بالای عنوان
<>
{categories.map((category, index) => (
{editingIndex === index ? (
- // حالت ویرایش
<>
{
/>
>
) : (
- // حالت عادی - آیکنها بالای عنوان
<>
-
+
handleStartEdit(index)}
title="ویرایش"
/>
- {category}
+ handleDeleteCategory(index)}
title="حذف"
/>
-
{category}
>
)}
@@ -286,13 +332,12 @@ const EditCafe = () => {
- {/* دکمه اضافه کردن عنوان جدید - بیرون از container اسکرول */}
{isEditMode && (
-
+
{!isAdding ? (
setIsAdding(true)}
- className="text-[#7f4629] hover:text-[#5f494f] font-bold transition-colors whitespace-nowrap w-full"
+ className="text-[#7f4629] hover:text-[#5f494f] font-bold transition-colors whitespace-nowrap w-full text-sm lg:text-base"
title="اضافه کردن عنوان جدید"
>
+ افزودن
@@ -306,7 +351,7 @@ const EditCafe = () => {
title="ذخیره"
/>
{
setIsAdding(false);
setNewCategory("");
@@ -328,35 +373,35 @@ const EditCafe = () => {
)}
-
+
-
+
افزودن زیر عنوان
-
+
قهوه ها
-
+
-
+
آیتم
-
+
-
اسپرسو100%
+ className="mt-8 lg:mt-13 flex items-center flex-col justify-center w-full lg:w-[320px] h-[391px] transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
+
اسپرسو100%
-
+
قیمت
118.000
-
+
45 میلی لیتر، قهوه، 100% عربیکا، دم شده با دستگاه اسپرسو ساز،
به همراه یک عدد آب معدنی مینی
@@ -364,16 +409,16 @@ const EditCafe = () => {
-
+ className="mt-8 lg:mt-10 flex items-center flex-col justify-center w-full lg:w-[320px] h-[391px] transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
+
کارامل ماکیاتو
-
+
قیمت
149.000
-
+
220 میلی لیتر، 2 شات اسپرسو 30% روبوستا، 70% عربیکا، یک لکه
فوم شیر، سیروپ کارامل
@@ -381,72 +426,20 @@ const EditCafe = () => {
-
+ className="mt-8 lg:mt-10 flex items-center flex-col justify-center w-full lg:w-[320px] h-[391px] transform hover:-translate-y-1 transition-all duration-300 hover:shadow-md">
+
اسپرسو آفوگاتو
-
+
قیمت
118.000
+ className="font-light text-[#66585b] mt-2 mb-10 break-words px-4 lg:-mr-23 text-xs lg:text-[14.90px]">
اسپرسو، یک اسکوپ بستنی وانیلی
-
-
-
-
-
- اسپرسو آفوگاتو
-
-
-
- قیمت
- 118.000
-
-
- اسپرسو، یک اسکوپ بستنی وانیلی
-
-
-
-
-
-
- اسپرسو آفوگاتو
-
-
-
- قیمت
- 118.000
-
-
- اسپرسو، یک اسکوپ بستنی وانیلی
-
-
-
-
-
-
- اسپرسو آفوگاتو
-
-
-
- قیمت
- 118.000
-
-
-
- اسپرسو، یک اسکوپ بستنی وانیلی
-
-
+
diff --git a/src/pages/Login/Login.jsx b/src/pages/Login/Login.jsx
index 904c90a..0c12f8d 100644
--- a/src/pages/Login/Login.jsx
+++ b/src/pages/Login/Login.jsx
@@ -1,65 +1,81 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect } from 'react';
import Loginpic from '../../assets/image/loginpic.jpg';
import { FiLock } from 'react-icons/fi';
-import { HiOutlineDevicePhoneMobile } from "react-icons/hi2";
+import { FaRegUser } from "react-icons/fa6";
import axios from 'axios';
import LogoDM from "../../assets/icons/LogoDM.svg";
+import { useNavigate, Navigate } from 'react-router-dom';
+
+// تنظیم base URL برای axios
+axios.defaults.baseURL = 'https://cafeju.maksiran.ir';
const Login = () => {
- const [phoneNumber, setPhoneNumber] = useState('');
+ const [userName, setUserName] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
+ const navigate = useNavigate();
- const handlePhoneChange = (e) => {
- const value = e.target.value;
- // فقط اجازه ورود اعداد
- if (value === '' || /^[0-9\b]+$/.test(value)) {
- // محدود کردن به 11 رقم
- if (value.length <= 11) {
- setPhoneNumber(value);
- }
+ // چک کردن token موقع لود شدن صفحه
+ useEffect(() => {
+ const token = localStorage.getItem('token');
+ // اگر قبلاً لاگین کرده، به داشبورد هدایت میشه
+ if (token) {
+ navigate('/dashboard');
}
- };
+ }, [navigate]);
const handleLogin = (e) => {
e.preventDefault();
setError('');
setLoading(true);
- // آمادهسازی داده برای ارسال
const loginData = {
- userName: phoneNumber,
+ userName: userName,
password: password
};
- // ارسال درخواست به API
+ console.log('Sending login request with:', loginData);
+
axios.post('/api/admin/v1/login', loginData)
.then((response) => {
setLoading(false);
- console.log('Login successful:', response.data);
+ console.log('✅ Login successful!');
+ console.log('Response:', response.data);
- // ذخیره توکن در localStorage
- if (response.data.token) {
- localStorage.setItem('token', response.data.token);
+ if (response.data.success && response.data.data && response.data.data.tokens) {
+ const accessToken = response.data.data.tokens.accessToken;
+ const refreshToken = response.data.data.tokens.refreshToken;
+
+ localStorage.setItem('accessToken', accessToken);
+ localStorage.setItem('refreshToken', refreshToken);
+ localStorage.setItem('token', accessToken);
+
+ localStorage.setItem('adminInfo', JSON.stringify(response.data.data.admin));
+
+ console.log('Tokens saved to localStorage');
+ console.log('Admin info:', response.data.data.admin);
+
+ console.log('Navigating to dashboard...');
+ navigate('/dashboard');
+ } else {
+ console.error('❌ Invalid response format');
+ setError('پاسخ سرور معتبر نیست');
}
-
- // هدایت به صفحه داشبورد یا صفحه اصلی
- // window.location.href = '/dashboard';
- alert('ورود موفقیتآمیز بود!');
})
.catch((error) => {
setLoading(false);
- console.error('Login error:', error);
+ console.error('❌ Login error:', error);
- // مدیریت خطاها
if (error.response) {
- // سرور پاسخ داد اما با خطا
- setError(error.response.data.message || 'نام کاربری یا رمز عبور اشتباه است');
+ console.error('Response status:', error.response.status);
+ console.error('Response data:', error.response.data);
+ setError(error.response.data.message || error.response.data.en_message || 'نام کاربری یا رمز عبور اشتباه است');
} else if (error.request) {
- // درخواست ارسال شد اما پاسخی دریافت نشد
+ console.error('No response received from server');
setError('خطا در برقراری ارتباط با سرور');
} else {
+ console.error('Error setting up request');
setError('خطای نامشخص رخ داده است');
}
});
@@ -134,7 +150,6 @@ const Login = () => {