Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search bar in home #838

Merged
merged 17 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 101 additions & 36 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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();
Expand All @@ -89,6 +92,10 @@ function App() {
}
};

const handleSearch = (e) => {
setSearchText(e.target.value);
};

const scrollTop = () => {
window.scrollTo({ top: 0, behavior: "smooth" });
};
Expand Down Expand Up @@ -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 (
<RowModeContext.Provider value={rowMode}>
<ErrorBoundary inApp={true}>
Expand Down Expand Up @@ -185,48 +210,88 @@ function App() {
path="/dummygram/"
element={
user ? (
<div className="flex">
<div style={{ display: "flex", justifyContent: "center" }}>
<ErrorBoundary inApp={true}>
<SideBar />
</ErrorBoundary>
<div
className="home-posts-container"
style={
!loadingPosts
? {}
: {
width: "100%",
minHeight: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
}
}
style={{
display: "flex",
justifyContent: "center",
flexDirection: rowMode ? "row" : "column",
}}
>
{loadingPosts ? (
<Loader />
) : (
<div
className={`${
rowMode ? "app__posts" : "app_posts_column flex"
}`}
>
<ErrorBoundary inApp>
{posts.map(({ id, post }) => (
<Post
rowMode={rowMode}
key={id}
postId={id}
user={user}
post={post}
shareModal={setOpenShareModal}
setLink={setCurrentPostLink}
setPostText={setPostText}
<div
className="home-posts-container"
style={
!loadingPosts
? {}
: {
width: "100%",
minHeight: "100vh",
display: "flex",
flexDirection: rowMode ? "row" : "column",
justifyContent: "center",
alignItems: "center",
}
}
>
{loadingPosts ? (
<Loader />
) : (
<div
style={{ display: "flex", flexDirection: "column" }}
className={`${
rowMode ? "app__posts " : "app_posts_column flex"
}`}
>
<div
className="search-bar"
style={{ minWidth: "300px" }}
>
<input
type="search"
className="search-input"
value={searchText}
placeholder="Search Here..."
onChange={handleSearch}
/>
))}
</ErrorBoundary>
</div>
)}
<label className="search-icon">
<FaSearch />
</label>
</div>
<ErrorBoundary inApp={true}>
<div className={rowMode ? "app__posts" : ""}>
{searchText
? searchedPosts.map(({ id, post }) => (
<Post
rowMode={rowMode}
key={id}
postId={id}
user={user}
post={post}
shareModal={setOpenShareModal}
setLink={setCurrentPostLink}
setPostText={setPostText}
/>
))
: posts.map(({ id, post }) => (
<Post
rowMode={rowMode}
key={id}
postId={id}
user={user}
post={post}
shareModal={setOpenShareModal}
setLink={setCurrentPostLink}
setPostText={setPostText}
/>
))}
</div>
</ErrorBoundary>
</div>
)}
</div>
</div>
</div>
) : (
Expand Down
39 changes: 0 additions & 39 deletions src/components/SearchBar/index.css

This file was deleted.

7 changes: 4 additions & 3 deletions src/components/SearchBar/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "./index.css";

import React, { memo, useEffect, useState } from "react";
import { auth, db } from "../../lib/firebase";

Expand Down Expand Up @@ -68,7 +66,10 @@ function SearchBar() {
currentPostLink={currentPostLink}
postText={postText}
/>
<div className="search-bar" style={{ marginTop: "-150px" }}>
<div
className="search-bar"
style={{ marginTop: "-150px", minWidth: "380px" }}
>
<input
type="search"
className="search-input"
Expand Down
Loading
Loading