Upload 5.11

This commit is contained in:
Andrew Trieu
2023-06-20 15:56:00 +03:00
parent 67a52f38a2
commit 8c37a7308b
4 changed files with 63 additions and 10 deletions

View File

@@ -72,7 +72,7 @@ const App = () => {
}
};
const newBlogRef = useRef();
const handleNewBlog = async (newBlog) => {
const handleNewBlog = (newBlog) => {
newBlogRef.current.toggleVisibility();
blogService
.create(newBlog)
@@ -89,6 +89,23 @@ const App = () => {
});
};
const handleRemoveBlog = (blog) => {
if (window.confirm(`Remove blog ${blog.title} by ${blog.author}?`)) {
blogService
.remove(blog.id)
.then(() => {
setBlogs(blogs.filter((b) => b.id !== blog.id));
setNotification(
`Blog ${blog.title} by ${blog.author} removed`,
"success"
);
})
.catch((error) => {
setNotification("Failed to remove blog: " + error, "error");
});
}
};
const loginForm = () => (
<form onSubmit={handleLogin}>
<div>
@@ -123,7 +140,12 @@ const App = () => {
</Togglable>
<h2> All blogs</h2>
{blogs.map((blog) => (
<Blog key={blog.id} blog={blog} />
<Blog
key={blog.id}
blog={blog}
setNotification={setNotification}
removeBlog={handleRemoveBlog}
/>
))}
</div>
);