From 598c894752d4c1c02141e4952a82dcb1ed065368 Mon Sep 17 00:00:00 2001 From: meet Date: Mon, 5 Aug 2024 19:47:39 +0530 Subject: [PATCH] Showing users ratings in Testimonials --- frontend/src/components/TestimonialSlider.tsx | 100 ++++++++---------- frontend/src/types.ts | 12 ++- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/frontend/src/components/TestimonialSlider.tsx b/frontend/src/components/TestimonialSlider.tsx index 423d6d22..541a7a2d 100644 --- a/frontend/src/components/TestimonialSlider.tsx +++ b/frontend/src/components/TestimonialSlider.tsx @@ -1,33 +1,16 @@ import React, { useState, useEffect } from 'react'; -import bgHero from '../assets/bgHero.png'; -import { IoCaretForwardOutline } from "react-icons/io5"; -import { IoCaretBackOutline } from "react-icons/io5"; import axios from 'axios'; - -interface Testimonial { - quote: string; - author: string; - image: string; -} - -interface Feedback { - id: string; - rating: number; - comment: string; - userId: string; - createdAt: string; - user: { - id: string; - username: string; - avatar: string; - }; -} +import { IoCaretForwardOutline, IoCaretBackOutline } from "react-icons/io5"; +import { MdOutlineStarOutline, MdOutlineStar } from "react-icons/md"; +import bgHero from '../assets/bgHero.png'; +import { IUserFeedback } from '../types'; +import { ImQuotesLeft, ImQuotesRight } from "react-icons/im"; const TestimonialSlider: React.FC = () => { const [currentIndex, setCurrentIndex] = useState(0); const [slidesToShow, setSlidesToShow] = useState(1); const [isHovered, setIsHovered] = useState(false); - const [testimonials, setTestimonials] = useState([]); + const [testimonials, setTestimonials] = useState([]); useEffect(() => { const updateSlidesToShow = () => { @@ -45,6 +28,12 @@ const TestimonialSlider: React.FC = () => { return () => window.removeEventListener('resize', updateSlidesToShow); }, []); + useEffect(() => { + axios.get('/api/v1/user/getfeedback') + .then(response => setTestimonials(response.data)) + .catch(error => console.error('Error fetching testimonials:', error)); + }, []); + useEffect(() => { let intervalId: ReturnType; @@ -57,25 +46,6 @@ const TestimonialSlider: React.FC = () => { return () => clearInterval(intervalId); }, [slidesToShow, isHovered, testimonials.length]); - useEffect(() => { - const fetchFeedback = async () => { - try { - const response = await axios.get('/api/v1/user/getfeedback'); - const data: Feedback[] = response.data; - const formattedTestimonials = data.map((feedback) => ({ - quote: feedback.comment, - author: feedback.user.username, - image: feedback.user.avatar, - })); - setTestimonials(formattedTestimonials); - } catch (error) { - console.error('Error fetching feedback:', error); - } - }; - - fetchFeedback(); - }, []); - const goToNext = () => { setCurrentIndex((prevIndex) => (prevIndex + slidesToShow) % testimonials.length); }; @@ -92,6 +62,16 @@ const TestimonialSlider: React.FC = () => { setIsHovered(false); }; + const renderStars = (rating: number) => { + return ( +
+ {[...Array(5)].map((_, index) => ( + index < rating ? : + ))} +
+ ); + }; + return (
{ > -
- {testimonials.slice(currentIndex, currentIndex + slidesToShow).map((testimonial, index) => ( -
- {`${testimonial.author}'s -

"{testimonial.quote}"

-

- {testimonial.author}

+
+ {testimonials.slice(currentIndex, currentIndex + slidesToShow).map((testimonial, index) => ( +
+
+
- ))} -
+
+

{testimonial.comment}

+
+
+ +
+
+ profile picture +
+
{renderStars(testimonial.rating)}
+
{testimonial.user.username}
+
+
+ + ))} +
+