Skip to content

Commit

Permalink
got Post CRUD working
Browse files Browse the repository at this point in the history
  • Loading branch information
nrg51098 committed Nov 11, 2020
1 parent fc0b08a commit 66e3df7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
debug.log

.vscode
88 changes: 44 additions & 44 deletions src/components/posts/PostForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ProfileContext } from "../auth/AuthProvider"
export const PostForm = (props) => {
// Use the required context providers for data
const { addPost, posts, updatePost, getPosts } = useContext(PostContext)
const { profile, getProfile } = useContext(ProfileContext)

// const { profile, getProfile } = useContext(ProfileContext)
// Component state
const [post, setPost] = useState({})

Expand Down Expand Up @@ -50,35 +50,36 @@ export const PostForm = (props) => {
}, [posts])


const constructNewPost = () => {
const locationId = parseInt(post.locationId)

if (locationId === 0) {
window.alert("Please select a location")
const constructNewPost = () => {
const now = new Date ();

if (editMode) {
// PUT
updatePost({
id: post.id,
title: post.title,
content: post.content,
categoryId: 1,
publicationDate: now,
// userId: parseInt(localStorage.getItem("rare_user_id")),
userId: 1,
headerImgUrl: post.headerImgUrl
})
.then(() => props.history.push("/posts"))
} else {
if (editMode) {
// PUT
updatePost({
id: post.id,
name: post.name,
breed: post.breed,
locationId: locationId,
treatment: post.treatment,
customerId: parseInt(localStorage.getItem("kennel_customer"))
})
.then(() => props.history.push("/posts"))
} else {
// POST
addPost({
name: post.name,
breed: post.breed,
locationId: locationId,
treatment: post.treatment,
customerId: parseInt(localStorage.getItem("kennel_customer"))
})
.then(() => props.history.push("/posts"))
}
// POST
addPost({
title: post.title,
content: post.content,
categoryId: 1,
publicationDate: now,
// userId: parseInt(localStorage.getItem("rare_user_id")),
userId: 1,
headerImgUrl: post.headerImgUrl
})
.then(() => props.history.push("/posts"))
}

}

return (
Expand All @@ -97,11 +98,19 @@ export const PostForm = (props) => {
<fieldset>
<div className="form-group">
<label htmlFor="content">Post content: </label>
<input type="text" name="content" required className="form-control"
placeholder="Post content"
defaultValue={post.content}
onChange={handleControlledInputChange}
/>
<textarea type="text" name="content" required className="form-control"
value={post.content}
onChange={handleControlledInputChange}>
</textarea>
</div>
</fieldset>
<fieldset>
<div className="form-group">
<label htmlFor="headerImgUrl">Header Img Url: </label>
<input type="text" name="headerImgUrl" className="form-control"
defaultValue={post.header_img_url}
onChange={handleControlledInputChange}>
</input>
</div>
</fieldset>
{/* <fieldset>
Expand All @@ -119,16 +128,7 @@ export const PostForm = (props) => {
))}
</select>
</div>
</fieldset> */}
<fieldset>
<div className="form-group">
<label htmlFor="treatment">Treatments: </label>
<textarea type="text" name="treatment" className="form-control"
value={post.treatment}
onChange={handleControlledInputChange}>
</textarea>
</div>
</fieldset>
</fieldset> */}
<button type="submit"
onClick={evt => {
evt.preventDefault()
Expand Down

0 comments on commit 66e3df7

Please sign in to comment.