From a9ddf03027ca97fb45edd5dae528bbd9fe16a67a Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Thu, 20 Jun 2024 17:05:29 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=80=20[BUG]=20post=20testimonials=20ba?= =?UTF-8?q?ckend=20Fixed=20(#1056)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed * Update AddTestimonial.jsx * Update Testimonial.jsx --- backend/app/models/Testimonial.js | 2 +- backend/app/routes/testimonial/index.js | 18 ++++++++++++++++- .../app/routes/testimonial/postTestimonial.js | 2 +- .../AddTestimonial/AddTestimonial.jsx | 18 +++++++++++++---- .../ManageTestimonial/ManageTestimonial.jsx | 2 +- frontend/src/service/Testimonial.jsx | 20 +++++++++++++++---- 6 files changed, 50 insertions(+), 12 deletions(-) diff --git a/backend/app/models/Testimonial.js b/backend/app/models/Testimonial.js index 0080abbd..5c830f61 100644 --- a/backend/app/models/Testimonial.js +++ b/backend/app/models/Testimonial.js @@ -18,7 +18,7 @@ const testimonialSchema = new Schema( }, image: { type: String, - required: true, + default: null, }, text: { type: String, diff --git a/backend/app/routes/testimonial/index.js b/backend/app/routes/testimonial/index.js index 86e44f23..b22e0329 100644 --- a/backend/app/routes/testimonial/index.js +++ b/backend/app/routes/testimonial/index.js @@ -1,4 +1,7 @@ const router = require('express').Router({ mergeParams: true }); +const multer = require('multer'); +const { nanoid } = require('nanoid'); +const path = require('path'); const validationMiddleware = require('../../../helpers/middlewares/validation'); const { authMiddleware } = require('../../../helpers/middlewares/auth'); @@ -7,7 +10,20 @@ const postTestimonial = require('./postTestimonial'); const getTestimonials = require('./getTestimonials'); const deleteTestimonial = require('./deleteTestimonial'); -router.post('/', validationMiddleware(postTestimonialValidationSchema), authMiddleware, postTestimonial); +const store = multer.diskStorage({ + destination: (req, file, cb) => { + const dir = 'uploads/TestimonialProfile/'; + cb(null, dir); + }, + filename: (req, file, cb) => { + const uniqueFilename = nanoid() + path.extname(file.originalname); + console.log(`Generated filename: ${uniqueFilename}`); + cb(null, uniqueFilename); + }, +}); +const upload = multer({ storage: store }); + +router.post('/', authMiddleware, upload.single('image'), postTestimonial); router.get('/getTestimonials', getTestimonials); router.delete('/:id', authMiddleware, deleteTestimonial); diff --git a/backend/app/routes/testimonial/postTestimonial.js b/backend/app/routes/testimonial/postTestimonial.js index bda23b77..da06b0f2 100644 --- a/backend/app/routes/testimonial/postTestimonial.js +++ b/backend/app/routes/testimonial/postTestimonial.js @@ -4,7 +4,7 @@ const { ErrorHandler } = require('../../../helpers/error'); const constants = require('../../../constants'); module.exports = async (req, res, next) => { - const [err, { _id }] = await to(Testimonial.create({ ...req.body })); + const [err, { _id }] = await to(Testimonial.create({ ...req.body, image: req.file?.path })); if (err) { const error = new ErrorHandler(constants.ERRORS.DATABASE, { statusCode: 500, diff --git a/frontend/src/pages/Admin/Components/Testimonial/AddTestimonial/AddTestimonial.jsx b/frontend/src/pages/Admin/Components/Testimonial/AddTestimonial/AddTestimonial.jsx index 76fc7105..a38938c0 100644 --- a/frontend/src/pages/Admin/Components/Testimonial/AddTestimonial/AddTestimonial.jsx +++ b/frontend/src/pages/Admin/Components/Testimonial/AddTestimonial/AddTestimonial.jsx @@ -11,7 +11,7 @@ export function AddTestimonial() { name: "", position: "", company: "", - image: "https://i.pinimg.com/originals/f4/cd/d8/f4cdd85c50e44aa59a303fb163ff90f8.jpg", + image: "", text: "", rating: "", }); @@ -27,7 +27,7 @@ export function AddTestimonial() { name: Joi.string().required().label("Name"), position: Joi.string().required().label("Position"), company: Joi.string().required().label("Company"), - image: Joi.any().required().label("Image"), + image: Joi.any().optional().label("Image"), text: Joi.string().required().label("Text"), rating: Joi.number().required().min(1).max(5).label("Rating"), }; @@ -55,6 +55,7 @@ export function AddTestimonial() { if (files && files[0]) { setPic(files[0]); + setFormData({ ...formData, image: files[0] }); let reader = new FileReader(); reader.onload = function (e) { setPicUrl(e.target.result); @@ -96,9 +97,18 @@ export function AddTestimonial() { console.log(errors); } else { // Call the server - await addTestimonial(formData, setToast, toast); + const form = new FormData(); + form.append("name", formData.name); + form.append("position", formData.position); + form.append("company", formData.company); + form.append("text", formData.text); + form.append("rating", formData.rating); + form.append("image", formData.image); + + await addTestimonial(form, setToast, toast); const temp = { + ...formData, name: "", position: "", company: "", @@ -106,7 +116,6 @@ export function AddTestimonial() { rating: "", }; setFormData(temp); - setPicUrl("./images/testimonialImg.png"); } return pic; }; @@ -124,6 +133,7 @@ export function AddTestimonial() {
diff --git a/frontend/src/pages/Admin/Components/Testimonial/ManageTestimonial/ManageTestimonial.jsx b/frontend/src/pages/Admin/Components/Testimonial/ManageTestimonial/ManageTestimonial.jsx index d2f9d28a..70803480 100644 --- a/frontend/src/pages/Admin/Components/Testimonial/ManageTestimonial/ManageTestimonial.jsx +++ b/frontend/src/pages/Admin/Components/Testimonial/ManageTestimonial/ManageTestimonial.jsx @@ -53,7 +53,7 @@ export function ManageTestimonial() {
diff --git a/frontend/src/service/Testimonial.jsx b/frontend/src/service/Testimonial.jsx index cc9db870..d1b19cb2 100644 --- a/frontend/src/service/Testimonial.jsx +++ b/frontend/src/service/Testimonial.jsx @@ -13,7 +13,20 @@ async function getTestimonials(setTestimonials, setToast) { if (response.ok) { const data = await response.json(); - setTestimonials(data); + let _data = [...data] + await data?.map((item,index) => { + let formattedPath = item.image?.replace(/\\/g, "/"); + if (formattedPath?.startsWith("uploads/")) { + formattedPath = formattedPath.replace("uploads/", ""); + if (formattedPath) { + formattedPath = `${END_POINT}/${formattedPath}`; + } + }else{ + formattedPath = "./images/testimonialImg.png"; + } + _data[index].image = formattedPath; + }); + setTestimonials(_data); // we won't be showing the success message for this as it looks wierd on the home page. } else { showToast(setToast, "Failed to fetch testimonials.", "error"); @@ -58,10 +71,9 @@ const addTestimonial = async (testimonial, setToast,toast) => { const response = await fetch(`${END_POINT}/testimonials/`, { method: "POST", headers: { - "Content-Type": "application/json", Authorization: `Bearer ${localStorage.getItem("token")}`, }, - body: JSON.stringify(testimonial), + body: testimonial, }); const res = await response.json(); if (!response.ok) { @@ -86,4 +98,4 @@ const addTestimonial = async (testimonial, setToast,toast) => { } -export { getTestimonials, addTestimonial, deleteTestimonial} \ No newline at end of file +export { getTestimonials, addTestimonial, deleteTestimonial}