-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Home&Explore): add filter function and a accordin
- Loading branch information
1 parent
4db523b
commit fa78b81
Showing
8 changed files
with
302 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useInView } from "react-intersection-observer"; | ||
|
||
import { Input } from "@/components/ui"; | ||
import useDebounce from "@/hooks/useDebounce"; | ||
import { GridPostList, Loader } from "@/components/shared"; | ||
import { useGetPosts, useSearchPosts } from "@/lib/react-query/queries"; | ||
|
||
export type SearchResultProps = { | ||
isSearchFetching: boolean; | ||
searchedPosts: any; | ||
}; | ||
|
||
const SearchResults = ({ isSearchFetching, searchedPosts }: SearchResultProps) => { | ||
if (isSearchFetching) { | ||
return <Loader />; | ||
} else if (searchedPosts && searchedPosts.documents.length > 0) { | ||
return <GridPostList posts={searchedPosts.documents} />; | ||
} else { | ||
return ( | ||
<p className="text-light-4 mt-10 text-center w-full">No results found</p> | ||
); | ||
} | ||
}; | ||
|
||
const Explore = () => { | ||
const { ref, inView } = useInView(); | ||
const { data: posts, fetchNextPage, hasNextPage } = useGetPosts(); | ||
|
||
const [searchValue, setSearchValue] = useState(""); | ||
const debouncedSearch = useDebounce(searchValue, 500); | ||
const { data: searchedPosts, isFetching: isSearchFetching } = useSearchPosts(debouncedSearch); | ||
|
||
useEffect(() => { | ||
if (inView && !searchValue) { | ||
fetchNextPage(); | ||
} | ||
}, [inView, searchValue]); | ||
|
||
if (!posts) | ||
return ( | ||
<div className="flex-center w-full h-full"> | ||
<Loader /> | ||
</div> | ||
); | ||
|
||
const shouldShowSearchResults = searchValue !== ""; | ||
const shouldShowPosts = !shouldShowSearchResults && | ||
posts.pages.every((item) => item.documents.length === 0); | ||
|
||
return ( | ||
<div className="explore-container"> | ||
<div className="explore-inner_container"> | ||
<h2 className="h3-bold md:h2-bold w-full">Search Posts</h2> | ||
<div className="flex gap-1 px-4 w-full rounded-lg bg-dark-4"> | ||
<img | ||
src="/assets/icons/search.svg" | ||
width={24} | ||
height={24} | ||
alt="search" | ||
/> | ||
<Input | ||
type="text" | ||
placeholder="Search" | ||
className="explore-search" | ||
value={searchValue} | ||
onChange={(e) => { | ||
const { value } = e.target; | ||
setSearchValue(value); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div className="flex-between w-full max-w-5xl mt-16 mb-7"> | ||
<h3 className="body-bold md:h3-bold">Popular Today</h3> | ||
|
||
<div className="flex-center gap-3 bg-dark-3 rounded-xl px-4 py-2 cursor-pointer"> | ||
<p className="small-medium md:base-medium text-light-2">All</p> | ||
<img | ||
src="/assets/icons/filter.svg" | ||
width={20} | ||
height={20} | ||
alt="filter" | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div className="flex flex-wrap gap-9 w-full max-w-5xl"> | ||
{shouldShowSearchResults ? ( | ||
<SearchResults | ||
isSearchFetching={isSearchFetching} | ||
searchedPosts={searchedPosts} | ||
/> | ||
) : shouldShowPosts ? ( | ||
<p className="text-light-4 mt-10 text-center w-full">End of posts</p> | ||
) : ( | ||
posts.pages.map((item, index) => ( | ||
<GridPostList key={`page-${index}`} posts={item.documents} /> | ||
)) | ||
)} | ||
</div> | ||
|
||
{hasNextPage && !searchValue && ( | ||
<div ref={ref} className="mt-10"> | ||
<Loader /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Explore; |
Oops, something went wrong.