Skip to content

Commit c6b5f73

Browse files
authored
Merge pull request #567 from MeetDOD/issue-566
Feat: Showing users ratings in Testimonials and also using best coding practices successfully issue 566
2 parents 464b3d7 + 598c894 commit c6b5f73

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed
Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
import React, { useState, useEffect } from 'react';
2-
import bgHero from '../assets/bgHero.png';
3-
import { IoCaretForwardOutline } from "react-icons/io5";
4-
import { IoCaretBackOutline } from "react-icons/io5";
52
import axios from 'axios';
6-
7-
interface Testimonial {
8-
quote: string;
9-
author: string;
10-
image: string;
11-
}
12-
13-
interface Feedback {
14-
id: string;
15-
rating: number;
16-
comment: string;
17-
userId: string;
18-
createdAt: string;
19-
user: {
20-
id: string;
21-
username: string;
22-
avatar: string;
23-
};
24-
}
3+
import { IoCaretForwardOutline, IoCaretBackOutline } from "react-icons/io5";
4+
import { MdOutlineStarOutline, MdOutlineStar } from "react-icons/md";
5+
import bgHero from '../assets/bgHero.png';
6+
import { IUserFeedback } from '../types';
7+
import { ImQuotesLeft, ImQuotesRight } from "react-icons/im";
258

269
const TestimonialSlider: React.FC = () => {
2710
const [currentIndex, setCurrentIndex] = useState<number>(0);
2811
const [slidesToShow, setSlidesToShow] = useState<number>(1);
2912
const [isHovered, setIsHovered] = useState<boolean>(false);
30-
const [testimonials, setTestimonials] = useState<Testimonial[]>([]);
13+
const [testimonials, setTestimonials] = useState<IUserFeedback[]>([]);
3114

3215
useEffect(() => {
3316
const updateSlidesToShow = () => {
@@ -45,6 +28,12 @@ const TestimonialSlider: React.FC = () => {
4528
return () => window.removeEventListener('resize', updateSlidesToShow);
4629
}, []);
4730

31+
useEffect(() => {
32+
axios.get('/api/v1/user/getfeedback')
33+
.then(response => setTestimonials(response.data))
34+
.catch(error => console.error('Error fetching testimonials:', error));
35+
}, []);
36+
4837
useEffect(() => {
4938
let intervalId: ReturnType<typeof setInterval>;
5039

@@ -57,25 +46,6 @@ const TestimonialSlider: React.FC = () => {
5746
return () => clearInterval(intervalId);
5847
}, [slidesToShow, isHovered, testimonials.length]);
5948

60-
useEffect(() => {
61-
const fetchFeedback = async () => {
62-
try {
63-
const response = await axios.get('/api/v1/user/getfeedback');
64-
const data: Feedback[] = response.data;
65-
const formattedTestimonials = data.map((feedback) => ({
66-
quote: feedback.comment,
67-
author: feedback.user.username,
68-
image: feedback.user.avatar,
69-
}));
70-
setTestimonials(formattedTestimonials);
71-
} catch (error) {
72-
console.error('Error fetching feedback:', error);
73-
}
74-
};
75-
76-
fetchFeedback();
77-
}, []);
78-
7949
const goToNext = () => {
8050
setCurrentIndex((prevIndex) => (prevIndex + slidesToShow) % testimonials.length);
8151
};
@@ -92,6 +62,16 @@ const TestimonialSlider: React.FC = () => {
9262
setIsHovered(false);
9363
};
9464

65+
const renderStars = (rating: number) => {
66+
return (
67+
<div className="flex">
68+
{[...Array(5)].map((_, index) => (
69+
index < rating ? <MdOutlineStar key={index} size={20} className="text-yellow-500"/> : <MdOutlineStarOutline key={index} size={20} className="text-gray-300"/>
70+
))}
71+
</div>
72+
);
73+
};
74+
9575
return (
9676
<div
9777
className="testimonial-slider-container w-full flex flex-col text-center py-10 text-[#000435] bg-white dark:text-white dark:bg-[#000435]"
@@ -107,19 +87,29 @@ const TestimonialSlider: React.FC = () => {
10787
>
10888
<IoCaretBackOutline/>
10989
</button>
110-
<div className="flex overflow-hidden max-w-full">
111-
{testimonials.slice(currentIndex, currentIndex + slidesToShow).map((testimonial, index) => (
112-
<div key={index} className="testimonial border border-blue-400 dark:border-gray-00 px-2 py-4 md:py-24 rounded-md mx-2 p-6 md:p-10 rounded-lg shadow-lg bg-white dark:bg-[#000435] text-[#000435] dark:text-white flex flex-col items-center justify-center min-w-[260px] md:min-w-[350px] lg:min-w-[400px]">
113-
<img
114-
src={testimonial.image|| `https://ui-avatars.com/api/?name=${testimonial.author}&background=0ea5e9&color=fff&rounded=true&bold=true`}
115-
alt={`${testimonial.author}'s picture`}
116-
className="w-24 h-24 md:w-32 md:h-32 rounded-full mb-6 border-4 p-1 border-[#a238ff] dark:border-white"
117-
/>
118-
<p className="text-lg md:text-2xl italic mb-4 text-center">"{testimonial.quote}"</p>
119-
<h4 className="text-base md:text-xl font-semibold text-center">- {testimonial.author}</h4>
90+
<div className="max-w-screen-xl px-4 py-8 mx-auto text-center lg:py-16 lg:px-6">
91+
{testimonials.slice(currentIndex, currentIndex + slidesToShow).map((testimonial, index) => (
92+
<figure key={index} className="max-w-screen-md mx-auto">
93+
<div className='flex justify-start mb-5'>
94+
<ImQuotesLeft size={30} />
12095
</div>
121-
))}
122-
</div>
96+
<blockquote>
97+
<p className="text-2xl font-medium text-gray-900 dark:text-white">{testimonial.comment}</p>
98+
</blockquote>
99+
<div className='flex justify-end mt-5'>
100+
<ImQuotesRight size={30} />
101+
</div>
102+
<figcaption className="flex items-center justify-center mt-6 space-x-3">
103+
<img className="w-12 h-12 rounded-full" src={testimonial.user.avatar || `https://ui-avatars.com/api/?name=${testimonial.user.username}&background=0ea5e9&color=fff&rounded=true&bold=true`} alt="profile picture" />
104+
<div className="flex flex-col items-center divide-y-2 divide-gray-500 dark:divide-gray-700">
105+
<div className="pb-1 text-sm font-light text-gray-500 dark:text-gray-400">{renderStars(testimonial.rating)}</div>
106+
<div className="pt-1 font-medium text-gray-900 dark:text-white">{testimonial.user.username}</div>
107+
</div>
108+
</figcaption>
109+
</figure>
110+
))}
111+
</div>
112+
123113
<button
124114
className="next-arrow text-4xl cursor-pointer transform hover:scale-125 transition-transform duration-300"
125115
onClick={goToNext}
@@ -140,4 +130,4 @@ const TestimonialSlider: React.FC = () => {
140130
);
141131
};
142132

143-
export default TestimonialSlider;
133+
export default TestimonialSlider;

frontend/src/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ export interface ILeaderboardUser {
5050
postCount: number;
5151
totalReactions:number;
5252
avatar?:string;
53-
}
53+
}
54+
55+
export interface IUserFeedback {
56+
id: string;
57+
rating: number;
58+
comment: string;
59+
userId: string;
60+
visible: boolean;
61+
createdAt: string;
62+
user: IUser;
63+
}

0 commit comments

Comments
 (0)