'use client'; import { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import { Star, Send, Check } from 'lucide-react'; interface FeedbackData { businessName: string; googleReviewUrl?: string; thankYouMessage?: string; } export default function FeedbackPage() { const params = useParams(); const slug = params.slug as string; const [feedback, setFeedback] = useState(null); const [loading, setLoading] = useState(true); const [rating, setRating] = useState(0); const [hoverRating, setHoverRating] = useState(0); const [comment, setComment] = useState(''); const [submitted, setSubmitted] = useState(false); const [submitting, setSubmitting] = useState(false); useEffect(() => { async function fetchFeedback() { try { const res = await fetch(`/api/qrs/public/${slug}`); if (res.ok) { const data = await res.json(); if (data.contentType === 'FEEDBACK') { setFeedback(data.content as FeedbackData); } } } catch (error) { console.error('Error fetching feedback data:', error); } finally { setLoading(false); } } fetchFeedback(); }, [slug]); const handleSubmit = async () => { if (rating === 0) return; setSubmitting(true); try { await fetch('/api/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ slug, rating, comment }), }); setSubmitted(true); if (rating >= 4 && feedback?.googleReviewUrl) { setTimeout(() => { const url = ensureAbsoluteUrl(feedback.googleReviewUrl); if (url) window.location.href = url; }, 2000); } } catch (error) { console.error('Error submitting feedback:', error); } finally { setSubmitting(false); } }; // Loading if (loading) { return (
); } // Not found if (!feedback) { return (

This feedback form is not available.

); } // Success if (submitted) { return (

Thank you!

{feedback.thankYouMessage || 'Your feedback has been submitted.'}

{rating >= 4 && feedback.googleReviewUrl && (

Redirecting to Google Reviews...

)}
); } // Rating Form return (
{/* Card */}
{/* Colored Header */}

How was your experience?

{feedback.businessName}

{/* Content */}
{/* Stars */}
{[1, 2, 3, 4, 5].map((star) => ( ))}
{/* Rating text */}

0 ? '#6366f1' : 'transparent' }}> {rating === 1 && 'Poor'} {rating === 2 && 'Fair'} {rating === 3 && 'Good'} {rating === 4 && 'Great!'} {rating === 5 && 'Excellent!'}

{/* Comment */}