"use client"; import { sliderProps } from "@/utility/sliderProps"; import Slider from "react-slick"; import { useEffect, useState } from "react"; import Link from "next/link"; const Testimonial = () => { const [reviews, setReviews] = useState([]); const [loading, setLoading] = useState(true); const [expandedReview, setExpandedReview] = useState(null); useEffect(() => { async function loadReviews() { try { const res = await fetch("/api/reviews"); const data = await res.json(); const cleaned = (data.reviews || []).filter(r => (r.text || r.description || r.snippet || r.review_text || r.body || r.content) && r.rating >= 4 ); setReviews(cleaned); } catch (error) { console.error("Home: Failed to fetch reviews", error); } finally { setLoading(false); } } loadReviews(); }, []); function renderStars(rating) { return [...Array(5)].map((_, i) => ( )); } function getReviewText(r) { return r.text || r.description || r.snippet || r.review_text || r.body || r.content || ""; } function truncateText(text) { return text.length > 200 ? text.substring(0, 200) + "..." : text; } function getProfileImage(r) { const url = r.profile_photo_url || r.author_profile_photo_url || r.user?.thumbnail; if (!url) return null; return url.startsWith("http") ? url : `https://lh3.googleusercontent.com/${url}`; } function getInitials(name) { if (!name) return "U"; return name.split(' ').map(n => n[0]).join('').substring(0, 2).toUpperCase(); } return ( customer feedback what have lot’s off happy customer explore feedback review review review {loading ? ( Loading reviews... ) : ( {reviews.map((r, index) => { const name = r.user?.name || r.author_name || "Customer"; const profileImg = getProfileImage(r); const fullText = getReviewText(r); const isExpanded = expandedReview === index; return ( {profileImg ? ( (e.target.style.display = 'none')} style={{ width: '100%', height: '100%', objectFit: 'cover' }} /> ) : ( {getInitials(name)} )} {name} {renderStars(r.rating)} {isExpanded ? fullText : truncateText(fullText)} {fullText.length > 200 && ( { e.stopPropagation(); setExpandedReview(isExpanded ? null : index); }} > {isExpanded ? "Read Less" : "Read More"} )} ); })} )} Review us on Google ); }; export default Testimonial;
Loading reviews...