From 26164903f324b5afd5de090e4a26af87acb72727 Mon Sep 17 00:00:00 2001 From: lestertai8 Date: Mon, 20 Jan 2025 13:43:32 -0600 Subject: [PATCH] search bar for feed --- src/components/CourseSelect/CourseSelect.css | 11 ++++ src/components/CourseSelect/CourseSelect.jsx | 55 ++++++++++---------- src/components/Feed/Feed.jsx | 23 ++++++-- 3 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 src/components/CourseSelect/CourseSelect.css diff --git a/src/components/CourseSelect/CourseSelect.css b/src/components/CourseSelect/CourseSelect.css new file mode 100644 index 0000000..538d12a --- /dev/null +++ b/src/components/CourseSelect/CourseSelect.css @@ -0,0 +1,11 @@ +.search-bar { + background-color: #f5f5f5 !important; + opacity: 1; + z-index: 10; /* Makes it on top of everything else*/ + position: sticky; + top: 10px; /* creates separation between top of screen*/ + width: 100%; + padding: 2px 4px; + display: flex; + align-items: center; +} diff --git a/src/components/CourseSelect/CourseSelect.jsx b/src/components/CourseSelect/CourseSelect.jsx index 94ba842..b66a759 100644 --- a/src/components/CourseSelect/CourseSelect.jsx +++ b/src/components/CourseSelect/CourseSelect.jsx @@ -1,37 +1,36 @@ import React from 'react'; -import InputLabel from '@mui/material/InputLabel'; -import MenuItem from '@mui/material/MenuItem'; -import FormControl from '@mui/material/FormControl'; -import Select from '@mui/material/Select'; +import Paper from '@mui/material/Paper'; +import IconButton from '@mui/material/IconButton'; +import SearchIcon from '@mui/icons-material/Search'; +import InputBase from '@mui/material/InputBase'; +import "./CourseSelect.css"; +// tips for building a search bar: https://dev.to/salehmubashar/search-bar-in-react-js-545l +// Source: https://samhalll.medium.com/implementing-a-search-bar-feature-in-react-10739d594f79 +// this explains how to pass props to a search bar comp -function CourseSelect() { - const [course, setCourse] = React.useState('All'); // change to 'All' later - - const handleChange = (event) => { - setCourse(event.target.value); - console.log(event.target.value); - }; +function CourseSelect( {searchFunc} ) { + + function onType(e) { + // console.log(e.target.value.toLowerCase()); + searchFunc(e.target.value.toLowerCase()); + } return ( -
- - Course Filter - - -
+ + + + + + ); } diff --git a/src/components/Feed/Feed.jsx b/src/components/Feed/Feed.jsx index 9e72ef1..c5fbd87 100644 --- a/src/components/Feed/Feed.jsx +++ b/src/components/Feed/Feed.jsx @@ -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'; @@ -39,6 +40,9 @@ const fake_friends = ["Alice", "Bob", "Charlie", "David"]; function Feed() { 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 @@ -53,6 +57,18 @@ function Feed() { 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(0); const handleChange = (event, newValue) => { @@ -67,12 +83,13 @@ function Feed() { } component={Link} to="/feed" /> } component={Link} to="/public" /> + - { posts.length === 0 ? - (

Loading posts...

) : - (posts + { filteredposts.length === 0 ? + (

No results found...

) : + (filteredposts .slice() .sort((a, b) => b.date.seconds - a.date.seconds).map((post) =>