From 0c1d8d797af63772de054b83bea3df27b6a56861 Mon Sep 17 00:00:00 2001 From: Sumin Kim Date: Fri, 15 Nov 2024 14:05:48 +0900 Subject: [PATCH] feat: create search params component --- .../4q-gallery/_components/search-params.tsx | 156 ++++++++++++++++ src/app/(pages)/4q-gallery/page.tsx | 174 ++---------------- 2 files changed, 167 insertions(+), 163 deletions(-) diff --git a/src/app/(pages)/4q-gallery/_components/search-params.tsx b/src/app/(pages)/4q-gallery/_components/search-params.tsx index e69de29..2babd50 100644 --- a/src/app/(pages)/4q-gallery/_components/search-params.tsx +++ b/src/app/(pages)/4q-gallery/_components/search-params.tsx @@ -0,0 +1,156 @@ +import React, { useState, useCallback, Suspense } from "react"; +import { useRouter, usePathname, useSearchParams } from "next/navigation"; +import { IoIosArrowBack, IoIosArrowForward, IoIosSearch } from "react-icons/io"; +import { categories } from "../_lib/categories"; +import { Select, Input } from "antd"; +import { ScrollMenu, VisibilityContext } from "react-horizontal-scrolling-menu"; +import styles from "../page.module.css"; + +const { Search } = Input; + +const LeftArrow = () => { + const { isFirstItemVisible, scrollPrev } = + React.useContext(VisibilityContext); + + return ( +
scrollPrev()} + > + +
+ ); +}; + +const RightArrow = () => { + const { isLastItemVisible, scrollNext } = React.useContext(VisibilityContext); + + return ( +
scrollNext()} + > + +
+ ); +}; + +type SearchProps = GetProps; + +export default function SearchParamsHandler({ + isSearchContainerVisible, + setIsSearchContainerVisible, +}: { + isSearchContainerVisible: boolean; + setIsSearchContainerVisible: React.Dispatch>; +}) { + const router = useRouter(); + const pathname = usePathname(); + const searchParams = useSearchParams(); + const [selectedCategory, setSelectedCategory] = useState("all"); + + const tagParam = searchParams?.get("tag") || ""; + + const createQueryString = useCallback( + (name: string, value?: string) => { + const params = new URLSearchParams( + searchParams ? searchParams.toString() : "" + ); + + if (value) { + params.set(name, value); + } else { + params.delete(name); + } + + return params.toString(); + }, + [searchParams] + ); + + const handleCategoryClick = (categoryId: string) => { + setSelectedCategory(categoryId); + const newQueryString = createQueryString( + "category", + categoryId !== "all" ? categoryId : undefined + ); + router.push(`${pathname}?${newQueryString}`); + }; + + const onSearch: SearchProps["onSearch"] = (value) => { + const newQueryString = createQueryString( + "tag", + value.trim() ? value : undefined + ); + router.push(`${pathname}?${newQueryString}`); + }; + + const handleFilterBtnClick = () => { + setIsSearchContainerVisible((prev) => !prev); + }; + + return ( +
+
+ { - const sortValue = value === "최신순" ? "latest" : "popular"; - const newQueryString = createQueryString("sort", sortValue); - router.push(`${pathname}?${newQueryString}`); - }} - options={[ - { value: "최신순", label: "최신순" }, - { value: "인기순", label: "인기순" }, - ]} - /> -
- {tagParam ? ( -
1
- ) : ( - - )} - 검색 -
-
-
-
- - {categories.map((category) => ( -
handleCategoryClick(category.id)} - > - {category.name} -
- ))} -
-
-
-
-

태그 검색

- -
-
- - - -
- - ); -} - -export default function Page() { - const [isSearchContainerVisible, setIsSearchContainerVisible] = - useState(false); - - return ( + + + + ); } - -const LeftArrow = () => { - const { isFirstItemVisible, scrollPrev } = - React.useContext(VisibilityContext); - - return ( -
scrollPrev()} - > - -
- ); -}; - -const RightArrow = () => { - const { isLastItemVisible, scrollNext } = React.useContext(VisibilityContext); - - return ( -
scrollNext()} - > - -
- ); -};