Skip to content

Commit

Permalink
implement post creation
Browse files Browse the repository at this point in the history
  • Loading branch information
JHurdle91 committed Oct 19, 2021
1 parent 9975d15 commit cf59ad0
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ label {
border-radius: 50%;
}

.PostList, .PostDetail {
.PostList, .PostDetail, .PostCreate {
margin: 0 20px;
}

.navbar-brand {
margin-left: 10px;
}

.PostCreateForm .btn {
margin-top: 5px;
width: 100%;
}
2 changes: 2 additions & 0 deletions src/Routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserRouter, Switch, Route } from "react-router-dom";
import App from "./App";
import Navbar from "./components/Navbar";
import PostCreate from "./components/PostCreate";
import PostDetail from "./components/PostDetail";
import Login from "./components/Login";
import Register from "./components/Register";
Expand All @@ -18,6 +19,7 @@ const Routes = () => {
<Switch>
<Route exact path={["/", "/home"]} component={App} />
<Route exact path={posts} component={App} />
<Route path={`${posts}/create`} component={PostCreate} />
<Route path={`${posts}/:postId`} component={PostDetail} />
<Route exact path={login} component={Login} />
<Route exact path={register} component={Register} />
Expand Down
35 changes: 35 additions & 0 deletions src/components/PostCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useState, useEffect } from "react";

import PostCreateForm from "./PostCreateForm";
import AuthService from "../services/auth.service";

const PostCreate = () => {
const [admin, setAdmin] = useState(false);
const [currentUser, setCurrentUser] = useState(false);

useEffect(() => {
const user = AuthService.getCurrentUser();
if (user) {
const roles = [];
user.roles.map((role) => roles.push(role.name));
setAdmin(roles.includes("ROLE_ADMIN"));
setCurrentUser(user);
}
}, []);

return (
<div className="PostCreate">
<h1>New Post</h1>
<hr />
{admin ? (
<div>
<PostCreateForm currentUser={currentUser} />
</div>
) : (
<div>Only the Admin can create new posts at this time...</div>
)}
</div>
);
};

export default PostCreate;
99 changes: 99 additions & 0 deletions src/components/PostCreateForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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 (
<div className="PostCreateForm">
<form>
<div className="form-group">
<label htmlFor="titleInput">Title:</label>
<input
id="titleInput"
name="titleInput"
onChange={handleTitleChangeText}
className="form-control"
placeholder="Title"
value={title}
required
/>
<label htmlFor="bodyInput">Post Body:</label>
<textarea
id="commentBox"
name="commentBox"
onChange={handleBodyChangeText}
className="form-control"
type="textarea"
placeholder="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
value={body}
required
></textarea>
</div>
<br />
<div>
<button className="btn btn-danger" onClick={handleCancelClick}>
Cancel
</button>
<br />
<button className="btn btn-primary" onClick={handleSaveClick}>
Save Post
</button>
<br />
<button
className="btn btn-primary"
onClick={handleSaveAndPublishClick}
>
Save and Publish Post
</button>
</div>
</form>
</div>
);
};

export default PostCreateForm;
9 changes: 5 additions & 4 deletions src/components/PostDetail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState, useEffect, useCallback } from "react";
import { useParams, Link } from "react-router-dom";
import Switch from "react-switch";

Expand Down Expand Up @@ -44,14 +44,15 @@ const PostDetail = () => {
}, [post.published]);

const [comments, setComments] = useState([]);
const reloadComments = () => {
const reloadComments = useCallback(() => {
UserService.getComments(postId).then((response) => {
setComments(response.data);
});
};
}, [postId]);

useEffect(() => {
reloadComments();
}, []);
}, [reloadComments]);

const handleChange = () => {
UserService.togglePublished(postId);
Expand Down
13 changes: 13 additions & 0 deletions src/components/PostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ const PostList = () => {
}
}, []);

const handleClick = () => {
window.location.href = "/posts/create";
};

return (
<div className="PostList">
{admin && (
<div>
<button className="btn btn-primary" onClick={handleClick}>
+ New Post
</button>
<br />
<br />
</div>
)}
{posts.map((post) => {
if (post.published || admin) {
return (
Expand Down
12 changes: 12 additions & 0 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ const getPost = (id) => {
return axios.get(`${API_URL}/posts/${id}`);
};

const createPost = (userId, title, body, published) => {
return axios.post(`${API_URL}/posts/create`, {
params: {
userId,
title,
body,
published,
},
});
};

const deletePost = (id) => {
return axios.post(`${API_URL}/posts/${id}/delete`);
};
Expand Down Expand Up @@ -39,6 +50,7 @@ const togglePublished = (postId) => {
const UserService = {
getPosts,
getPost,
createPost,
deletePost,
getComments,
deleteComment,
Expand Down
3 changes: 2 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- admin board
- create post
- edit post
- rich text editor for posting and commenting
- make large post to test truncating on post list

0 comments on commit cf59ad0

Please sign in to comment.