From 2f2b99cbdee2cc27c1711d0e0f3163536461aa53 Mon Sep 17 00:00:00 2001 From: Elara Liu <40444637+ZL-Asica@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:45:47 -0500 Subject: [PATCH] fix: timePreferences saved but not updating in frontend. (#15) --- src/hooks/data/useTimePreferences.js | 30 +++++++++++++++++++--------- src/utils/firestore/userProfile.js | 15 +------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/hooks/data/useTimePreferences.js b/src/hooks/data/useTimePreferences.js index 2db1ff1..09fdd33 100644 --- a/src/hooks/data/useTimePreferences.js +++ b/src/hooks/data/useTimePreferences.js @@ -1,29 +1,41 @@ import { useState, useEffect } from 'react'; import { useAuthState } from '@auth/useAuthState'; -import useUserProfile from '@data/useUserProfile'; -import { saveTimePreferences } from '@firestore/userProfile'; +import { fetchUserProfile, updateUserProfile } from '@firestore/userProfile'; import { useNavigate } from 'react-router-dom'; const useTimePreferences = () => { + // Initialize as null to indicate loading state const [selectedTimes, setSelectedTimes] = useState([]); + const [loading, setLoading] = useState(true); const [user] = useAuthState(); const navigate = useNavigate(); const userId = user?.uid; - const { userProfile, loading } = useUserProfile(); // Set selected times from user profile only when userProfile is updated useEffect(() => { - if (userProfile && userProfile.timePreferences) { - setSelectedTimes(userProfile.timePreferences); - } - }, [userProfile]); + const loadPreferences = async () => { + try { + const { profile } = await fetchUserProfile(userId); + const fetchedTimes = profile?.timePreferences || []; + setSelectedTimes(fetchedTimes); + } catch (err) { + console.error('Failed to load time preferences'); + } finally { + setLoading(false); + } + }; + + loadPreferences(); + }, [userId]); // Function to save the selected time preferences const savePreferences = async () => { try { - await saveTimePreferences(userId, selectedTimes); + await updateUserProfile(userId, { + timePreferences: selectedTimes, + }); navigate(`/profile/${userId}`); } catch (err) { console.error('Failed to save time preferences'); @@ -33,7 +45,7 @@ const useTimePreferences = () => { return { selectedTimes, setSelectedTimes, - loading, + loading: loading, savePreferences, }; }; diff --git a/src/utils/firestore/userProfile.js b/src/utils/firestore/userProfile.js index 1e98db1..f586fe1 100644 --- a/src/utils/firestore/userProfile.js +++ b/src/utils/firestore/userProfile.js @@ -1,6 +1,6 @@ // User profile operations (get, update, check) import { db } from '@utils/firebaseConfig'; -import { doc, setDoc, getDoc, updateDoc, onSnapshot } from 'firebase/firestore'; +import { doc, setDoc, getDoc, updateDoc } from 'firebase/firestore'; // Unified function to fetch and listen to user profile changes by UID // (supports both regular and transaction-based fetches) @@ -81,16 +81,3 @@ export const updateUserProfile = async (uid, updates) => { // email: "newemail@example.com", // major: "Computer Science" // }); - -// Function to save/update time preferences in Firebase -export const saveTimePreferences = async (uid, selectedTimes) => { - try { - const userDocRef = doc(db, 'users', uid); - await updateDoc(userDocRef, { - timePreferences: selectedTimes, // Update timePreferences field - }); - console.warn('Time preferences updated successfully.'); - } catch (error) { - console.error('Error updating time preferences:', error); - } -};