From 5eabbb8c4500cf9321932282410652f27ace1f56 Mon Sep 17 00:00:00 2001 From: Anna Cai Date: Tue, 21 Jan 2025 16:21:12 -0600 Subject: [PATCH] move loadFriends from MyFriends.jsx to App.jsx --- src/App.jsx | 25 ++++++++++++-- src/components/Account/Account.jsx | 36 ++------------------ src/components/Account/Friends/MyFriends.jsx | 28 ++------------- 3 files changed, 29 insertions(+), 60 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index fb1c9a0..e1803c2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,5 +1,5 @@ // App.js -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import './App.css'; import { BrowserRouter as Router, @@ -18,6 +18,9 @@ import MyFriends from './components/Account/Friends/MyFriends'; import RatingHistory from './components/RatingHistory/RatingHistory'; import Comment from './components/Comment/Comment'; import SearchPage from './components/SearchPage/SearchPage'; +import { fetchUserFriends } from './services/friendService'; +import { doc, getDoc } from 'firebase/firestore'; +import { db } from './utilities/firebase'; // Toggle this to `true` in dev if you want to skip sign-in const DEV_MODE = false; @@ -31,6 +34,24 @@ function App() { const isAuthenticated = DEV_MODE || user; + const loadFriends = async () => { + const friendIds = await fetchUserFriends(user.uid); + const friendProfiles = await Promise.all(friendIds.map(async (fid) => { + const ref = doc(db, 'users', fid); + const snap = await getDoc(ref); + if (snap.exists()) { + return { id: fid, ...snap.data() }; + } + return { id: fid, displayName: 'Unknown', email: null }; + })); + setFriends(friendProfiles); + }; + + useEffect(() => { + if (!user) return; + loadFriends(); + }, [user]); + return ( @@ -80,7 +101,7 @@ function App() { path="/my-friends" element={ isAuthenticated - ? + ? : } /> diff --git a/src/components/Account/Account.jsx b/src/components/Account/Account.jsx index 70670ed..99cf47c 100644 --- a/src/components/Account/Account.jsx +++ b/src/components/Account/Account.jsx @@ -1,15 +1,13 @@ -import { Box, Container, Typography, Button, Switch, Divider, Card, CardContent } from '@mui/material'; +import { Box, Container, Typography, Button, Divider, Card, CardContent } from '@mui/material'; import EditIcon from '@mui/icons-material/Edit'; import NavigationBar from '../NavigationBar/NavigationBar'; import AppBar from '../AppBar/AppBar'; -import React, {useEffect} from 'react'; +import React from 'react'; import { useNavigate } from 'react-router-dom'; import Avatar from '@mui/material/Avatar'; import "./Account.css"; -import { collection, getDocs } from "firebase/firestore"; -import { db } from "../../utilities/firebase"; -function Account({ userName, userEmail, profilePic, friends, filteredPost, setFilteredPost }) { +function Account({ userName, userEmail, profilePic, friends, filteredPost }) { const navigate = useNavigate(); const signOut = () => { @@ -20,34 +18,6 @@ function Account({ userName, userEmail, profilePic, friends, filteredPost, setFi navigate('/my-friends'); }; - async function getPostsFromDB() { - try { - const querySnapshot = await getDocs(collection(db, "posts")); - const posts = []; - querySnapshot.forEach((doc) => { - if (doc.data().username === userName?.displayName){ - posts.push({ id: doc.id, ...doc.data() }); - } - }); - return posts; - } catch (error) { - console.log("Error: ", error); - } - -} - -useEffect(() => { - async function fetchPosts() { - try { - const fetchedPosts = await getPostsFromDB(); - setFilteredPost(fetchedPosts); - } catch (error) { - console.error("Error fetching posts: ", error); - } - } - fetchPosts(); -}, [userName]); - return (
diff --git a/src/components/Account/Friends/MyFriends.jsx b/src/components/Account/Friends/MyFriends.jsx index ddb2989..e689d25 100644 --- a/src/components/Account/Friends/MyFriends.jsx +++ b/src/components/Account/Friends/MyFriends.jsx @@ -1,33 +1,11 @@ -import React, { useEffect } from 'react'; +import React from 'react'; import { List, ListItem, ListItemText, Button, Container } from '@mui/material'; -import { fetchUserFriends, removeFriend } from '../../../services/friendService'; -import { doc, getDoc } from 'firebase/firestore'; -import { db } from '../../../utilities/firebase'; +import { removeFriend } from '../../../services/friendService'; import AppBar from '../../AppBar/AppBar'; import NavigationBar from '../../NavigationBar/NavigationBar'; import "./Friends.css"; -function MyFriends({ user, friends, setFriends }) { - - useEffect(() => { - if (!user) return; - loadFriends(); - }, [user]); - - const loadFriends = async () => { - const friendIds = await fetchUserFriends(user.uid); - const friendProfiles = await Promise.all(friendIds.map(async (fid) => { - const ref = doc(db, 'users', fid); - const snap = await getDoc(ref); - if (snap.exists()) { - return { id: fid, ...snap.data() }; - } else { - return { id: fid, displayName: 'Unknown', email: null }; - } - })); - setFriends(friendProfiles); - }; - +function MyFriends({ user, friends, loadFriends }) { const handleUnfriend = async (friendId) => { await removeFriend(user.uid, friendId); await loadFriends();