diff --git a/src/App.css b/src/App.css
index 14aaefd..0c755ab 100644
--- a/src/App.css
+++ b/src/App.css
@@ -38,7 +38,12 @@ label {
margin-left: 10px;
}
-.PostCreateForm .btn {
+.PostCreate .btn, .PostUpdate .btn {
margin-top: 5px;
width: 100%;
}
+
+.PostDetail .btn {
+ margin-top: 5px;
+ width: 170px;
+}
diff --git a/src/Routes.js b/src/Routes.js
index fcc64ca..7328319 100644
--- a/src/Routes.js
+++ b/src/Routes.js
@@ -3,6 +3,7 @@ import App from "./App";
import Navbar from "./components/Navbar";
import PostCreate from "./components/PostCreate";
import PostDetail from "./components/PostDetail";
+import PostUpdate from "./components/PostUpdate";
import Login from "./components/Login";
import Register from "./components/Register";
import Profile from "./components/Profile";
@@ -20,6 +21,7 @@ const Routes = () => {
+
diff --git a/src/components/PostCreate.js b/src/components/PostCreate.js
index 56b401c..7195456 100644
--- a/src/components/PostCreate.js
+++ b/src/components/PostCreate.js
@@ -1,11 +1,15 @@
import { useState, useEffect } from "react";
-import PostCreateForm from "./PostCreateForm";
+import PostForm from "./PostForm";
+import PostFormButtons from "./PostFormButtons";
import AuthService from "../services/auth.service";
+import UserService from "../services/user.service";
const PostCreate = () => {
const [admin, setAdmin] = useState(false);
const [currentUser, setCurrentUser] = useState(false);
+ const [title, setTitle] = useState("");
+ const [body, setBody] = useState("");
useEffect(() => {
const user = AuthService.getCurrentUser();
@@ -17,13 +21,57 @@ const PostCreate = () => {
}
}, []);
+ const handleChangeTitle = (value) => {
+ setTitle(value);
+ };
+
+ const handleChangeBody = (value) => {
+ setBody(value);
+ };
+
+ const handleClickCancel = (e) => {
+ window.location.href = `/posts/`;
+ };
+
+ const handleClickSave = () => {
+ const published = false;
+ createPostAndRedirect(published);
+ };
+
+ const handleClickSaveAndPublish = () => {
+ const published = true;
+ createPostAndRedirect(published);
+ };
+
+ const createPostAndRedirect = (published) => {
+ if (title && body) {
+ UserService.createPost(currentUser._id, title, body, published).then(
+ (response) => {
+ const postId = response.data._id;
+ window.location.href = `/posts/${postId}`;
+ }
+ );
+ } else {
+ alert("Title and Body are required.");
+ }
+ };
+
return (
New Post
{admin ? (
) : (
Only the Admin can create new posts at this time...
diff --git a/src/components/PostCreateForm.js b/src/components/PostCreateForm.js
deleted file mode 100644
index eab4fb7..0000000
--- a/src/components/PostCreateForm.js
+++ /dev/null
@@ -1,99 +0,0 @@
-import { useState } from "react";
-
-import UserService from "../services/user.service";
-
-const PostCreateForm = (props) => {
- const user = props.currentUser;
- const [title, setTitle] = useState("");
- const [body, setBody] = useState("");
-
- const handleTitleChangeText = (e) => {
- const title = e.target.value;
- setTitle(title);
- };
-
- const handleBodyChangeText = (e) => {
- const body = e.target.value;
- setBody(body);
- };
-
- const handleCancelClick = (e) => {
- e.preventDefault();
-
- window.location.href = `/posts/`;
- };
-
- const handleSaveClick = (e) => {
- e.preventDefault();
- const published = false;
- createPostAndRedirect(published);
- };
-
- const handleSaveAndPublishClick = (e) => {
- e.preventDefault();
- const published = true;
- createPostAndRedirect(published);
- };
-
- const createPostAndRedirect = (published) => {
- if (title && body) {
- UserService.createPost(user._id, title, body, published).then(
- (response) => {
- const postId = response.data._id;
- window.location.href = `/posts/${postId}`;
- }
- );
- } else {
- alert("Title and Body are required.");
- }
- };
-
- return (
-
- );
-};
-
-export default PostCreateForm;
diff --git a/src/components/PostDetail.js b/src/components/PostDetail.js
index 4ea7843..63fee82 100644
--- a/src/components/PostDetail.js
+++ b/src/components/PostDetail.js
@@ -59,7 +59,11 @@ const PostDetail = () => {
setPublished(!published);
};
- const handleClick = () => {
+ const handleClickEdit = () => {
+ window.location.href = `/posts/${postId}/edit`;
+ };
+
+ const handleClickDelete = () => {
UserService.deletePost(postId).then(() => {
window.location.href = `/posts/`;
});
@@ -80,7 +84,11 @@ const PostDetail = () => {
{published ? "Published" : "Unpublished"}
-
diff --git a/src/components/PostForm.js b/src/components/PostForm.js
new file mode 100644
index 0000000..e57b830
--- /dev/null
+++ b/src/components/PostForm.js
@@ -0,0 +1,43 @@
+const PostCreateForm = (props) => {
+ const handleChangeTitle = (e) => {
+ e.preventDefault();
+ props.onChangeTitle(e.target.value);
+ };
+
+ const handleChangeBody = (e) => {
+ e.preventDefault();
+ props.onChangeBody(e.target.value);
+ };
+
+ return (
+
+ );
+};
+
+export default PostCreateForm;
diff --git a/src/components/PostFormButtons.js b/src/components/PostFormButtons.js
new file mode 100644
index 0000000..0cd5709
--- /dev/null
+++ b/src/components/PostFormButtons.js
@@ -0,0 +1,34 @@
+const PostFormButtons = (props) => {
+ const handleClickCancel = (e) => {
+ e.preventDefault();
+ props.onClickCancel();
+ };
+
+ const handleClickSave = (e) => {
+ e.preventDefault();
+ props.onClickSave();
+ };
+
+ const handleClickSaveAndPublish = (e) => {
+ e.preventDefault();
+ props.onClickSaveAndPublish();
+ };
+
+ return (
+
+
+ Cancel
+
+
+
+ Save Post (Unpublished)
+
+
+
+ Save and Publish Post
+
+
+ );
+};
+
+export default PostFormButtons;
diff --git a/src/components/PostUpdate.js b/src/components/PostUpdate.js
new file mode 100644
index 0000000..586094a
--- /dev/null
+++ b/src/components/PostUpdate.js
@@ -0,0 +1,105 @@
+import { useState, useEffect } from "react";
+import { useParams } from "react-router-dom";
+
+import PostForm from "./PostForm";
+import PostFormButtons from "./PostFormButtons";
+import AuthService from "../services/auth.service";
+import UserService from "../services/user.service";
+
+const PostUpdate = () => {
+ const [admin, setAdmin] = useState(false);
+ const [title, setTitle] = useState("");
+ const [body, setBody] = useState("");
+ const [post, setPost] = useState({
+ title: "",
+ timestamp: "",
+ timestamp_formatted: "",
+ text: "",
+ published: false,
+ user: {
+ username: "",
+ password: "",
+ },
+ });
+
+ const { postId } = useParams();
+
+ useEffect(() => {
+ UserService.getPost(postId).then((response) => {
+ setPost(response.data);
+ });
+ }, [postId]);
+
+ useEffect(() => {
+ setTitle(post.title);
+ setBody(post.text);
+ }, [post]);
+
+ useEffect(() => {
+ const user = AuthService.getCurrentUser();
+ if (user) {
+ const roles = [];
+ user.roles.map((role) => roles.push(role.name));
+ setAdmin(roles.includes("ROLE_ADMIN"));
+ }
+ }, []);
+
+ const handleChangeTitle = (value) => {
+ setTitle(value);
+ };
+
+ const handleChangeBody = (value) => {
+ setBody(value);
+ };
+
+ const handleClickCancel = () => {
+ window.location.href = `/posts/${postId}`;
+ };
+
+ const handleClickSave = () => {
+ const published = false;
+ updatePostAndRedirect(published);
+ };
+
+ const handleClickSaveAndPublish = () => {
+ const published = true;
+ updatePostAndRedirect(published);
+ };
+
+ const updatePostAndRedirect = (published) => {
+ if (title && body) {
+ UserService.updatePost(postId, title, body, published).then(() => {
+ window.location.href = `/posts/${postId}`;
+ });
+ } else {
+ alert("Title and Body are required.");
+ }
+ };
+
+ return (
+
+
Update Post
+
+ {admin ? (
+
+ ) : (
+
Only the Admin can update posts at this time...
+ )}
+
+ );
+};
+
+export default PostUpdate;
diff --git a/src/services/user.service.js b/src/services/user.service.js
index ce58389..fb16065 100644
--- a/src/services/user.service.js
+++ b/src/services/user.service.js
@@ -21,6 +21,16 @@ const createPost = (userId, title, body, published) => {
});
};
+const updatePost = (postId, title, body, published) => {
+ return axios.post(`${API_URL}/posts/${postId}/update`, {
+ params: {
+ title,
+ body,
+ published,
+ },
+ });
+};
+
const deletePost = (id) => {
return axios.post(`${API_URL}/posts/${id}/delete`);
};
@@ -51,6 +61,7 @@ const UserService = {
getPosts,
getPost,
createPost,
+ updatePost,
deletePost,
getComments,
deleteComment,
diff --git a/todo.md b/todo.md
index 4ce1506..6bc6b92 100644
--- a/todo.md
+++ b/todo.md
@@ -1,4 +1,2 @@
- admin board
- - edit post
- rich text editor for posting and commenting
- - make large post to test truncating on post list