import React, { useState } from "react"; import { Modal } from "antd"; import { Link } from "react-router-dom"; import { User, LogIn, Wallet, Heart, Languages, List, Mail, Info, Edit, MapPin, LogOut, } from "lucide-react"; import { useTranslation } from "react-i18next"; import styles from "./ProfileMenu.module.scss"; import LoginModal from "../LogIn"; import SignUpModal from "../SignUp"; import ProfileModal from "..//MyProfileModal/index"; import tm from "../../assets/tm.png"; import ru from "../../assets/ru.png"; import en from "../../assets/en.png"; import { useAuth } from "../../context/authContext"; import { useGetProfileQuery } from "../../app/api/myProfileApi"; const ProfileMenu = () => { const [activeModal, setActiveModal] = useState(null); const { t, i18n } = useTranslation(); const { isAuthenticated, logout } = useAuth(); // Fetch profile data from API const { data: profileData, isLoading } = useGetProfileQuery(undefined, { skip: !isAuthenticated, // Skip the API call if not authenticated }); // Extract user data from API response const userData = profileData?.data || { first_name: "", last_name: "", phone_number: "", address: "", }; const languages = [ { code: "tk", img: tm, name: "Türkmen" }, { code: "ru", img: ru, name: "Русский" }, { code: "en", img: en, name: "English" }, ]; const handleMenuClick = (item) => { if (item.action === "logout") { logout(); return; } if (item.action) { setActiveModal(item.action); } }; const handleLanguageChange = async (langCode) => { await i18n.changeLanguage(langCode); localStorage.setItem("preferredLanguage", langCode); setActiveModal(null); window.location.reload(); }; // Handle edit profile click const handleEditProfile = () => { setActiveModal("editProfile"); }; // Close any modal const handleCloseModal = () => { setActiveModal(null); }; // Render authenticated profile view const renderAuthenticatedProfile = () => { const menuItems = [ // { icon: , text: t("profile.my_address"), path: "/addresses" }, { icon: , text: t("profile.orders"), path: "/orders" }, { icon: , text: t("profile.favorites"), path: "/wishlist" }, { icon: , text: t("profile.language"), action: "language" }, { icon: , text: t("profile.delivery"), path: "/delivery-and-payment", }, { icon: , text: t("profile.contact"), path: "/contactus" }, { icon: , text: t("profile.about"), path: "/about-us" }, { icon: , text: t("profile.logout"), action: "logout" }, ]; return (
{/* User profile header */}
+993 {userData.phone_number}
{/* First name section */}
{t("profile.name")}
{userData.first_name || "Registered User"}
{/* Last name section */}
{t("profile.lastname")}
{userData.last_name || "Registered User"}
{/* Address section - Add this new section */}
{t("profile.address")}
{userData.address || "Ashgabat"}
{/* Menu items */}
{menuItems.map((item, index) => renderMenuItem(item, index))}
{/* Language Modal */}
{languages.map((lang) => ( ))}
{/* Edit Profile Modal */}
); }; // Render unauthenticated menu const renderUnauthenticatedMenu = () => { const menuItems = [ // { icon: , text: t("profile.registration"), action: "signUp" }, { icon: , text: t("profile.login"), action: "login" }, { icon: , text: t("profile.orders"), path: "/orders" }, { icon: , text: t("profile.favorites"), path: "/wishlist" }, { icon: , text: t("profile.language"), action: "language" }, { icon: , text: t("profile.delivery"), path: "/delivery-and-payment", }, { icon: , text: t("profile.contact"), path: "/contactus" }, { icon: , text: t("profile.about"), path: "/about-us" }, ]; return (
{menuItems.map((item, index) => renderMenuItem(item, index))}
{/* Login/Signup Modals */} {/* Language Modal */}
{languages.map((lang) => ( ))}
); }; const renderMenuItem = (item, index) => { const content = ( <> {item.icon} {item.text} ); if (item.path) { return ( {content} ); } return ( ); }; // Show loading state while fetching profile data if (isAuthenticated && isLoading) { return
Loading profile...
; } // Render different views based on authentication state return isAuthenticated ? renderAuthenticatedProfile() : renderUnauthenticatedMenu(); }; export default ProfileMenu;