-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |