Upload 5.5, 5.6, and 5.7
This commit is contained in:
@@ -1,26 +1,22 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useRef } from "react";
|
||||||
import Blog from "./components/Blog";
|
import Blog from "./components/Blog";
|
||||||
|
import { BlogForm } from "./components/BlogForm";
|
||||||
import Notification from "./components/Notification";
|
import Notification from "./components/Notification";
|
||||||
import blogService from "./services/blogs";
|
import blogService from "./services/blogs";
|
||||||
import loginService from "./services/login";
|
import loginService from "./services/login";
|
||||||
|
import Togglable from "./components/Togglable";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [blogs, setBlogs] = useState([]);
|
const [blogs, setBlogs] = useState([]);
|
||||||
const [user, setUser] = useState(null);
|
const [user, setUser] = useState(null);
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [title, setTitle] = useState("");
|
|
||||||
const [author, setAuthor] = useState("");
|
|
||||||
const [urlAddress, setUrlAddress] = useState("");
|
|
||||||
const [errorMessage, setErrorMessage] = useState(null);
|
const [errorMessage, setErrorMessage] = useState(null);
|
||||||
const [successMessage, setSuccessMessage] = useState(null);
|
const [successMessage, setSuccessMessage] = useState(null);
|
||||||
|
|
||||||
const resetInputFields = () => {
|
const resetInputFields = () => {
|
||||||
setUsername("");
|
setUsername("");
|
||||||
setPassword("");
|
setPassword("");
|
||||||
setTitle("");
|
|
||||||
setAuthor("");
|
|
||||||
setUrlAddress("");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const setNotification = (message, type) => {
|
const setNotification = (message, type) => {
|
||||||
@@ -75,22 +71,16 @@ const App = () => {
|
|||||||
setNotification("Logout failed", "error");
|
setNotification("Logout failed", "error");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const newBlogRef = useRef();
|
||||||
const handleNewBlog = async (event) => {
|
const handleNewBlog = async (newBlog) => {
|
||||||
event.preventDefault();
|
newBlogRef.current.toggleVisibility();
|
||||||
const newBlog = {
|
|
||||||
title: title,
|
|
||||||
author: author,
|
|
||||||
url: urlAddress,
|
|
||||||
};
|
|
||||||
|
|
||||||
blogService
|
blogService
|
||||||
.create(newBlog)
|
.create(newBlog)
|
||||||
.then((returnedBlog) => {
|
.then((returnedBlog) => {
|
||||||
setBlogs(blogs.concat(returnedBlog));
|
setBlogs(blogs.concat(returnedBlog));
|
||||||
resetInputFields();
|
resetInputFields();
|
||||||
setNotification(
|
setNotification(
|
||||||
`A new blog ${title} by ${author} at ${urlAddress} added`,
|
`A new blog ${returnedBlog.title} by ${returnedBlog.author} at ${returnedBlog.url} added`,
|
||||||
"success"
|
"success"
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
@@ -123,52 +113,21 @@ const App = () => {
|
|||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
|
|
||||||
const blogForm = () => (
|
const blogForm = () => {
|
||||||
<div>
|
return (
|
||||||
<h2> Create new blog</h2>
|
<div>
|
||||||
<form onSubmit={handleNewBlog}>
|
<p> {user.name} logged in </p>
|
||||||
<div>
|
<button onClick={handleLogout}>Logout</button>
|
||||||
Title:
|
<Togglable buttonLabel="New blog" ref={newBlogRef}>
|
||||||
<input
|
<BlogForm createBlog={handleNewBlog} />
|
||||||
type="text"
|
</Togglable>
|
||||||
value={title}
|
<h2> All blogs</h2>
|
||||||
name="Title"
|
|
||||||
onChange={({ target }) => setTitle(target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
Author:
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={author}
|
|
||||||
name="Author"
|
|
||||||
onChange={({ target }) => setAuthor(target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
URL:
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={urlAddress}
|
|
||||||
name="Url"
|
|
||||||
onChange={({ target }) => setUrlAddress(target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<button type="submit">Create</button>
|
|
||||||
</form>
|
|
||||||
<h2> All blogs</h2>
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<th>Title</th>
|
|
||||||
<th>Author</th>
|
|
||||||
<th>URL</th>
|
|
||||||
</tr>
|
|
||||||
{blogs.map((blog) => (
|
{blogs.map((blog) => (
|
||||||
<Blog key={blog.id} blog={blog} />
|
<Blog key={blog.id} blog={blog} />
|
||||||
))}
|
))}
|
||||||
</table>
|
</div>
|
||||||
</div>
|
);
|
||||||
);
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -183,11 +142,7 @@ const App = () => {
|
|||||||
{loginForm()}
|
{loginForm()}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div>
|
<div>{blogForm()}</div>
|
||||||
<p> {user.name} logged in </p>
|
|
||||||
<button onClick={handleLogout}>Logout</button>
|
|
||||||
{blogForm()}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,9 +1,26 @@
|
|||||||
const Blog = ({ blog }) => (
|
import Togglable from "./Togglable";
|
||||||
<tr>
|
|
||||||
<td>{blog.title}</td>
|
const Blog = ({ blog }) => {
|
||||||
<td>{blog.author}</td>
|
const blogStyle = {
|
||||||
<td>{blog.url}</td>
|
paddingTop: 10,
|
||||||
</tr>
|
paddingLeft: 2,
|
||||||
);
|
border: "solid",
|
||||||
|
borderWidth: 1,
|
||||||
|
marginBottom: 5,
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div style={blogStyle}>
|
||||||
|
<div>
|
||||||
|
{blog.title}
|
||||||
|
<Togglable buttonLabel="view">
|
||||||
|
<ul>
|
||||||
|
<li> {blog.author}</li>
|
||||||
|
<li> {blog.url}</li>
|
||||||
|
</ul>
|
||||||
|
</Togglable>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default Blog;
|
export default Blog;
|
||||||
|
|||||||
59
part5/bloglist-frontend/src/components/BlogForm.js
Normal file
59
part5/bloglist-frontend/src/components/BlogForm.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
|
export const BlogForm = ({ createBlog }) => {
|
||||||
|
const [title, setTitle] = useState("");
|
||||||
|
const [author, setAuthor] = useState("");
|
||||||
|
const [urlAddress, setUrlAddress] = useState("");
|
||||||
|
|
||||||
|
const resetInputFields = () => {
|
||||||
|
setTitle("");
|
||||||
|
setAuthor("");
|
||||||
|
setUrlAddress("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const addBlog = async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
createBlog({
|
||||||
|
title: title,
|
||||||
|
author: author,
|
||||||
|
url: urlAddress,
|
||||||
|
});
|
||||||
|
resetInputFields();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h2> Create new blog</h2>
|
||||||
|
<form onSubmit={addBlog}>
|
||||||
|
<div>
|
||||||
|
Title:
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={title}
|
||||||
|
name="title"
|
||||||
|
onChange={(event) => setTitle(event.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Author:
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={author}
|
||||||
|
name="author"
|
||||||
|
onChange={(event) => setAuthor(event.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
URL:
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={urlAddress}
|
||||||
|
name="urlAddress"
|
||||||
|
onChange={(event) => setUrlAddress(event.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button type="submit">Create</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
30
part5/bloglist-frontend/src/components/Togglable.js
Normal file
30
part5/bloglist-frontend/src/components/Togglable.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { useState, forwardRef, useImperativeHandle } from "react";
|
||||||
|
|
||||||
|
const Togglable = forwardRef((props, refs) => {
|
||||||
|
const [visible, setVisible] = useState(false);
|
||||||
|
|
||||||
|
const hideWhenVisible = { display: visible ? "none" : "" };
|
||||||
|
const showWhenVisible = { display: visible ? "" : "none" };
|
||||||
|
|
||||||
|
const toggleVisibility = () => {
|
||||||
|
setVisible(!visible);
|
||||||
|
};
|
||||||
|
|
||||||
|
useImperativeHandle(refs, () => {
|
||||||
|
return { toggleVisibility };
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div style={hideWhenVisible}>
|
||||||
|
<button onClick={toggleVisibility}>{props.buttonLabel}</button>
|
||||||
|
</div>
|
||||||
|
<div style={showWhenVisible}>
|
||||||
|
{props.children}
|
||||||
|
<button onClick={toggleVisibility}>cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Togglable;
|
||||||
Reference in New Issue
Block a user