sungjin

Add new feature, search

......@@ -71,4 +71,14 @@ export const createComment = async (id, content) => {
throw new Error('Failed to create comment!');
}
return response.data;
}
export const searchPost = async (search) => {
const response = await axios.post(`${SERVER_BASE_URL}/post/search/`, {
search,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Failed to get posts!');
}
return response.data;
}
\ No newline at end of file
......
import { searchPost } from "../api/post/post";
import { useState } from "react";
export default function Search() {
const handleSubmit = async (e) => {
e.preventDefault();
const search = await searchPost(e.target.search.value);
console.log(search)
setResult(search);
}
const [result, setResult] = useState([]);
return (
<div>
<form onSubmit={handleSubmit}>
<input name="search" id="search" placeholder="Search Something..."></input>
<button type="submit">Search</button>
</form>
<div>
<h1>Search Result</h1>
{result.map((post) => {
return (
<div>
<h2>{post.title}</h2>
</div>
)
})}
</div>
</div>
);
}
\ No newline at end of file