import createApolloServerClient from "@/graphql/server-client"; import { GET_TESTIMONIALS } from "@/graphql/queries/getTestimonials"; import { GET_TESTIMONIAL_PAGE_TYPE } from "@/graphql/queries/getPageTypeId"; import Heading from "../reuseableUI/heading"; import { TestimonialCard } from "../reuseableUI/testimonialCard"; import EmptyState from "../reuseableUI/emptyState"; export const TestimonialsGrid = async ({ first = 6 }: { first?: number }) => { let items: Array<{ id: string; name: string; text: string; rating: number; publishedAt: string; }> = []; if (!process.env.NEXT_PUBLIC_API_URL) { items = []; } else { try { const client = createApolloServerClient(); const pageTypeResp = await client.query<{ pageTypes?: { edges?: Array<{ node?: { id: string; slug: string } | null }>; }; }>({ query: GET_TESTIMONIAL_PAGE_TYPE, errorPolicy: "all" }); const pageTypeId = pageTypeResp.data.pageTypes?.edges?.[0]?.node?.id; if (!pageTypeId) { items = []; } else { const testimonialsResp = await client.query<{ pages?: { edges?: Array<{ node?: { id: string; publishedAt: string; attributes?: Array<{ attribute?: { slug?: string | null } | null; values?: Array<{ name?: string | null; value?: string | null; }> | null; }> | null; } | null; }>; }; }>({ query: GET_TESTIMONIALS, variables: { pageTypeId, first }, errorPolicy: "all", }); items = (testimonialsResp.data.pages?.edges || []) .map((e) => e.node) .filter(Boolean) .map((n) => { const attrs = n!.attributes || []; const getAttributeValue = (slug: string): string => { const attr = attrs.find((a) => a?.attribute?.slug === slug); return attr?.values?.[0]?.name || attr?.values?.[0]?.value || ""; }; const publishedAt = n!.publishedAt; const name = getAttributeValue("testimonial_name"); const text = getAttributeValue("testimonial_text"); const ratingValue = getAttributeValue("testimonial_rating"); const rating = Math.max(0, Math.min(5, Number(ratingValue) || 0)); return { id: n!.id, name, text, rating, publishedAt, }; }) .filter((item) => item.name && item.text); } } catch (error) { console.error("Error fetching testimonials:", error); items = []; } } return (
{items.length > 0 ? ( items.map((t) => ( )) ) : (
)}
); };