ReactJS 簡単なブログアプリケーションを作ってみた 📔

# 完成したアプリケーション:tada:

![2022-07-23_16h57_05.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/0ff81157-0cc4-3558-47f1-9c78ef652d23.gif)


### こちらのリンクからも確認できます:arrow_down:

https://t5rhc6.csb.app/



# 開発環境
### 今回は、**CodeSandbox**を使って開発を進めていきます:rocket:

https://codesandbox.io/


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/52cb399b-ec28-524c-d04b-5d2d685d53cc.png)

### Create SandboxからReactを選択してください!

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/7451bff9-b4df-d9c4-38c9-ff85dda2d6d5.png)

### Reactプロジェクトがセットアップされます！

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/e0999e62-242e-4e1e-9055-8974432ffbca.png)

### それぞれ該当するファイルに下記のソースコードをコピーペーストしてください！

```javascript:App.js
import { useState } from "react";
import "./styles.css";

export default function App() {
  const [blogs, setBlogs] = useState([
    { id: 1, author: "mario", title: "First", body: "First  post" },
    { id: 2, author: "mario", title: "Second", body: "Second post" }
  ]);

  const [title, setTitle] = useState("");
  const [author, setAuthor] = useState("");
  const [body, setBody] = useState("");
  const [id, setId] = useState(0);

  const [isEdit, setIsEdit] = useState(false);

  // Add
  const handleAddPost = () => {
    if (!title || !author || !body) {
      return alert("Please input something ");
    }

    const newPost = {
      id: Math.floor(Math.random() * 1000),
      author: author,
      title: title,
      body: body
    };

    setBlogs([...blogs, newPost]);

    setTitle("");
    setAuthor("");
    setBody("");
  };

  // Delete
  const deletePost = (id) => {
    const newBlogs = blogs.filter((blog) => blog.id !== id);
    setBlogs(newBlogs);
  };

  // Edit
  const handleEdit = () => {
    setBlogs(
      blogs.map((blog) => {
        return blog.id === id
          ? { ...blog, title: title, author: author, body: body }
          : blog;
      })
    );

    setTitle("");
    setAuthor("");
    setBody("");
    setId(0);
    setIsEdit(false);
  };

  const editPost = (blog) => {
    setIsEdit(true);

    setTitle(blog.title);
    setAuthor(blog.author);
    setBody(blog.body);
    setId(blog.id);

    window.scroll({ top: 0, behavior: "smooth" });
  };

  return (
    <div className="App">
      <h1>All Blogs</h1>
      <h2>Total posts :{blogs.length}</h2>

      <div className="inputForm">
        <input
          type="text"
          placeholder="Title..."
          value={title}
          onChange={(e) => setTitle(e.target.value)}
        />
        <input
          type="text"
          placeholder="Author..."
          value={author}
          onChange={(e) => setAuthor(e.target.value)}
        />
        <input
          type="text"
          placeholder="Body..."
          value={body}
          onChange={(e) => setBody(e.target.value)}
        />

        {isEdit ? (
          <button className="add-btn" onClick={handleEdit}>
            Edit
          </button>
        ) : (
          <button className="add-btn" onClick={handleAddPost}>
            Add Post
          </button>
        )}
      </div>

      {blogs.map((blog) => (
        <div key={blog.id} className="card">
          <div>
            <h1>Title : {blog.title}</h1>
            <p>Body : {blog.body}</p>
            <p>Author : {blog.author}</p>
          </div>

          <div>
            <button onClick={() => deletePost(blog.id)} className="delete-btn">
              Delete
            </button>
            <button onClick={() => editPost(blog)} className="edit-btn">
              Edit
            </button>
          </div>
        </div>
      ))}
    </div>
  );
}
```

```css:styles.css
.App {
  font-family: sans-serif;
  text-align: center;
  align-items: center;
}

.inputForm {
  display: flex;
  flex-direction: column;
  width: 30%;
  margin: auto;
}

input {
  padding: 5px;
  margin-top: 5px;
}

.add-btn {
  padding: 5px;
  width: 50%;
  margin: 5px auto;
  background-color: #329eff;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid black;
}

.add-btn:hover {
  background-color: #b8dcf5;
}

.delete-btn {
  padding: 5px;
  width: 30%;
  margin: auto;
  background-color: #f16f6f;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid rgb(214, 18, 18);
}

.delete-btn:hover {
  background-color: #f5bbc0;
}

.edit-btn {
  padding: 5px;
  width: 30%;
  margin: auto;
  background-color: #45eb90;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid green;
}

.edit-btn:hover {
  background-color: #ade7c6;
}

.card {
  border: solid 2px #329eff;
  width: 60%;
  border-radius: 20px;
  padding: 4px;
  margin: 8px auto;
  background-color: rgb(243, 243, 230);
}

```