another one
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import type React from "react"
|
||||
|
||||
import { useState } from "react"
|
||||
import { Checkbox } from "./ui/checkbox"
|
||||
import { Button } from "./ui/button"
|
||||
@@ -30,6 +32,11 @@ export function CartItems() {
|
||||
updateQuantity(id, newQuantity)
|
||||
}
|
||||
|
||||
const handleButtonClick = (e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="mb-4">
|
||||
@@ -48,22 +55,29 @@ export function CartItems() {
|
||||
{items.map((item) => (
|
||||
<div key={item.id} className="flex gap-4 p-4 bg-white rounded-lg">
|
||||
<Checkbox checked={selectedItems.includes(item.id)} onCheckedChange={() => toggleItem(item.id)} />
|
||||
<Link href={`/product/${item.id}`} className="flex-grow flex gap-4">
|
||||
<Image
|
||||
src={item.image || "/placeholder.svg"}
|
||||
alt={item.title}
|
||||
width={100}
|
||||
height={100}
|
||||
className="object-cover"
|
||||
/>
|
||||
<div className="flex-grow flex gap-4">
|
||||
<Link href={`/product/${item.id}`} className="block">
|
||||
<Image
|
||||
src={item.image || "/placeholder.svg"}
|
||||
alt={item.title}
|
||||
width={100}
|
||||
height={100}
|
||||
className="object-cover"
|
||||
/>
|
||||
</Link>
|
||||
<div className="flex-1">
|
||||
<h3 className="font-medium">{item.title}</h3>
|
||||
<div className="flex gap-4 mt-4">
|
||||
<Link href={`/product/${item.id}`}>
|
||||
<h3 className="font-medium">{item.title}</h3>
|
||||
</Link>
|
||||
<div className="flex flex-wrap gap-4 mt-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleUpdateQuantity(item.id, item.quantity - 1)}
|
||||
onClick={(e) => {
|
||||
handleButtonClick(e)
|
||||
handleUpdateQuantity(item.id, item.quantity - 1)
|
||||
}}
|
||||
>
|
||||
<Minus className="h-4 w-4" />
|
||||
</Button>
|
||||
@@ -71,20 +85,37 @@ export function CartItems() {
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleUpdateQuantity(item.id, item.quantity + 1)}
|
||||
onClick={(e) => {
|
||||
handleButtonClick(e)
|
||||
handleUpdateQuantity(item.id, item.quantity + 1)
|
||||
}}
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<Button variant="ghost" size="icon" onClick={() => handleToggleFavorite(item)}>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={(e) => {
|
||||
handleButtonClick(e)
|
||||
handleToggleFavorite(item)
|
||||
}}
|
||||
>
|
||||
<Heart className={`h-4 w-4 ${isFavorite(item.id) ? "fill-red-500 text-red-500" : ""}`} />
|
||||
</Button>
|
||||
<Button variant="ghost" size="icon" onClick={() => removeAllFromCart(item.id)}>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={(e) => {
|
||||
handleButtonClick(e)
|
||||
removeAllFromCart(item.id)
|
||||
}}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<div className="text-lg font-bold">{item.price * item.quantity} ₽</div>
|
||||
<div className="text-sm text-muted-foreground">{item.price} ₽ за шт.</div>
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Button } from "@/components/ui/button"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { CheckCircle2, ExternalLink } from "lucide-react"
|
||||
import Cookies from "js-cookie"
|
||||
|
||||
// Mock function for generating YooMoney payment link
|
||||
const generateYooMoneyPaymentLink = async (amount: number): Promise<string> => {
|
||||
@@ -26,6 +27,10 @@ const verifyYooMoneyPayment = async (): Promise<boolean> => {
|
||||
return true
|
||||
}
|
||||
|
||||
const setTotalPriceCookie = (totalPrice: number) => {
|
||||
Cookies.set("totalPrice", totalPrice.toString(), { expires: 1 }) // Expires in 1 day
|
||||
}
|
||||
|
||||
export function CheckoutForm() {
|
||||
const [address, setAddress] = useState("")
|
||||
const [isProcessing, setIsProcessing] = useState(false)
|
||||
@@ -48,6 +53,7 @@ export function CheckoutForm() {
|
||||
const handleInitiatePayment = async () => {
|
||||
setIsProcessing(true)
|
||||
try {
|
||||
setTotalPriceCookie(totalPrice)
|
||||
const link = await generateYooMoneyPaymentLink(totalPrice)
|
||||
setPaymentLink(link)
|
||||
setStep(2)
|
||||
|
||||
@@ -5,14 +5,13 @@ import Link from "next/link"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { Search } from "./search"
|
||||
import { Button } from "./ui/button"
|
||||
import { ShoppingCart, Heart, User, Menu, X } from "lucide-react"
|
||||
import { ShoppingCart, Heart, User, Menu } from "lucide-react"
|
||||
import { CatalogMenu } from "./catalog-menu"
|
||||
import { Sheet, SheetContent, SheetTrigger, SheetClose } from "./ui/sheet"
|
||||
import { Sheet, SheetContent, SheetTrigger } from "./ui/sheet"
|
||||
import { useCart } from "@/contexts/cart-context"
|
||||
import { useFavorites } from "@/contexts/favorites-context"
|
||||
import { useAuth } from "@/contexts/auth-context"
|
||||
import { Badge } from "./ui/badge"
|
||||
import { CartSummary } from "./cart-summary"
|
||||
|
||||
export function Header() {
|
||||
const [isMenuOpen, setIsMenuOpen] = useState(false)
|
||||
@@ -24,96 +23,21 @@ export function Header() {
|
||||
|
||||
const handleNavigate = (path: string) => {
|
||||
router.push(path)
|
||||
setIsMenuOpen(false) // Закрываем меню после навигации
|
||||
setIsMenuOpen(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<header className="border-b sticky top-0 bg-white z-50">
|
||||
<div className="container mx-auto px-4 py-4">
|
||||
<div className="flex flex-wrap items-center justify-between">
|
||||
<div className="flex items-center w-full sm:w-auto mb-4 sm:mb-0">
|
||||
<Link href="/" className="text-2xl font-bold text-blue-600 mr-4">
|
||||
STORE
|
||||
<div className="flex flex-col sm:flex-row items-center justify-between gap-4 sm:gap-8">
|
||||
{/* Левая секция: логотип и каталог */}
|
||||
<div className="flex items-center gap-4 w-full sm:w-auto justify-between sm:justify-start">
|
||||
<Link href="/" className="text-2xl font-bold text-blue-600">
|
||||
ETERNOS
|
||||
</Link>
|
||||
<div className="hidden sm:block">
|
||||
<CatalogMenu />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center w-full sm:flex-1 order-3 sm:order-2 mb-4 sm:mb-0">
|
||||
<div className="flex-grow mr-4">
|
||||
<Search />
|
||||
</div>
|
||||
<div className="hidden sm:flex items-center gap-4">
|
||||
<Button variant="ghost" size="icon" asChild>
|
||||
<Link href={isLoggedIn ? "/profile" : "/login"}>
|
||||
<User className="h-5 w-5" />
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="ghost" size="icon" className="relative" asChild>
|
||||
<Link href="/favorites">
|
||||
<Heart className="h-5 w-5" />
|
||||
{getTotalFavorites() > 0 && (
|
||||
<Badge
|
||||
variant="destructive"
|
||||
className="absolute -top-2 -right-2 h-5 w-5 flex items-center justify-center p-0"
|
||||
>
|
||||
{getTotalFavorites()}
|
||||
</Badge>
|
||||
)}
|
||||
</Link>
|
||||
</Button>
|
||||
<Link href="/cart">
|
||||
<Button variant="ghost" size="icon" className="relative">
|
||||
<ShoppingCart className="h-5 w-5" />
|
||||
</Button>
|
||||
</Link>
|
||||
|
||||
{/* <Sheet open={isCartOpen} onOpenChange={setIsCartOpen}>
|
||||
<SheetTrigger asChild>
|
||||
<Button variant="ghost" size="icon" className="relative">
|
||||
<ShoppingCart className="h-5 w-5" />
|
||||
<Link href="/cart" onClick={() => setIsCartOpen(false)}>
|
||||
|
||||
</Link>
|
||||
{getTotalUniqueItems() > 0 && (
|
||||
<Badge
|
||||
variant="destructive"
|
||||
className="absolute -top-2 -right-2 h-5 w-5 flex items-center justify-center p-0"
|
||||
>
|
||||
{getTotalUniqueItems()}
|
||||
</Badge>
|
||||
)}
|
||||
</Button>
|
||||
</SheetTrigger>
|
||||
<SheetContent side="right" className="w-full sm:max-w-md">
|
||||
|
||||
<div className="mt-6">
|
||||
<Button asChild className="w-full">
|
||||
<Link href="/cart" onClick={() => setIsCartOpen(false)}>
|
||||
Перейти в корзину
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h2 className="text-2xl font-bold">Корзина</h2>
|
||||
<SheetClose asChild>
|
||||
<Button variant="ghost" size="icon">
|
||||
<X className="h-5 w-5" />
|
||||
</Button>
|
||||
</SheetClose>
|
||||
</div>
|
||||
<CartSummary />
|
||||
<div className="mt-6">
|
||||
<Button asChild className="w-full">
|
||||
<Link href="/cart" onClick={() => setIsCartOpen(false)}>
|
||||
Перейти в корзину
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
</SheetContent>
|
||||
</Sheet> */}
|
||||
</div>
|
||||
<div className="sm:hidden">
|
||||
<Sheet open={isMenuOpen} onOpenChange={setIsMenuOpen}>
|
||||
<SheetTrigger asChild>
|
||||
@@ -121,39 +45,87 @@ export function Header() {
|
||||
<Menu className="h-5 w-5" />
|
||||
</Button>
|
||||
</SheetTrigger>
|
||||
<SheetContent side="right" className="h-[100vh] pt-16">
|
||||
<nav className="flex flex-col gap-4">
|
||||
<SheetContent
|
||||
side="right"
|
||||
className="w-[300px] h-[100vh] border-l"
|
||||
// Отключаем стандартную кнопку закрытия
|
||||
closeButton={false}
|
||||
>
|
||||
<nav className="flex flex-col gap-4 pt-2">
|
||||
<div className="border-b pb-4">
|
||||
<CatalogMenu onSelect={() => setIsMenuOpen(false)} />
|
||||
</div>
|
||||
<button
|
||||
onClick={() => handleNavigate(isLoggedIn ? "/profile" : "/login")}
|
||||
<Link
|
||||
href={isLoggedIn ? "/profile" : "/login"}
|
||||
className="flex items-center gap-2 p-2 hover:bg-gray-100 rounded-md"
|
||||
onClick={() => setIsMenuOpen(false)}
|
||||
>
|
||||
<User className="h-5 w-5" />
|
||||
<span>Личный кабинет</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleNavigate("/favorites")}
|
||||
</Link>
|
||||
<Link
|
||||
href="/favorites"
|
||||
className="flex items-center gap-2 p-2 hover:bg-gray-100 rounded-md"
|
||||
onClick={() => setIsMenuOpen(false)}
|
||||
>
|
||||
<Heart className="h-5 w-5" />
|
||||
<span>Избранное</span>
|
||||
{getTotalFavorites() > 0 && <Badge variant="destructive">{getTotalFavorites()}</Badge>}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleNavigate("/cart")}
|
||||
</Link>
|
||||
<Link
|
||||
href="/cart"
|
||||
className="flex items-center gap-2 p-2 hover:bg-gray-100 rounded-md"
|
||||
onClick={() => setIsMenuOpen(false)}
|
||||
>
|
||||
<ShoppingCart className="h-5 w-5" />
|
||||
<span>Корзина</span>
|
||||
{getTotalUniqueItems() > 0 && <Badge variant="destructive">{getTotalUniqueItems()}</Badge>}
|
||||
</button>
|
||||
</Link>
|
||||
</nav>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Центральная секция: поиск */}
|
||||
<div className="w-full sm:max-w-lg">
|
||||
<Search />
|
||||
</div>
|
||||
|
||||
{/* Правая секция: кнопки действий */}
|
||||
<div className="hidden sm:flex items-center gap-6 justify-end">
|
||||
<Button variant="ghost" size="icon" asChild>
|
||||
<Link href={isLoggedIn ? "/profile" : "/login"}>
|
||||
<User className="h-5 w-5" />
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="ghost" size="icon" className="relative" asChild>
|
||||
<Link href="/favorites">
|
||||
<Heart className="h-5 w-5" />
|
||||
{getTotalFavorites() > 0 && (
|
||||
<Badge
|
||||
variant="destructive"
|
||||
className="absolute -top-2 -right-2 h-5 w-5 flex items-center justify-center p-0"
|
||||
>
|
||||
{getTotalFavorites()}
|
||||
</Badge>
|
||||
)}
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="ghost" size="icon" className="relative" asChild>
|
||||
<Link href="/cart">
|
||||
<ShoppingCart className="h-5 w-5" />
|
||||
{getTotalUniqueItems() > 0 && (
|
||||
<Badge
|
||||
variant="destructive"
|
||||
className="absolute -top-2 -right-2 h-5 w-5 flex items-center justify-center p-0"
|
||||
>
|
||||
{getTotalUniqueItems()}
|
||||
</Badge>
|
||||
)}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
@@ -12,6 +12,14 @@ import (
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Price float64 `json:"price"`
|
||||
Description string `json:"description"`
|
||||
ImageURL string `json:"image_url"`
|
||||
}
|
||||
|
||||
type Review struct {
|
||||
ID int `json:"id"`
|
||||
ProductID int `json:"product_id"`
|
||||
@@ -25,16 +33,22 @@ var db *sql.DB
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", "./reviews.db")
|
||||
db, err = sql.Open("sqlite3", "./products.db")
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка открытия БД:", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
createTable()
|
||||
createTables()
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/product/{product_id:[0-9]+}", getReviews).Methods("GET")
|
||||
r.HandleFunc("/product/{product_id:[0-9]+}", addReview).Methods("POST")
|
||||
|
||||
// Маршруты для товаров
|
||||
r.HandleFunc("/product/{id}", getProductHandler).Methods("GET")
|
||||
|
||||
// Маршруты для отзывов
|
||||
r.HandleFunc("/product/{product_id:[0-9]+}/reviews", getReviews).Methods("GET")
|
||||
r.HandleFunc("/product/{product_id:[0-9]+}/reviews", addReview).Methods("POST")
|
||||
|
||||
corsHandler := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"http://localhost:3000"},
|
||||
@@ -43,27 +57,69 @@ func main() {
|
||||
AllowCredentials: true,
|
||||
}).Handler(r)
|
||||
|
||||
log.Println("Server is running on port 8080...")
|
||||
log.Println("Сервер запущен на порту 8080...")
|
||||
if err := http.ListenAndServe(":8080", corsHandler); err != nil {
|
||||
log.Fatal("Ошибка запуска сервера:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createTable() {
|
||||
func createTables() {
|
||||
// Создание таблицы товаров
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
price REAL NOT NULL,
|
||||
description TEXT,
|
||||
image_url TEXT
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка создания таблицы products:", err)
|
||||
}
|
||||
|
||||
// Создание таблицы отзывов
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS reviews (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
product_id INTEGER NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
rating INTEGER NOT NULL CHECK(rating >= 1 AND rating <= 5),
|
||||
comment TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id)
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка создания таблицы:", err)
|
||||
log.Fatal("Ошибка создания таблицы reviews:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func getProductHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id, err := strconv.Atoi(vars["id"])
|
||||
if err != nil {
|
||||
http.Error(w, "Некорректный ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var product Product
|
||||
err = db.QueryRow("SELECT id, title, price, description, image_url FROM products WHERE id = ?", id).
|
||||
Scan(&product.ID, &product.Title, &product.Price, &product.Description, &product.ImageURL)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
http.Error(w, "Товар не найден", http.StatusNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
log.Println("Ошибка получения товара:", err)
|
||||
http.Error(w, "Ошибка сервера", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(product)
|
||||
}
|
||||
|
||||
func addReview(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
productID, err := strconv.Atoi(vars["product_id"])
|
||||
@@ -80,7 +136,19 @@ func addReview(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Игнорируем переданный в JSON product_id и устанавливаем его из URL
|
||||
// Проверяем существование товара
|
||||
var exists bool
|
||||
err = db.QueryRow("SELECT EXISTS(SELECT 1 FROM products WHERE id = ?)", productID).Scan(&exists)
|
||||
if err != nil {
|
||||
http.Error(w, "Ошибка проверки существования товара", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
http.Error(w, "Товар не найден", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Устанавливаем product_id из URL
|
||||
review.ProductID = productID
|
||||
log.Printf("Добавление отзыва для товара %d: %+v", productID, review)
|
||||
|
||||
@@ -102,10 +170,9 @@ func addReview(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
log.Printf("Добавлено строк: %d для товара %d", rowsAffected, productID)
|
||||
id, _ := result.LastInsertId()
|
||||
review.ID = int(id)
|
||||
|
||||
// Возвращаем созданный отзыв
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(review)
|
||||
@@ -143,7 +210,6 @@ func getReviews(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Printf("Найдено %d отзывов для товара %s", len(reviews), productID)
|
||||
|
||||
// Всегда возвращаем JSON массив, даже если он пустой
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(reviews)
|
||||
}
|
||||
@@ -54,7 +54,7 @@ export function ProductDetail({ product }: ProductDetailProps) {
|
||||
<div className="flex flex-col md:flex-row gap-8">
|
||||
<div className="md:w-1/2">
|
||||
<Image
|
||||
src={product.images?.[0] || "/placeholder.svg"}
|
||||
src={product.image_url || "/placeholder.svg"}
|
||||
alt={product.title}
|
||||
width={500}
|
||||
height={500}
|
||||
|
||||
BIN
frontend/style/components/product.db
Normal file
BIN
frontend/style/components/product.db
Normal file
Binary file not shown.
0
frontend/style/components/product.mod
Normal file
0
frontend/style/components/product.mod
Normal file
27
frontend/style/components/product.ts
Normal file
27
frontend/style/components/product.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export interface Review {
|
||||
id: number
|
||||
userId: number
|
||||
rating: number
|
||||
comment: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface Seller {
|
||||
id: number
|
||||
name: string
|
||||
shopName: string
|
||||
}
|
||||
|
||||
export interface Product {
|
||||
id: number
|
||||
title: string
|
||||
price: number
|
||||
description: string
|
||||
image_url: string
|
||||
category?: string
|
||||
brand?: string
|
||||
licenseType?: string
|
||||
reviews?: Review[]
|
||||
seller?: Seller
|
||||
}
|
||||
|
||||
BIN
frontend/style/components/products.db
Normal file
BIN
frontend/style/components/products.db
Normal file
Binary file not shown.
@@ -1,14 +1,14 @@
|
||||
"use client"
|
||||
|
||||
import type React from "react"
|
||||
import { useState } from "react"
|
||||
import { Button } from "./ui/button"
|
||||
import { Textarea } from "./ui/textarea"
|
||||
import { Label } from "./ui/label"
|
||||
import { LogIn, Star } from 'lucide-react'
|
||||
import { Star } from 'lucide-react'
|
||||
import { useAuth } from "@/contexts/auth-context"
|
||||
import Link from "next/link"
|
||||
|
||||
|
||||
interface ReviewFormProps {
|
||||
productId: number
|
||||
}
|
||||
@@ -16,43 +16,69 @@ interface ReviewFormProps {
|
||||
export function ReviewForm({ productId }: ReviewFormProps) {
|
||||
const [rating, setRating] = useState(0)
|
||||
const [comment, setComment] = useState("")
|
||||
const { isLoggedIn } = useAuth()
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const { isLoggedIn, user } = useAuth()
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
|
||||
const reviewData = {
|
||||
username: "No name", // Здесь можно подтянуть имя из Auth-контекста
|
||||
rating,
|
||||
comment,
|
||||
}
|
||||
setIsSubmitting(true)
|
||||
|
||||
try {
|
||||
|
||||
const response = await fetch(`http://localhost:8080/product/${productId}`, {
|
||||
// Добавляем console.log для отладки
|
||||
console.log('Submitting review with data:', {
|
||||
productId,
|
||||
rating,
|
||||
comment,
|
||||
username: user?.name || 'Anonymous'
|
||||
});
|
||||
|
||||
const reviewData = {
|
||||
product_id: productId,
|
||||
username: user?.name || "Anonymous",
|
||||
rating: rating,
|
||||
comment: comment
|
||||
}
|
||||
|
||||
const response = await fetch(`http://localhost:8080/product/${productId}/reviews`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(reviewData),
|
||||
body: JSON.stringify(reviewData)
|
||||
})
|
||||
|
||||
// Добавляем console.log для отладки ответа
|
||||
console.log('Response status:', response.status);
|
||||
const responseData = await response.json();
|
||||
console.log('Response data:', responseData);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Ошибка при отправке отзыва")
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
console.log("Отзыв успешно отправлен!")
|
||||
// Очищаем форму после успешной отправки
|
||||
setRating(0)
|
||||
setComment("")
|
||||
alert("Отзыв успешно добавлен!")
|
||||
|
||||
// Перезагружаем страницу для отображения нового отзыва
|
||||
window.location.reload()
|
||||
} catch (error) {
|
||||
console.error("Ошибка:", error)
|
||||
console.error("Error submitting review:", error)
|
||||
alert("Произошла ошибка при отправке отзыва. Пожалуйста, попробуйте еще раз.")
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isLoggedIn) {
|
||||
return (
|
||||
<p className="text-gray-600">
|
||||
Чтобы оставить отзыв, пожалуйста, <Link href="/login" className="text-blue-600 hover:underline">войдите в систему</Link>.
|
||||
Чтобы оставить отзыв, пожалуйста,{" "}
|
||||
<Link href="/login" className="text-blue-600 hover:underline">
|
||||
войдите в систему
|
||||
</Link>
|
||||
.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
@@ -63,20 +89,27 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
||||
<Label htmlFor="rating">Рейтинг</Label>
|
||||
<div className="flex items-center gap-1">
|
||||
{[1, 2, 3, 4, 5].map((star) => (
|
||||
<button
|
||||
key={star}
|
||||
type="button"
|
||||
onClick={() => setRating(star)}
|
||||
<button
|
||||
key={star}
|
||||
type="button"
|
||||
onClick={() => setRating(star)}
|
||||
className="focus:outline-none"
|
||||
>
|
||||
<Star
|
||||
<Star
|
||||
className={`h-6 w-6 ${
|
||||
star <= rating ? 'text-yellow-400 fill-yellow-400' : 'text-gray-300'
|
||||
}`}
|
||||
star <= rating ? "text-yellow-400 fill-yellow-400" : "text-gray-300"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{/* Добавляем скрытое поле для отображения выбранного рейтинга */}
|
||||
<input
|
||||
type="hidden"
|
||||
name="rating"
|
||||
value={rating}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="comment">Комментарий</Label>
|
||||
@@ -86,10 +119,14 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
||||
onChange={(e) => setComment(e.target.value)}
|
||||
rows={4}
|
||||
placeholder="Напишите ваш отзыв здесь..."
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<Button type="submit" disabled={rating === 0 || comment.trim() === ""}>
|
||||
Отправить отзыв
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isSubmitting || rating === 0 || comment.trim() === ""}
|
||||
>
|
||||
{isSubmitting ? "Отправка..." : "Отправить отзыв"}
|
||||
</Button>
|
||||
</form>
|
||||
)
|
||||
|
||||
@@ -30,7 +30,7 @@ export function ReviewList({ productId }: ReviewListProps) {
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const response = await fetch(`http://localhost:8080/product/${productId}`)
|
||||
const response = await fetch(`http://localhost:8080/product/${productId}/reviews`)
|
||||
|
||||
// Добавляем логирование для отладки
|
||||
console.log("Response status:", response.status)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user