sungjin

Add new feature, search

...@@ -71,4 +71,14 @@ export const createComment = async (id, content) => { ...@@ -71,4 +71,14 @@ export const createComment = async (id, content) => {
71 throw new Error('Failed to create comment!'); 71 throw new Error('Failed to create comment!');
72 } 72 }
73 return response.data; 73 return response.data;
74 +}
75 +
76 +export const searchPost = async (search) => {
77 + const response = await axios.post(`${SERVER_BASE_URL}/post/search/`, {
78 + search,
79 + });
80 + if (response.status !== 200 && response.status !== 201) {
81 + throw new Error('Failed to get posts!');
82 + }
83 + return response.data;
74 } 84 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +import { searchPost } from "../api/post/post";
2 +import { useState } from "react";
3 +
4 +export default function Search() {
5 + const handleSubmit = async (e) => {
6 + e.preventDefault();
7 + const search = await searchPost(e.target.search.value);
8 + console.log(search)
9 + setResult(search);
10 + }
11 +
12 + const [result, setResult] = useState([]);
13 +
14 + return (
15 + <div>
16 + <form onSubmit={handleSubmit}>
17 + <input name="search" id="search" placeholder="Search Something..."></input>
18 + <button type="submit">Search</button>
19 + </form>
20 + <div>
21 + <h1>Search Result</h1>
22 + {result.map((post) => {
23 + return (
24 + <div>
25 + <h2>{post.title}</h2>
26 + </div>
27 + )
28 + })}
29 + </div>
30 + </div>
31 + );
32 +}
...\ No newline at end of file ...\ No newline at end of file