Skip to content

Commit

Permalink
fix: timePreferences saved but not updating in frontend. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica authored Oct 18, 2024
1 parent 31f6c37 commit 2f2b99c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
30 changes: 21 additions & 9 deletions src/hooks/data/useTimePreferences.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -33,7 +45,7 @@ const useTimePreferences = () => {
return {
selectedTimes,
setSelectedTimes,
loading,
loading: loading,
savePreferences,
};
};
Expand Down
15 changes: 1 addition & 14 deletions src/utils/firestore/userProfile.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -81,16 +81,3 @@ export const updateUserProfile = async (uid, updates) => {
// email: "[email protected]",
// 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);
}
};

0 comments on commit 2f2b99c

Please sign in to comment.