Skip to content

Commit

Permalink
feat: implement search function
Browse files Browse the repository at this point in the history
  • Loading branch information
najitwo committed Oct 26, 2024
1 parent b5ba32b commit 23bb578
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
34 changes: 23 additions & 11 deletions components/boards/BoardList.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 (
<section className={styles.wrapper}>
Expand All @@ -39,7 +51,7 @@ const BoardList = () => {
<Button className={styles.button}>글쓰기</Button>
</div>
<div className={styles.toolbar}>
<SearchBar />
<SearchBar initialValue={typeof keyword === "string" ? keyword : ""} />
<Dropdown
className={styles.dropdown}
options={options}
Expand Down
2 changes: 1 addition & 1 deletion components/ui/SearchBar.module.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.container {
.form {
position: relative;
flex: 1;
}
Expand Down
26 changes: 22 additions & 4 deletions components/ui/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { ChangeEvent, FormEvent, useState } from "react";
import { useRouter } from "next/router";
import Image from "next/image";
import Container from "../layout/Container";
import styles from "./SearchBar.module.css";
import searchIcon from "@/public/ic_search.svg";

const SearchBar = () => {
const SearchBar = ({ initialValue = "" }: { initialValue: string }) => {
const [value, setValue] = useState(initialValue);
const router = useRouter();

const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
};

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!value) {
router.push("/boards");
return;
}
router.push(`/boards?keyword=${value}`);
};

return (
<Container className={styles.container}>
<form className={styles.form} onSubmit={handleSubmit}>
<Image src={searchIcon} alt="검색" className={styles.image} />
<input
className={styles.input}
type="text"
placeholder="검색할 상품을 입력해주세요"
onChange={handleChange}
/>
</Container>
</form>
);
};

Expand Down

0 comments on commit 23bb578

Please sign in to comment.