diff --git a/frontend/src/app/dashboard/profile/page.tsx b/frontend/src/app/dashboard/profile/page.tsx index 38fbb3a..28fa780 100644 --- a/frontend/src/app/dashboard/profile/page.tsx +++ b/frontend/src/app/dashboard/profile/page.tsx @@ -2,19 +2,23 @@ import { ProfilePictureUpload } from "@/components/profile-picture-upload-v2" import { Button } from "@/components/ui/button" -import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" +import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" -import { Save, Loader2 } from "lucide-react" +import { Save, Loader2, Building2 } from "lucide-react" import { useEffect, useState } from "react" -import { profileApi, authApi } from "@/lib/api" +import { profileApi, authApi, adminCompaniesApi, type AdminCompany } from "@/lib/api" import { toast } from "sonner" +import { getCurrentUser, isAdminUser } from "@/lib/auth" export default function ProfilePage() { const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) + const [savingCompany, setSavingCompany] = useState(false) const [user, setUser] = useState(null) + const [company, setCompany] = useState(null) + const [isAdmin, setIsAdmin] = useState(false) const [formData, setFormData] = useState({ fullName: "", @@ -23,7 +27,17 @@ export default function ProfilePage() { bio: "", }) + const [companyData, setCompanyData] = useState({ + name: "", + email: "", + phone: "", + website: "", + description: "", + }) + useEffect(() => { + const currentUser = getCurrentUser() + setIsAdmin(isAdminUser(currentUser)) loadProfile() }, []) @@ -34,11 +48,31 @@ export default function ProfilePage() { console.log("[PROFILE_FLOW] Profile loaded:", userData) setUser(userData) setFormData({ - fullName: userData.fullName || "", + fullName: userData.fullName || userData.name || "", email: userData.email || "", phone: userData.phone || "", bio: userData.bio || "" }) + + // Load company if user has companyId + if (userData.companyId) { + try { + const companiesRes = await adminCompaniesApi.list() + const userCompany = companiesRes.data.find(c => c.id === userData.companyId) + if (userCompany) { + setCompany(userCompany) + setCompanyData({ + name: userCompany.name || "", + email: userCompany.email || "", + phone: userCompany.phone || "", + website: userCompany.website || "", + description: userCompany.description || "", + }) + } + } catch (err) { + console.warn("[PROFILE_FLOW] Could not load company:", err) + } + } } catch (error) { console.error("[PROFILE_FLOW] Error loading profile:", error) toast.error("Failed to load profile") @@ -51,6 +85,10 @@ export default function ProfilePage() { setFormData(prev => ({ ...prev, [field]: value })) } + const handleCompanyChange = (field: string, value: string) => { + setCompanyData(prev => ({ ...prev, [field]: value })) + } + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setSaving(true) @@ -71,6 +109,30 @@ export default function ProfilePage() { } } + const handleCompanySubmit = async (e: React.FormEvent) => { + e.preventDefault() + if (!company) return + setSavingCompany(true) + console.log("[PROFILE_FLOW] Updating company:", companyData) + try { + await adminCompaniesApi.update(company.id, { + name: companyData.name, + email: companyData.email, + phone: companyData.phone, + website: companyData.website, + description: companyData.description, + }) + console.log("[PROFILE_FLOW] Company updated successfully") + toast.success("Company updated") + loadProfile() + } catch (error) { + console.error("[PROFILE_FLOW] Error updating company:", error) + toast.error("Failed to update company") + } finally { + setSavingCompany(false) + } + } + if (loading) { return (
@@ -80,7 +142,8 @@ export default function ProfilePage() { } return ( -
+
+ {/* Profile Card */} @@ -155,6 +218,79 @@ export default function ProfilePage() { + + {/* Company Card - Only for Admin users who have a company */} + {isAdmin && company && ( + + +
+ + Company Information +
+ Edit your company details +
+ +
+
+
+ + handleCompanyChange("name", e.target.value)} + /> +
+
+ + handleCompanyChange("email", e.target.value)} + /> +
+
+ +
+
+ + handleCompanyChange("phone", e.target.value)} + /> +
+
+ + handleCompanyChange("website", e.target.value)} + placeholder="https://example.com" + /> +
+
+ +
+ +