Files
SweetHome/app/profile/page.js

371 lines
13 KiB
JavaScript
Raw Normal View History

2026-06-24 00:15:21 +03:00
"use client";
2026-06-24 00:15:21 +03:00
import { useState, useEffect } from "react";
import { motion } from "framer-motion";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import {
User,
Mail,
Phone,
MessageCircle,
2026-06-17 06:01:24 -07:00
Check,
X,
ArrowLeft,
Building,
Home,
Calendar,
MapPin,
Loader2,
2026-06-24 00:15:21 +03:00
Pencil,
} from "lucide-react";
import toast, { Toaster } from "react-hot-toast";
import AuthService from "../services/AuthService";
import { getCustomerByUserId, getOwnerByUserId } from "../utils/api";
export default function ProfilePage() {
const { t, i18n } = useTranslation();
const router = useRouter();
const [user, setUser] = useState(null);
const [isLoading, setIsLoading] = useState(true);
2026-06-17 06:01:24 -07:00
const [editingBio, setEditingBio] = useState(false);
2026-06-24 00:15:21 +03:00
const [bioDraft, setBioDraft] = useState("");
2026-06-17 06:01:24 -07:00
const [formData, setFormData] = useState({
2026-06-24 00:15:21 +03:00
name: "",
email: "",
phone: "",
whatsapp: "",
bio: "",
location: "",
joinedDate: "",
});
2026-06-17 06:01:24 -07:00
2026-06-24 00:15:21 +03:00
const [avatarPreview, setAvatarPreview] = useState("");
useEffect(() => {
const authUser = AuthService.getUser();
if (authUser) {
const userData = {
id: authUser.id,
2026-06-24 00:15:21 +03:00
name: authUser.name || "",
email: authUser.email || "",
phone: authUser.phone || "",
role: AuthService.isOwner() ? "owner" : "customer",
};
setUser(userData);
async function fetchProfile() {
try {
2026-06-24 00:15:21 +03:00
const fetchFn =
userData.role === "owner" ? getOwnerByUserId : getCustomerByUserId;
const profile = await fetchFn(userData.id);
if (profile) {
const profileData = {
2026-06-24 00:15:21 +03:00
name:
profile.fullName ||
profile.name ||
`${profile.firstName || ""} ${profile.lastName || ""}`.trim() ||
userData.name ||
"",
email: profile.email || userData.email || "",
phone:
profile.phone || profile.phoneNumber || userData.phone || "",
whatsapp: profile.whatsAppNumber || profile.whatsapp || "",
bio: profile.bio || "",
location: profile.address || profile.location || "",
joinedDate: profile.createdAt
? new Date(profile.createdAt).toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', {
2026-06-24 00:15:21 +03:00
month: "long",
year: "numeric",
})
: new Date().toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', {
2026-06-24 00:15:21 +03:00
month: "long",
year: "numeric",
}),
};
setFormData(profileData);
2026-06-17 06:01:24 -07:00
setBioDraft(profileData.bio);
2026-06-24 00:15:21 +03:00
localStorage.setItem("userProfile", JSON.stringify(profileData));
setIsLoading(false);
return;
}
} catch (err) {
2026-06-17 06:01:24 -07:00
// Ignore API errors and fall back to local data.
}
2026-06-24 00:15:21 +03:00
const savedProfile = localStorage.getItem("userProfile");
let profileData;
if (savedProfile) {
profileData = JSON.parse(savedProfile);
} else {
profileData = {
2026-06-24 00:15:21 +03:00
name: userData.name || "",
email: userData.email || "",
phone: "",
whatsapp: "",
bio: "",
location: "",
joinedDate: new Date().toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', {
2026-06-24 00:15:21 +03:00
month: "long",
year: "numeric",
}),
};
}
setFormData(profileData);
2026-06-24 00:15:21 +03:00
setBioDraft(profileData.bio || "");
setIsLoading(false);
}
2026-06-24 00:15:21 +03:00
const savedAvatar = localStorage.getItem("userAvatar");
if (savedAvatar) {
setAvatarPreview(savedAvatar);
}
fetchProfile();
} else {
2026-06-24 00:15:21 +03:00
router.push("/login");
}
}, [router]);
2026-06-17 06:01:24 -07:00
const startBioEditing = () => {
2026-06-24 00:15:21 +03:00
setBioDraft(formData.bio || "");
2026-06-17 06:01:24 -07:00
setEditingBio(true);
};
2026-06-17 06:01:24 -07:00
const cancelBioEditing = () => {
2026-06-24 00:15:21 +03:00
setBioDraft(formData.bio || "");
2026-06-17 06:01:24 -07:00
setEditingBio(false);
};
2026-06-17 06:01:24 -07:00
const saveBio = () => {
const updatedData = { ...formData, bio: bioDraft };
setFormData(updatedData);
2026-06-24 00:15:21 +03:00
localStorage.setItem("userProfile", JSON.stringify(updatedData));
2026-06-17 06:01:24 -07:00
setEditingBio(false);
toast.success(t('bio-updated-success'));
};
const fadeInUp = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0 },
2026-06-24 00:15:21 +03:00
transition: { duration: 0.5 },
};
if (isLoading) {
return (
<div className="min-h-screen bg-gray-50 flex items-center justify-center">
<div className="text-center">
<Loader2 className="w-12 h-12 text-amber-500 animate-spin mx-auto mb-4" />
<p className="text-gray-600">{t('loading-profile')}</p>
</div>
</div>
);
}
return (
<div dir={i18n.language === 'ar' ? 'rtl' : 'ltr'} className="min-h-screen bg-gray-50 py-8">
<Toaster position="top-center" reverseOrder={false} />
2026-06-17 06:01:24 -07:00
<div className="container mx-auto px-4 max-w-4xl">
<motion.div
initial={{ opacity: 0, y: -20 }}
animate={{ opacity: 1, y: 0 }}
className="flex justify-between items-center mb-6"
>
<Link
href="/"
className="flex items-center gap-2 text-gray-600 hover:text-amber-600 transition-colors"
>
<ArrowLeft className="w-5 h-5" />
<span>{t('backToHome')}</span>
</Link>
</motion.div>
<motion.div
variants={fadeInUp}
initial="initial"
animate="animate"
className="bg-white rounded-3xl shadow-xl overflow-hidden"
>
<div className="h-48 bg-gradient-to-r from-amber-500 to-amber-600 relative overflow-hidden">
<motion.div
className="absolute inset-0 bg-white/10"
animate={{
2026-06-24 00:15:21 +03:00
x: ["-100%", "100%"],
}}
transition={{
duration: 3,
repeat: Infinity,
2026-06-24 00:15:21 +03:00
ease: "linear",
}}
/>
</div>
<div className="px-8 pb-8">
<div className="flex justify-center -mt-16 mb-6">
<div className="relative group">
2026-06-17 06:01:24 -07:00
<div className="w-32 h-32 rounded-full border-4 border-white bg-gradient-to-br from-amber-500 to-amber-600 flex items-center justify-center text-white text-4xl font-bold shadow-xl overflow-hidden">
{avatarPreview ? (
<img
src={avatarPreview}
alt={formData.name}
className="w-full h-full object-cover"
/>
) : (
2026-06-24 00:15:21 +03:00
formData.name?.charAt(0).toUpperCase() || "U"
)}
</div>
</div>
</div>
<div className="text-center mb-6">
<div className="flex items-center justify-center gap-2">
2026-06-24 00:15:21 +03:00
<h1 className="text-3xl font-bold text-gray-900">
{formData.name}
</h1>
</div>
2026-06-17 06:01:24 -07:00
<div className="flex items-center justify-center gap-2 text-gray-500 mt-2">
<MapPin className="w-4 h-4" />
<span>{formData.location || t('addressNotSpecified')}</span>
</div>
2026-06-17 06:01:24 -07:00
<div className="flex items-center justify-center gap-2 text-gray-500 mt-1">
<Calendar className="w-4 h-4" />
<span> {t('member-since')} {formData.joinedDate}</span>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div className="bg-gray-50 p-4 rounded-xl group">
<div className="flex justify-between items-start mb-2">
<label className="text-sm font-medium text-gray-600">
{t('emailLabel')}
</label>
</div>
2026-06-17 06:01:24 -07:00
<div className="flex items-center gap-2 text-gray-900">
<Mail className="w-5 h-5 text-gray-400" />
<span>{formData.email}</span>
</div>
</div>
<div className="bg-gray-50 p-4 rounded-xl group">
<div className="flex justify-between items-start mb-2">
<label className="text-sm font-medium text-gray-600">
{t('phoneLabel')}
</label>
</div>
2026-06-17 06:01:24 -07:00
<div className="flex items-center gap-2 text-gray-900">
<Phone className="w-5 h-5 text-gray-400" />
<span>{formData.phone || t('not-specified')}</span>
2026-06-17 06:01:24 -07:00
</div>
</div>
<div className="bg-gray-50 p-4 rounded-xl group">
<div className="flex justify-between items-start mb-2">
<label className="text-sm font-medium text-gray-600">
{t('whatsapp-label')}
</label>
</div>
2026-06-17 06:01:24 -07:00
<div className="flex items-center gap-2 text-gray-900">
<MessageCircle className="w-5 h-5 text-gray-400" />
<span>{formData.whatsapp || t('not-specified')}</span>
2026-06-17 06:01:24 -07:00
</div>
</div>
<div className="bg-gray-50 p-4 rounded-xl">
<label className="block text-sm font-medium text-gray-600 mb-2">
{t('account-type')}
</label>
<div className="flex items-center gap-2">
2026-06-24 00:15:21 +03:00
{user?.role === "owner" ? (
<>
<Building className="w-5 h-5 text-amber-500" />
<span className="text-gray-900">{t('property-owner')}</span>
</>
) : (
<>
<Home className="w-5 h-5 text-blue-500" />
<span className="text-gray-900">{t('customer')}</span>
</>
)}
</div>
</div>
</div>
2026-06-24 00:15:21 +03:00
{/* noo need for the descption of me on the profile*/}
{/* <div className="mt-6 bg-gray-50 p-4 rounded-xl group">
<div className="flex justify-between items-start mb-2">
<label className="text-sm font-medium text-gray-600">
نبذة عني
</label>
2026-06-17 06:01:24 -07:00
{!editingBio && (
<button
2026-06-17 06:01:24 -07:00
onClick={startBioEditing}
className="opacity-0 group-hover:opacity-100 transition-opacity text-gray-400 hover:text-amber-500"
>
<Pencil className="w-4 h-4" />
</button>
)}
</div>
2026-06-17 06:01:24 -07:00
{editingBio ? (
<div className="space-y-2">
<textarea
2026-06-17 06:01:24 -07:00
value={bioDraft}
onChange={(e) => setBioDraft(e.target.value)}
rows="4"
2026-06-17 06:01:24 -07:00
autoFocus
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-amber-500 focus:border-transparent"
placeholder="اكتب نبذة عن نفسك..."
/>
<div className="flex justify-end gap-2">
<button
2026-06-17 06:01:24 -07:00
onClick={saveBio}
className="px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 flex items-center gap-2"
>
<Check className="w-4 h-4" />
حفظ
</button>
<button
2026-06-17 06:01:24 -07:00
onClick={cancelBioEditing}
className="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 flex items-center gap-2"
>
<X className="w-4 h-4" />
إلغاء
</button>
</div>
</div>
) : (
<p className="text-gray-700 leading-relaxed">
{formData.bio || 'لا توجد نبذة تعريفية بعد'}
</p>
)}
2026-06-24 00:15:21 +03:00
</div> */}
2026-06-24 00:15:21 +03:00
{user?.role === "owner" && (
<div className="grid grid-cols-3 gap-4 mt-6">
<div className="bg-amber-50 p-4 rounded-xl text-center">
<div className="text-2xl font-bold text-amber-600">12</div>
<div className="text-xs text-gray-600">{t('propertiesListed')}</div>
</div>
<div className="bg-blue-50 p-4 rounded-xl text-center">
<div className="text-2xl font-bold text-blue-600">8</div>
<div className="text-xs text-gray-600">{t('citiesCovered')}</div>
</div>
<div className="bg-green-50 p-4 rounded-xl text-center">
<div className="text-2xl font-bold text-green-600">4.8</div>
<div className="text-xs text-gray-600">{t('customerSatisfaction')}</div>
</div>
</div>
)}
</div>
</motion.div>
</div>
</div>
);
2026-06-24 00:15:21 +03:00
}