From 55510ace754571d2e7427d99be3e536080181f48 Mon Sep 17 00:00:00 2001 From: Themanuelel Date: Sun, 19 Jan 2025 23:35:07 -0600 Subject: [PATCH] Component for Public Feed --- src/App.jsx | 2 + src/components/Feed/Feed.jsx | 16 ++++ src/components/PublicFeed/PublicFeed.css | 4 + src/components/PublicFeed/PublicFeed.jsx | 93 ++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/components/PublicFeed/PublicFeed.css create mode 100644 src/components/PublicFeed/PublicFeed.jsx diff --git a/src/App.jsx b/src/App.jsx index 125e2a6..01615e6 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -6,6 +6,7 @@ import { Route, Navigate, } from "react-router-dom"; +import PublicFeed from './components/PublicFeed/PublicFeed'; import Feed from './components/Feed/Feed'; import SignIn from './components/SignIn/SignIn'; import Submission from './components/Submission/Submission'; @@ -33,6 +34,7 @@ const App = () => { {/* comment the following line during development */} {/* logic to protect from seeing feed without logging in */} + : } /> : } /> : } /> : } /> diff --git a/src/components/Feed/Feed.jsx b/src/components/Feed/Feed.jsx index 93e7509..9e72ef1 100644 --- a/src/components/Feed/Feed.jsx +++ b/src/components/Feed/Feed.jsx @@ -1,6 +1,12 @@ import React, { useState, useEffect } from 'react'; import Post from '../Post/Post'; import NavigationBar from '../NavigationBar/NavigationBar'; +import RestoreIcon from '@mui/icons-material/Restore'; +import PeopleIcon from '@mui/icons-material/People'; +import PublicIcon from '@mui/icons-material/Public'; +import Tabs from '@mui/material/Tabs'; +import Tab from '@mui/material/Tab'; +import { Link } from "react-router-dom"; import { Container, Box, Stack } from '@mui/material'; import { collection, getDocs } from "firebase/firestore"; import {db} from "../../utilities/firebase"; @@ -47,10 +53,20 @@ function Feed() { fetchPosts(); }, []); + const [value, setValue] = React.useState(0); + + const handleChange = (event, newValue) => { + setValue(newValue); + }; + console.log(posts[0]); return (
+ + } component={Link} to="/feed" /> + } component={Link} to="/public" /> + diff --git a/src/components/PublicFeed/PublicFeed.css b/src/components/PublicFeed/PublicFeed.css new file mode 100644 index 0000000..5b55a61 --- /dev/null +++ b/src/components/PublicFeed/PublicFeed.css @@ -0,0 +1,4 @@ +.feed-screen { + background-color: white; + width: 100%; +} \ No newline at end of file diff --git a/src/components/PublicFeed/PublicFeed.jsx b/src/components/PublicFeed/PublicFeed.jsx new file mode 100644 index 0000000..8fc67bd --- /dev/null +++ b/src/components/PublicFeed/PublicFeed.jsx @@ -0,0 +1,93 @@ +import React, { useState, useEffect } from 'react'; +import Post from '../Post/Post'; +import NavigationBar from '../NavigationBar/NavigationBar'; +import RestoreIcon from '@mui/icons-material/Restore'; +import PeopleIcon from '@mui/icons-material/People'; +import PublicIcon from '@mui/icons-material/Public'; +import Tabs from '@mui/material/Tabs'; +import Tab from '@mui/material/Tab'; +import { Link } from "react-router-dom"; +import { Container, Box, Stack } from '@mui/material'; +import { collection, getDocs } from "firebase/firestore"; +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 + + // might have to make a "public" database for public posts + const querySnapshot = await getDocs(collection(db, "publicPosts")); + const posts = []; + querySnapshot.forEach((doc) => { + posts.push({id: doc.id, ...doc.data()}); + }); + return posts; +} + +const fake_friends = ["Alice", "Bob", "Charlie", "David"]; + +function PublicFeed() { + const [posts, setPosts] = useState([]); + // useEffect runs every time the component is rendered + useEffect(() => { + // wait for all posts from database to be fetched + async function fetchPosts() { + try { + const fetchedPosts = await getPostsFromDB(); + setPosts(fetchedPosts); + } catch (error) { + console.error("Error fetching posts: ", error); + } + } + fetchPosts(); + }, []); + + const [value, setValue] = React.useState(1); + + const handleChange = (event, newValue) => { + setValue(newValue); + }; + + console.log(posts[0]); + return ( +
+ + + } component={Link} to="/feed" /> + } component={Link} to="/public" /> + + + + + { posts.length === 0 ? + (

Loading posts...

) : + (posts + .slice() + .sort((a, b) => b.date.seconds - a.date.seconds).map((post) => +
+ +
) + ) + } +
+
+
+ +
+ ) +}; + +export default PublicFeed; \ No newline at end of file