Skip to content

Commit

Permalink
progress on edit button
Browse files Browse the repository at this point in the history
  • Loading branch information
makon57 committed Jul 1, 2021
1 parent 12d7d25 commit 886e624
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
15 changes: 8 additions & 7 deletions public/javascripts/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
window.addEventListener("load", (event)=>{
console.log("hello from javascript!")

// const writeReview = document.querySelector('.rate-btn hi');

// writeReview.addEventListener('click', (event) => {
// event.preventDefault();

// })

const editBtn = document.querySelector('.edit-btn');

editBtn.addEventListener('click', (event) => {
// event.preventDefault();
const area = document.querySelector('.review-text');
area.innerHTML = `<textarea>${review.text}</textarea>`;
editBtn.appendChild(area)
})
})
33 changes: 23 additions & 10 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ router.get('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
});
}));


router.post('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
const { text } = req.body;
const trailId = req.params.id;
Expand All @@ -48,18 +47,32 @@ router.post('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
res.redirect(`/trail/${trailId}`);
}));

// router.put('/:id(\\d+)', validateTweet, handleValidationErrors,asyncHandler(async(req, res, next) => {
// const tweetId = req.params.id;
// const tweet = await Tweet.findByPk(tweetId);

// if (tweet) {
// await tweet.update({ message: req.body.message });
// res.json({ tweet });
// } else {
// next(tweetNotFound(tweetId));
// router.put('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
// const trailId = req.params.id;
// const review = await db.Review.findByPk(trailId);
// const user = req.session.auth.userId
// if (review) {
// await review.update({ text: req.body.text });
// }
// }));

router.put(
"/trail/:id(\\d+)",
asyncHandler(async (req, res, next) => {
const review = await Review.findOne({ where: { id: req.params.id } });

if (req.user.id !== review.userId) {
const err = new Error("Unauthorized");
err.status = 401;
err.message = "You are not authorized to edit this tweet.";
err.title = "Unauthorized";
throw err;
} else if (review) {
await review.update({ text: req.body.text });
}
})
);

// router.delete('/:id(\\d+)', asyncHandler(async(req, res, next) => {
// const tweetId = req.params.id;
// const tweet = await Tweet.findByPk(tweetId);
Expand Down
8 changes: 7 additions & 1 deletion views/trail-detail.pug
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,17 @@ block content
button(type="submit" class="add-btn hi") Add to TrailList
div(class="review-container")
form(action=`/trail/${trail.id}` method="post" class="")
textarea(name="text" placeholder="Write a review!")
textarea(name="text" placeholder="Write a review!" class="review-input")
button(type="submit" class="rate-btn hi") Submit
div(class="list-reviews")
each review in reviews
div(class="review")
p(class="review-username")= `User: ${review.User.username}`
p(class="review-text")= `${review.text}`
p(class="review-date")= `Reviewed at: ${review.createdAt}`
button(type="submit" class="edit-btn") Edit Review
//- input(type="hidden" class="edit-input")
//- if (review.User.id === review.userId)
//- form(action=`/trail/${trail.id}` method="put" class="")
//- input()

0 comments on commit 886e624

Please sign in to comment.