diff --git a/src/App.jsx b/src/App.jsx index 38df009..e21645d 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -29,6 +29,7 @@ function App() { const [userEmail, setUserEmail] = useState(null); const [profilePic, setProfilePic] = useState(null); const [friends, setFriends] = useState([]); + const [filteredPost, setFilteredPost] = useState([]); const isAuthenticated = DEV_MODE || user; @@ -75,6 +76,8 @@ function App() { userEmail={userEmail} profilePic={profilePic} friends={friends} + filteredPost={filteredPost} + setFilteredPost={setFilteredPost} /> : } @@ -99,7 +102,7 @@ function App() { path="/rating-history" element={ isAuthenticated - ? + ? : } /> diff --git a/src/components/Account/Account.jsx b/src/components/Account/Account.jsx index 55b89fb..70670ed 100644 --- a/src/components/Account/Account.jsx +++ b/src/components/Account/Account.jsx @@ -2,12 +2,14 @@ import { Box, Container, Typography, Button, Switch, Divider, Card, CardContent import EditIcon from '@mui/icons-material/Edit'; import NavigationBar from '../NavigationBar/NavigationBar'; import AppBar from '../AppBar/AppBar'; -import React from 'react'; +import React, {useEffect} 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 }) { +function Account({ userName, userEmail, profilePic, friends, filteredPost, setFilteredPost }) { const navigate = useNavigate(); const signOut = () => { @@ -18,6 +20,34 @@ function Account({ userName, userEmail, profilePic, friends }) { 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 (
@@ -70,7 +100,7 @@ function Account({ userName, userEmail, profilePic, friends }) { Classes Rated: - 20 + {filteredPost.length} diff --git a/src/components/PublicFeed/PublicFeed.jsx b/src/components/PublicFeed/PublicFeed.jsx index 125e601..7d18bc6 100644 --- a/src/components/PublicFeed/PublicFeed.jsx +++ b/src/components/PublicFeed/PublicFeed.jsx @@ -14,18 +14,6 @@ import {db} from "../../utilities/firebase"; import "./PublicFeed.css"; import AppBar from '../AppBar/AppBar'; -const fake_post = { - id: 1, // automatically assigned - title: "I LOVE CS394", - course_name: "CS 394", - username: "john12nu", // no need to fill this out in the submission form - quarter: "Fall 2024", - body: "I had such an amazing time taking CS394. Highly recommend!", - rating: "5", - professor: "Prof. Riesbeck", - date: "January 10th, 2024", // use JS object to retrieve the date posted -}; - async function getPostsFromDB() { // get all the posts from the database @@ -81,12 +69,12 @@ function PublicFeed() { return (
- + } component={Link} to="/feed" /> } component={Link} to="/public" /> + - { filteredposts.length === 0 ? diff --git a/src/components/RatingHistory/RatingHistory.jsx b/src/components/RatingHistory/RatingHistory.jsx index f4a48e8..c86e130 100644 --- a/src/components/RatingHistory/RatingHistory.jsx +++ b/src/components/RatingHistory/RatingHistory.jsx @@ -1,7 +1,4 @@ import Post from '../Post/Post'; -import { collection, getDocs } from "firebase/firestore"; -import { db } from "../../utilities/firebase"; -import { useState, useEffect } from 'react'; import { Container, Box, Stack, Typography } from '@mui/material'; import AppBar from '../AppBar/AppBar'; import NavigationBar from '../NavigationBar/NavigationBar'; @@ -9,39 +6,10 @@ import './RatingHistory.css'; import Avatar from '@mui/material/Avatar'; -function RatingHistory({ userName, profilePic }) { - const [filteredPost, setFilteredPost] = useState([]); +function RatingHistory({ userName, profilePic, filteredPost }) { const fake_friends = ["Alice", "Bob", "Charlie", "David"]; - - 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); - // console.log("Filtered posts: ", fetchedPosts); - } catch (error) { - console.error("Error fetching posts: ", error); - } - } - fetchPosts(); - }, [userName]); // Add userName as a dependency to prevent it from running infinitely + // Add userName as a dependency to prevent it from running infinitely return (