diff --git a/src/App.jsx b/src/App.jsx index 79a3f4911..2126a6192 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -7,6 +7,7 @@ import { auth, db } from "./lib/firebase"; import ErrorBoundary from "./reusableComponents/ErrorBoundary"; import { FaArrowCircleUp } from "react-icons/fa"; +import { FaSearch } from "react-icons/fa"; import { RowModeContext } from "./hooks/useRowMode"; import { makeStyles } from "@mui/styles"; @@ -77,6 +78,8 @@ function App() { const [postText, setPostText] = useState(""); const [rowMode, setRowMode] = useState(false); const [showScroll, setShowScroll] = useState(false); + const [searchText, setSearchText] = useState(""); + const [searchedPosts, setSearchedPosts] = useState([]); const navigate = useNavigate(); const location = useLocation(); @@ -89,6 +92,10 @@ function App() { } }; + const handleSearch = (e) => { + setSearchText(e.target.value); + }; + const scrollTop = () => { window.scrollTo({ top: 0, behavior: "smooth" }); }; @@ -156,6 +163,24 @@ function App() { setLoadMorePosts(false); }, [loadMorePosts]); + useEffect(() => { + const fetchSearchResults = async () => { + setSearchedPosts( + posts.filter( + (post) => + post.post?.displayName + ?.toLowerCase() + .includes(searchText?.toLowerCase()) || + post.post?.username + ?.toLowerCase() + .includes(searchText?.toLowerCase()) + ) + ); + }; + + fetchSearchResults(); + }, [searchText, posts.length]); + return ( @@ -185,48 +210,88 @@ function App() { path="/dummygram/" element={ user ? ( -
+
- {loadingPosts ? ( - - ) : ( -
- - {posts.map(({ id, post }) => ( - + {loadingPosts ? ( + + ) : ( +
+
+ - ))} - -
- )} + +
+ +
+ {searchText + ? searchedPosts.map(({ id, post }) => ( + + )) + : posts.map(({ id, post }) => ( + + ))} +
+
+
+ )} +
) : ( diff --git a/src/components/SearchBar/index.css b/src/components/SearchBar/index.css deleted file mode 100644 index 23e348ea5..000000000 --- a/src/components/SearchBar/index.css +++ /dev/null @@ -1,39 +0,0 @@ -/* search file css */ -.search-bar { - position: relative; - height: 50px; - max-width: 380px; - background: #fff; - margin: 30px auto; - border: 1px solid #0cc; - border-radius: 25px; -} -.search-input { - position: absolute; - height: 100%; - width: 100%; - border-radius: 25px; - background: #fff; - border: none; - font-size: 16px; - padding-left: 20px; - margin-top: -2px; -} -.search-icon { - position: absolute; - right: 0px; - top: 0; - width: 54px; - background: linear-gradient(40deg, #0cc, #daf1f7); - height: 103%; - text-align: center; - line-height: 50px; - color: #fff; - font-size: 20px; - border-radius: 0 25px 25px 0; - margin-top: -2px; -} - -.text-white { - color: var(--color); -} diff --git a/src/components/SearchBar/index.jsx b/src/components/SearchBar/index.jsx index 9bbcb33f3..ceea75b15 100644 --- a/src/components/SearchBar/index.jsx +++ b/src/components/SearchBar/index.jsx @@ -1,5 +1,3 @@ -import "./index.css"; - import React, { memo, useEffect, useState } from "react"; import { auth, db } from "../../lib/firebase"; @@ -68,7 +66,10 @@ function SearchBar() { currentPostLink={currentPostLink} postText={postText} /> -
+