Skip to content

Commit

Permalink
feat(Home&Explore): add filter function and a accordin
Browse files Browse the repository at this point in the history
  • Loading branch information
IlIIIIIIlI committed Apr 11, 2024
1 parent 4db523b commit fa78b81
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 38 deletions.
24 changes: 24 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-scroll-area": "^1.0.5",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-toast": "^1.1.5",
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/_root/pages/Explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { Input } from "@/components/ui";
import useDebounce from "@/hooks/useDebounce";
import { GridPostList, Loader } from "@/components/shared";
import { useGetPosts, useSearchPosts } from "@/lib/react-query/queries";
import { EverydayQA } from "@/components/EverydayQA/EverydayQA";

export type SearchResultProps = {
isSearchFetching: boolean;
searchedPosts: any;
};

const SearchResults = ({ isSearchFetching, searchedPosts }: SearchResultProps) => {
const SearchResults = ({
isSearchFetching,
searchedPosts,
}: SearchResultProps) => {
if (isSearchFetching) {
return <Loader />;
} else if (searchedPosts && searchedPosts.documents.length > 0) {
Expand All @@ -29,7 +33,8 @@ const Explore = () => {

const [searchValue, setSearchValue] = useState("");
const debouncedSearch = useDebounce(searchValue, 500);
const { data: searchedPosts, isFetching: isSearchFetching } = useSearchPosts(debouncedSearch);
const { data: searchedPosts, isFetching: isSearchFetching } =
useSearchPosts(debouncedSearch);

useEffect(() => {
if (inView && !searchValue) {
Expand All @@ -45,7 +50,8 @@ const Explore = () => {
);

const shouldShowSearchResults = searchValue !== "";
const shouldShowPosts = !shouldShowSearchResults &&
const shouldShowPosts =
!shouldShowSearchResults &&
posts.pages.every((item) => item.documents.length === 0);

return (
Expand Down Expand Up @@ -85,6 +91,7 @@ const Explore = () => {
/>
</div>
</div>
<EverydayQA />

<div className="flex flex-wrap gap-9 w-full max-w-5xl">
{shouldShowSearchResults ? (
Expand Down
113 changes: 113 additions & 0 deletions frontend/src/_root/pages/Explore_org.tsx
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;
Loading

0 comments on commit fa78b81

Please sign in to comment.