Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import { Swiper, SwiperSlide } from "swiper/react";
import { useCallback, useRef, useEffect, useState } from "react";
import type { Swiper as SwiperClass } from "swiper";
import { Icon } from "@/components/ui/icons";
import { Testimonial } from "@/types/testimonial";
// Import Swiper styles
import "swiper/css/navigation";
import "swiper/css";
import SingleItem from "./SingleItem";
// Loading skeleton for testimonials - matches Swiper: 1 on mobile, 2 on md, 3 on xl
const TestimonialsSkeleton = () => (
<div className="flex gap-5">
{[1, 2, 3].map((i) => (
<div
key={i}
className={`flex-1 bg-white dark:bg-gray-800 rounded-[10px] p-8 animate-pulse ${
i === 3 ? 'hidden xl:block' : i === 2 ? 'hidden md:block' : ''
}`}
>
<div className="flex gap-1 mb-5">
{[1, 2, 3, 4, 5].map((j) => (
<div key={j} className="w-4 h-4 bg-gray-200 dark:bg-gray-700 rounded" />
))}
</div>
<div className="space-y-2 mb-6">
<div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-full" />
<div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-3/4" />
</div>
<div className="space-y-2">
<div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-24" />
<div className="h-3 bg-gray-200 dark:bg-gray-700 rounded w-20" />
</div>
</div>
))}
</div>
);
const Testimonials = () => {
const sliderRef = useRef<SwiperClass | null>(null);
const [testimonials, setTestimonials] = useState<Testimonial[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchTestimonials = async () => {
try {
setLoading(true);
const response = await fetch("/api/testimonials?limit=10");
const result = await response.json();
if (result.success && result.data) {
setTestimonials(result.data);
} else {
setError("Failed to load testimonials");
}
} catch {
setError("Failed to load testimonials");
} finally {
setLoading(false);
}
};
fetchTestimonials();
}, []);
const handlePrev = useCallback(() => {
if (!sliderRef.current) return;
sliderRef.current.slidePrev();
}, []);
const handleNext = useCallback(() => {
if (!sliderRef.current) return;
sliderRef.current.slideNext();
}, []);
// Don't render section if there's an error or no testimonials
if (error || (!loading && testimonials.length === 0)) {
return null;
}
return (
<section className="overflow-hidden pb-16.5">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0">
<div className="">
<div className="swiper testimonial-carousel common-carousel p-5">
{/* <!-- section title --> */}
<div className="mb-10 flex items-center justify-between">
<div>
<span className="flex items-center gap-2.5 font-medium text-dark dark:text-gray-200 mb-1.5">
<Icon name="users" className="text-blue" size={17} />
Testimonials
</span>
<h2 className="font-semibold text-xl xl:text-heading-5 text-dark dark:text-gray-100">
User Feedbacks
</h2>
</div>
{!loading && testimonials.length > 0 && (
<div className="flex items-center gap-3">
<div onClick={handlePrev} className="swiper-button-prev">
<Icon name="chevron-left" className="fill-current" size={24} />
</div>
<div onClick={handleNext} className="swiper-button-next">
<Icon name="chevron-right" className="fill-current" size={24} />
</div>
</div>
)}
</div>
{loading ? (
<TestimonialsSkeleton />
) : (
<Swiper
slidesPerView={3}
spaceBetween={20}
onSwiper={(swiper) => {
sliderRef.current = swiper;
}}
breakpoints={{
// when window width is >= 640px
0: {
slidesPerView: 1},
1000: {
slidesPerView: 2},
// when window width is >= 768px
1200: {
slidesPerView: 3}}}
>
{testimonials.map((item) => (
<SwiperSlide key={item.id || item.authorName}>
<SingleItem testimonial={item} />
</SwiperSlide>
))}
</Swiper>
)}
</div>
</div>
</div>
</section>
);
};
export default Testimonials;
|