107 lines
3.1 KiB
TypeScript
107 lines
3.1 KiB
TypeScript
"use client"
|
|
|
|
import { useEffect, useState } from "react"
|
|
import { Star } from "lucide-react"
|
|
import { useAuth } from "@/contexts/auth-context"
|
|
import Link from "next/link"
|
|
import { Button } from "./ui/button"
|
|
|
|
interface Review {
|
|
id: number
|
|
product_id: number
|
|
username: string
|
|
rating: number
|
|
comment: string
|
|
createdAt: string
|
|
}
|
|
|
|
interface ReviewListProps {
|
|
productId: number
|
|
}
|
|
|
|
export function ReviewList({ productId }: ReviewListProps) {
|
|
const { isLoggedIn } = useAuth()
|
|
const [reviews, setReviews] = useState<Review[]>([])
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
const fetchReviews = async () => {
|
|
setIsLoading(true)
|
|
setError(null)
|
|
try {
|
|
const response = await fetch(`http://localhost:8080/product/${productId}`)
|
|
|
|
// Добавляем логирование для отладки
|
|
console.log("Response status:", response.status)
|
|
const data = await response.json()
|
|
console.log("Fetched reviews:", data)
|
|
|
|
// Проверяем, является ли data массивом
|
|
if (Array.isArray(data)) {
|
|
setReviews(data)
|
|
} else {
|
|
setReviews([])
|
|
}
|
|
} catch (error) {
|
|
console.error("Error fetching reviews:", error)
|
|
setError("Ошибка при загрузке отзывов")
|
|
setReviews([])
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
if (productId) {
|
|
fetchReviews()
|
|
}
|
|
}, [productId])
|
|
|
|
if (!isLoggedIn) {
|
|
return (
|
|
<div className="text-center py-8">
|
|
<p className="text-gray-600 mb-4">Чтобы просматривать отзывы, пожалуйста, войдите в систему.</p>
|
|
<Button asChild variant="outline" className="rounded-full">
|
|
<Link href="/login">Войти</Link>
|
|
</Button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (isLoading) {
|
|
return <div>Загрузка отзывов...</div>
|
|
}
|
|
|
|
if (error) {
|
|
return <div className="text-red-500">{error}</div>
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<h3 className="text-lg font-semibold">Отзывы покупателей</h3>
|
|
{!reviews || reviews.length === 0 ? (
|
|
<p>Пока нет отзывов. Будьте первым!</p>
|
|
) : (
|
|
reviews.map((review) => (
|
|
<div key={review.id} className="border-b pb-4">
|
|
<div className="flex items-center gap-2">
|
|
<div className="flex">
|
|
{[1, 2, 3, 4, 5].map((star) => (
|
|
<Star
|
|
key={star}
|
|
className={`h-5 w-5 ${star <= review.rating ? "text-yellow-400 fill-yellow-400" : "text-gray-300"}`}
|
|
/>
|
|
))}
|
|
</div>
|
|
<span className="text-sm text-gray-500">{new Date(review.createdAt).toLocaleDateString()}</span>
|
|
</div>
|
|
<p className="font-semibold mt-1">{review.username}</p>
|
|
<p className="mt-2">{review.comment}</p>
|
|
</div>
|
|
))
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|