Skip to content

Commit

Permalink
also added search bar to public feed
Browse files Browse the repository at this point in the history
  • Loading branch information
lestertai8 committed Jan 20, 2025
1 parent 2616490 commit e005e8a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/PublicFeed/PublicFeed.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect } from 'react';
import Post from '../Post/Post';
import NavigationBar from '../NavigationBar/NavigationBar';
import CourseSelect from '../CourseSelect/CourseSelect';
import RestoreIcon from '@mui/icons-material/Restore';
import PeopleIcon from '@mui/icons-material/People';
import PublicIcon from '@mui/icons-material/Public';
Expand Down Expand Up @@ -41,6 +42,9 @@ const fake_friends = ["Alice", "Bob", "Charlie", "David"];

function PublicFeed() {
const [posts, setPosts] = useState([]);
const [search, setSearch] = useState();
const [filteredposts, setFilteredposts] = useState([]);

// useEffect runs every time the component is rendered
useEffect(() => {
// wait for all posts from database to be fetched
Expand All @@ -55,6 +59,18 @@ function PublicFeed() {
fetchPosts();
}, []);

// Source: https://www.freecodecamp.org/news/filter-arrays-in-javascript/
// How to filter an array by search term ^
// https://www.geeksforgeeks.org/how-to-build-a-search-filter-using-react-hooks/
useEffect(() => {
if (search) { // if there's something in the search bar
setFilteredposts(posts.filter(post => post.course_name.toLowerCase().includes(search)))
}
else {
setFilteredposts(posts) // retrieve all if the search bar isn't being used
}
}, [search, posts]); // must define the states that cause re-rendering

const [value, setValue] = React.useState(1);

const handleChange = (event, newValue) => {
Expand All @@ -69,12 +85,13 @@ function PublicFeed() {
<Tab label="Friends" icon={<PeopleIcon />} component={Link} to="/feed" />
<Tab label="Public" icon={<PublicIcon />} component={Link} to="/public" />
</Tabs>
<CourseSelect searchFunc={setSearch} />
<Container maxWidth="sm">
<Box paddingBottom="30px">
<Stack spacing={3}>
{ posts.length === 0 ?
(<p>Loading posts...</p>) :
(posts
{ filteredposts.length === 0 ?
(<p>No results found...</p>) :
(filteredposts
.slice()
.sort((a, b) => b.date.seconds - a.date.seconds).map((post) =>
<div key={post.id}>
Expand Down

0 comments on commit e005e8a

Please sign in to comment.