diff --git a/frontend/src/Pages/Profile.tsx b/frontend/src/Pages/Profile.tsx index ae8d71b5..6444526b 100644 --- a/frontend/src/Pages/Profile.tsx +++ b/frontend/src/Pages/Profile.tsx @@ -87,6 +87,7 @@ import { SavedDebateTranscript, } from "@/services/transcriptService"; import LoadingSpinner from "@/components/LoadingSpinner"; +import { DEFAULT_AVATAR_URL } from "@/constants/avatar"; interface ProfileData { displayName: string; @@ -206,6 +207,37 @@ const Profile: React.FC = () => { const availabilityCheckId = useRef(0); const editSessionRef = useRef(0); + const { user, setUser } = useUser(); + + const syncUserProfileToCache = (profile: { + displayName?: string; + email?: string; + avatarUrl?: string; + bio?: string; + rating?: number; + twitter?: string; + instagram?: string; + linkedin?: string; + }) => { + if (!user && !profile) return; + + const updatedUser = { + ...user, + email: profile.email || user?.email || "", + displayName: profile.displayName || user?.displayName || "User", + bio: profile.bio || user?.bio || "", + rating: profile.rating ?? user?.rating ?? 1500, + avatarUrl: profile.avatarUrl || user?.avatarUrl || DEFAULT_AVATAR_URL, + twitter: profile.twitter ?? user?.twitter, + instagram: profile.instagram ?? user?.instagram, + linkedin: profile.linkedin ?? user?.linkedin, + updatedAt: new Date().toISOString(), + }; + + setUser(updatedUser); + localStorage.setItem("userProfile", JSON.stringify(updatedUser)); + }; + const handleStartEdit = (field: string, initialValue: string = "") => { editSessionRef.current += 1; availabilityCheckId.current += 1; @@ -260,23 +292,24 @@ const Profile: React.FC = () => { setErrorMessage("Transcript not available for this debate."); return; } + setSelectedDebate(debate); setIsDebateDialogOpen(true); setTranscriptLoading(true); - setErrorMessage(""); - setFullTranscript(null); + setErrorMessage(""); + setFullTranscript(null); try { const transcript = await transcriptService.getTranscriptById(debate.id); setFullTranscript(transcript); - } catch(error) { - console.error("Error fetching transcript:", error); - setFullTranscript(null); - setErrorMessage( - error instanceof Error - ? error.message - : "Failed to load transcript. Please try again." - ); + } catch (error) { + console.error("Error fetching transcript:", error); + setFullTranscript(null); + setErrorMessage( + error instanceof Error + ? error.message + : "Failed to load transcript. Please try again." + ); } finally { setTranscriptLoading(false); } @@ -322,16 +355,23 @@ const Profile: React.FC = () => { field === "linkedin" ? draftValue : dashboard.profile.linkedin, dashboard.profile.avatarUrl ); + setSuccessMessage( `${field.charAt(0).toUpperCase() + field.slice(1)} updated successfully!` ); setErrorMessage(""); + if (editSessionRef.current === sessionAtSubmit) { handleCancelEdit(); } - // Refetch to sync updated displayName across the page + const updatedData = await getProfile(token); setDashboard(updatedData); + + if (updatedData.profile) { + syncUserProfileToCache(updatedData.profile); + } + if (updatedData.stats?.recentDebates) { setRecentDebates(updatedData.stats.recentDebates); } @@ -344,13 +384,19 @@ const Profile: React.FC = () => { const handleAvatarSelect = async (avatarUrl: string) => { if (!dashboard?.profile) return; + const token = getAuthToken(); if (!token) { setErrorMessage("Authentication token is missing."); return; } + try { - setDashboard({ ...dashboard, profile: { ...dashboard.profile, avatarUrl } }); + setDashboard({ + ...dashboard, + profile: { ...dashboard.profile, avatarUrl }, + }); + await updateProfile( token, dashboard.profile.displayName, @@ -360,6 +406,12 @@ const Profile: React.FC = () => { dashboard.profile.linkedin, avatarUrl ); + + syncUserProfileToCache({ + ...dashboard.profile, + avatarUrl, + }); + setSuccessMessage("Avatar updated successfully!"); setErrorMessage(""); } catch { @@ -676,7 +728,7 @@ const Profile: React.FC = () => {
{loadingFollowers ? ( -
+
) : followers.length === 0 ? (
No followers yet
) : ( @@ -730,6 +782,7 @@ const Profile: React.FC = () => { {errorMessage}
)} +
Avatar @@ -741,6 +794,7 @@ const Profile: React.FC = () => {
+ setIsAvatarModalOpen(false)} @@ -925,7 +979,6 @@ const Profile: React.FC = () => {
- {/* Keep chart canvases constrained to their cards so Recharts responds to every container resize. */} {totalMatches === 0 ? ( @@ -1014,7 +1067,16 @@ const Profile: React.FC = () => { - 5 ? -45 : 0} textAnchor="end" height={40} interval={Math.floor(filteredEloHistory.length / 5)} /> + 5 ? -45 : 0} + textAnchor="end" + height={40} + interval={Math.floor(filteredEloHistory.length / 5)} + /> } /> @@ -1235,4 +1297,4 @@ const Profile: React.FC = () => { ); }; -export default Profile; +export default Profile; \ No newline at end of file