diff --git a/components/boards/BoardList.tsx b/components/boards/BoardList.tsx index 4e22638e..d9f44af3 100644 --- a/components/boards/BoardList.tsx +++ b/components/boards/BoardList.tsx @@ -1,4 +1,5 @@ import { useState, useCallback, useEffect } from "react"; +import { useRouter } from "next/router"; import Image from "next/image"; import Board from "./Board"; import Button from "../ui/Button"; @@ -13,24 +14,35 @@ import { ArticleProps } from "@/types/articleTypes"; const BoardList = () => { const [boards, setBoards] = useState([]); const [order, setOrder] = useState("recent"); + const router = useRouter(); + const { keyword } = router.query; + const BASE_URL = "https://panda-market-api.vercel.app/articles"; const options: DropdownOptions = { recent: "최신순", like: "인기순", }; - const handleLoad = useCallback(async () => { - const { list } = await fetchData(BASE_URL, { - query: { - orderBy: order, - }, - }); - setBoards(list); - }, [order]); + const handleLoad = useCallback( + async (keyword: string = "") => { + const { list } = await fetchData(BASE_URL, { + query: { + orderBy: order, + keyword, + }, + }); + setBoards(list); + }, + [order] + ); useEffect(() => { - handleLoad(); - }, [handleLoad]); + if (typeof keyword === "string") { + handleLoad(keyword); + } else { + handleLoad(); + } + }, [handleLoad, keyword]); return (
@@ -39,7 +51,7 @@ const BoardList = () => {
- + { +const SearchBar = ({ initialValue = "" }: { initialValue: string }) => { + const [value, setValue] = useState(initialValue); + const router = useRouter(); + + const handleChange = (e: ChangeEvent) => { + setValue(e.target.value); + }; + + const handleSubmit = (e: FormEvent) => { + e.preventDefault(); + if (!value) { + router.push("/boards"); + return; + } + router.push(`/boards?keyword=${value}`); + }; + return ( - +
검색 - +
); };