From f5784316da37ca0752c69877d589b65a7aa680be Mon Sep 17 00:00:00 2001 From: Manna Kong Date: Thu, 1 Jul 2021 18:24:59 -0500 Subject: [PATCH] things --- public/javascripts/index.js | 40 ++++++++++++++++++++++++++++++------- routes/index.js | 30 +++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 6aa0c2d..a4abd3e 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -4,15 +4,41 @@ window.addEventListener("load", (event)=>{ const editBtn = document.querySelector('.edit-btn'); if (editBtn) { editBtn.addEventListener('click', (event) => { - // event.preventDefault(); - console.log("asjkbvkaj"); + event.preventDefault(); const id = event.target.id let area = document.querySelector(`#text-${id}`); - area.parentElement.innerHTML = ` `; - // editBtn.appendChild(area); - }) - } + area.parentElement.innerHTML = ` `; + + const saveBtn = document.querySelector('#save-btn'); + console.log(saveBtn); + if (saveBtn) { + saveBtn.addEventListener('click', async (event) => { + // event.preventDefault(); + // const id = event.target.id + // console.log(id); + + }); + } + + const updateValue = async (event) => { + const body = event.target.value; + console.log(body); + // area.parentElement.innerHTML = ``; + + // const review = await fetch(`http://localhost:8080/review/${id}`, { + // method: "PUT", + // headers: { 'Content-Type': 'application/json' }, + // body: JSON.stringify({body}), + // }); + // const update = await review.json(); + } + + area.addEventListener('change', updateValue) + + + }) + } -}) +}); diff --git a/routes/index.js b/routes/index.js index 6b6c407..8785f7a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -56,19 +56,39 @@ router.post('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => { // } // })); +// router.put( +// "/trail/:id(\\d+)", +// asyncHandler(async (req, res, next) => { +// const review = await db.Review.findOne({ where: { id: trailId, userId: req.session.auth.userId } }); +// console.log(review); +// 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.put( - "/trail/:id(\\d+)", + "/review/:id(\\d+)", asyncHandler(async (req, res, next) => { - const review = await Review.findOne({ where: { id: req.params.id } }); - - if (req.user.id !== review.userId) { + const id = req.params.id; + const review = await db.Review.findByPk(id); + console.log(review); + if (req.session.auth.userId !== 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 }); + console.log(req.body.body); + await review.update({ text: req.body.body }); + res.json(review) } }) );