From de4befc987ea60d88d3b516ac2861a2d8c8248a5 Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Sun, 11 Aug 2024 17:04:10 +0545 Subject: [PATCH] status display and storign done --- .../src/components/ProfileEditForm.tsx | 40 +++++++++++++------ .../src/components/header/UserProfile.tsx | 29 +++++++++++++- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/nepalingo-web/src/components/ProfileEditForm.tsx b/nepalingo-web/src/components/ProfileEditForm.tsx index b666df5..900df78 100644 --- a/nepalingo-web/src/components/ProfileEditForm.tsx +++ b/nepalingo-web/src/components/ProfileEditForm.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { supabaseClient } from "@/config/supabase-client"; import { useAuth } from "@/hooks/Auth"; @@ -16,12 +16,31 @@ const ProfileEditForm: React.FC = () => { const [status, setStatus] = useState(user?.user_metadata.status || ""); const [isDragging, setIsDragging] = useState(false); + useEffect(() => { + const fetchCurrentStatus = async () => { + if (user) { + const { data, error } = await supabaseClient + .from("updateUser") + .select("status") + .eq("username", user.user_metadata.username) + .single(); + + if (error) { + console.error("Error fetching current status:", error.message); + } else { + setStatus(data?.status || ""); // Update status with the current value from the database + } + } + }; + + fetchCurrentStatus(); + }, [user]); + const handleAvatarChange = async (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { const file = e.target.files[0]; const uploadedAvatarUrl = URL.createObjectURL(file); setAvatarUrl(uploadedAvatarUrl); - // Implement file upload logic here if needed } }; @@ -32,7 +51,6 @@ const ProfileEditForm: React.FC = () => { const file = e.dataTransfer.files[0]; const uploadedAvatarUrl = URL.createObjectURL(file); setAvatarUrl(uploadedAvatarUrl); - // Implement file upload logic here if needed } }; @@ -46,17 +64,15 @@ const ProfileEditForm: React.FC = () => { const handleSaveChanges = async () => { if (!user) return; - // Update user metadata in Supabase - const { error } = await supabaseClient.auth.updateUser({ - data: { - username, // Update username directly in Supabase - avatar_url: avatarUrl, - status, - }, + // Update status, username, and bio in the UpdateUser table + const { error: dbError } = await supabaseClient.from("updateUser").upsert({ + id: user.id, + username, + status, }); - if (error) { - console.error("Error updating profile:", error.message); + if (dbError) { + console.error("Error updating user in database:", dbError.message); return; } diff --git a/nepalingo-web/src/components/header/UserProfile.tsx b/nepalingo-web/src/components/header/UserProfile.tsx index e4c34b8..97206e1 100644 --- a/nepalingo-web/src/components/header/UserProfile.tsx +++ b/nepalingo-web/src/components/header/UserProfile.tsx @@ -5,9 +5,11 @@ import { getPhrase } from "@/components/header/StreakPhrase"; import { useAuth } from "@/hooks/Auth"; import fire from "@/assets/fire.svg"; import { useNavigate } from "react-router-dom"; +import { supabaseClient } from "@/config/supabase-client"; // Import Supabase client const UserProfile: React.FC = () => { const [isOpen, setIsOpen] = useState(false); + const [status, setStatus] = useState(null); const { user, signOut } = useAuth(); const { currentStreak, longestStreak } = useContext(StreakContext); const phrase = getPhrase(currentStreak); @@ -38,6 +40,27 @@ const UserProfile: React.FC = () => { }; }, []); + // Fetch the status from the UpdateUser table + useEffect(() => { + const fetchStatus = async () => { + if (user) { + const { data, error } = await supabaseClient + .from("updateUser") + .select("status") + .eq("username", user.user_metadata.username) + .single(); + + if (error) { + console.error("Error fetching status:", error.message); + } else { + setStatus(data?.status || ""); // Set status or empty string + } + } + }; + + fetchStatus(); + }, [user]); + return (