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 newBlogRef = useRef();
const handleNewBlog = async (newBlog) => { const handleNewBlog = (newBlog) => {
newBlogRef.current.toggleVisibility(); newBlogRef.current.toggleVisibility();
blogService blogService
.create(newBlog) .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 = () => ( const loginForm = () => (
<form onSubmit={handleLogin}> <form onSubmit={handleLogin}>
<div> <div>
@@ -123,7 +140,12 @@ const App = () => {
</Togglable> </Togglable>
<h2> All blogs</h2> <h2> All blogs</h2>
{blogs.map((blog) => ( {blogs.map((blog) => (
<Blog key={blog.id} blog={blog} /> <Blog
key={blog.id}
blog={blog}
setNotification={setNotification}
removeBlog={handleRemoveBlog}
/>
))} ))}
</div> </div>
); );

View File

@@ -1,6 +1,8 @@
import { useState } from "react";
import blogService from "../services/blogs";
import Togglable from "./Togglable"; import Togglable from "./Togglable";
const Blog = ({ blog }) => { const Blog = ({ blog, setNotification, removeBlog }) => {
const blogStyle = { const blogStyle = {
paddingTop: 10, paddingTop: 10,
paddingLeft: 2, paddingLeft: 2,
@@ -8,15 +10,36 @@ const Blog = ({ blog }) => {
borderWidth: 1, borderWidth: 1,
marginBottom: 5, marginBottom: 5,
}; };
const [likes, setLikes] = useState(blog.likes);
const handleLike = (blog) => {
blog.likes = blog.likes + 1;
blogService
.update(blog.id, blog)
.then((returnedBlog) => {
setLikes(returnedBlog.likes);
setNotification(`You liked ${blog.title}`, "success");
})
.catch((error) => {
setNotification(`Like failed. Error: ${error}`, "error");
});
};
return ( return (
<div style={blogStyle}> <div style={blogStyle}>
<div> <div>
{blog.title} <em>{blog.title}</em>
<Togglable buttonLabel="view"> <Togglable buttonLabel="View">
<ul> <ul>
<li> {blog.author}</li> <li> Author: {blog.author}</li>
<li> {blog.url}</li> <li> URL: {blog.url}</li>
<li>
{likes} likes&nbsp;
<button onClick={() => handleLike(blog)}>+</button>
</li>
</ul> </ul>
<button onClick={() => removeBlog(blog)}>Remove</button>
</Togglable> </Togglable>
</div> </div>
</div> </div>

View File

@@ -21,7 +21,7 @@ const Togglable = forwardRef((props, refs) => {
</div> </div>
<div style={showWhenVisible}> <div style={showWhenVisible}>
{props.children} {props.children}
<button onClick={toggleVisibility}>cancel</button> <button onClick={toggleVisibility}>Cancel</button>
</div> </div>
</div> </div>
); );

View File

@@ -23,8 +23,15 @@ const create = async (newObject) => {
}; };
const update = async (id, newObject) => { const update = async (id, newObject) => {
const request = axios.put(`${baseUrl} /${id}`, newObject); const response = await axios.put(`${baseUrl}/${id}`, newObject);
const response = await request; return response.data;
};
const remove = async (id) => {
const config = {
headers: { Authorization: token },
};
const response = await axios.delete(`${baseUrl}/${id}`, config);
return response.data; return response.data;
}; };
@@ -33,6 +40,7 @@ const blogService = {
getAll, getAll,
create, create,
update, update,
remove,
}; };
export default blogService; export default blogService;