reviews
This commit is contained in:
@@ -4,10 +4,11 @@ import { useState } from "react"
|
||||
import { Button } from "./ui/button"
|
||||
import { Textarea } from "./ui/textarea"
|
||||
import { Label } from "./ui/label"
|
||||
import { Star } from 'lucide-react'
|
||||
import { LogIn, Star } from 'lucide-react'
|
||||
import { useAuth } from "@/contexts/auth-context"
|
||||
import Link from "next/link"
|
||||
|
||||
|
||||
interface ReviewFormProps {
|
||||
productId: number
|
||||
}
|
||||
@@ -16,19 +17,44 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
||||
const [rating, setRating] = useState(0)
|
||||
const [comment, setComment] = useState("")
|
||||
const { isLoggedIn } = useAuth()
|
||||
const {email} = useAuth();
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
// Here you would typically send the review data to your backend
|
||||
console.log("Submitting review:", { productId, rating, comment })
|
||||
// Reset form after submission
|
||||
setRating(0)
|
||||
setComment("")
|
||||
|
||||
const reviewData = {
|
||||
username: {email}=useAuth(), // Здесь можно подтянуть имя из Auth-контекста
|
||||
rating,
|
||||
comment,
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const response = await fetch(`http://localhost:8080/product/1`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(reviewData),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Ошибка при отправке отзыва")
|
||||
}
|
||||
|
||||
console.log("Отзыв успешно отправлен!")
|
||||
setRating(0)
|
||||
setComment("")
|
||||
} catch (error) {
|
||||
console.error("Ошибка:", error)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isLoggedIn) {
|
||||
return (
|
||||
<p className="text-gray-600">Чтобы оставить отзыв, пожалуйста, <Link href="/login" className="text-blue-600 hover:underline">войдите в систему</Link>.</p>
|
||||
<p className="text-gray-600">
|
||||
Чтобы оставить отзыв, пожалуйста, <Link href="/login" className="text-blue-600 hover:underline">войдите в систему</Link>.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -69,4 +95,3 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user