Refactor Informacion component to improve seat selection logic and enhance data fetching for seats
This commit is contained in:
parent
b846835124
commit
c94e41b117
ventaboletos/src
|
@ -52,9 +52,7 @@ function Informacion() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchAsientos = async () => {
|
const fetchAsientos = async () => {
|
||||||
let { data, error } = await supabaseClient
|
let { data, error } = await supabaseClient.from("asientos").select("*"); // Include price in the select
|
||||||
.from("asientos")
|
|
||||||
.select("numero_asiento, categoria, precio"); // Include price in the select
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,7 +78,7 @@ function Informacion() {
|
||||||
const handleAsientoChange = (asiento) => {
|
const handleAsientoChange = (asiento) => {
|
||||||
setAsientoSeleccionado(asiento);
|
setAsientoSeleccionado(asiento);
|
||||||
const selectedAsiento = numeroAsiento.find(
|
const selectedAsiento = numeroAsiento.find(
|
||||||
(item) => item.numero_asiento === asiento
|
(item) => item.numero === parseInt(asiento)
|
||||||
);
|
);
|
||||||
setPrecioAsiento(selectedAsiento?.precio || null); // Set the price of the selected seat
|
setPrecioAsiento(selectedAsiento?.precio || null); // Set the price of the selected seat
|
||||||
};
|
};
|
||||||
|
@ -90,20 +88,26 @@ function Informacion() {
|
||||||
alert("Selecciona un tipo de asiento antes de continuar.");
|
alert("Selecciona un tipo de asiento antes de continuar.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const carritoActual = JSON.parse(localStorage.getItem("carrito")) || [];
|
const carritoActual = JSON.parse(localStorage.getItem("carrito")) || [];
|
||||||
const nuevoCarrito = [
|
const nuevoCarrito = [
|
||||||
...carritoActual,
|
...carritoActual,
|
||||||
{ ...selectedConcierto, asiento: asientoSeleccionado, precio: precioAsiento },
|
{
|
||||||
|
...selectedConcierto,
|
||||||
|
asiento: asientoSeleccionado,
|
||||||
|
precio: precioAsiento,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
localStorage.setItem("carrito", JSON.stringify(nuevoCarrito));
|
localStorage.setItem("carrito", JSON.stringify(nuevoCarrito));
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const filteredAsientos = numeroAsiento
|
const filteredAsientos = numeroAsiento
|
||||||
.filter((asiento) => asiento.categoria === selectedCategoria)
|
.filter((asiento) => {
|
||||||
.sort((a, b) => a.numero_asiento - b.numero_asiento);
|
return asiento.categoria === selectedCategoria;
|
||||||
|
})
|
||||||
|
.sort((a, b) => parseInt(a.numero) - parseInt(b.numero));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -123,7 +127,7 @@ function Informacion() {
|
||||||
<div
|
<div
|
||||||
key={concierto.id}
|
key={concierto.id}
|
||||||
className="flex items-center gap-4 p-4 border rounded-lg"
|
className="flex items-center gap-4 p-4 border rounded-lg"
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={`/images/${concierto.imagen || "default.jpeg"}`}
|
src={`/images/${concierto.imagen || "default.jpeg"}`}
|
||||||
alt={concierto.nombre}
|
alt={concierto.nombre}
|
||||||
|
@ -141,8 +145,13 @@ function Informacion() {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<DialogContent>
|
<DialogContent className="bg-white text-black">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
|
<img
|
||||||
|
src={`/images/default.jpeg`}
|
||||||
|
alt={""}
|
||||||
|
className="w-24 h-24 rounded-lg object-cover"
|
||||||
|
/>
|
||||||
<DialogTitle>{selectedConcierto?.nombre}</DialogTitle>
|
<DialogTitle>{selectedConcierto?.nombre}</DialogTitle>
|
||||||
<p className="text-sm text-gray-600">{selectedConcierto?.fecha}</p>
|
<p className="text-sm text-gray-600">{selectedConcierto?.fecha}</p>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
@ -167,11 +176,8 @@ function Informacion() {
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
{filteredAsientos.map((asiento) => (
|
{filteredAsientos.map((asiento) => (
|
||||||
<SelectItem
|
<SelectItem key={asiento.id} value={asiento.numero}>
|
||||||
key={asiento.numero_asiento}
|
{asiento.numero}
|
||||||
value={asiento.numero_asiento}
|
|
||||||
>
|
|
||||||
{asiento.numero_asiento}
|
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
))}
|
))}
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
|
@ -179,7 +185,9 @@ function Informacion() {
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<Label>
|
<Label>
|
||||||
{precioAsiento !== null ? `Precio: $${precioAsiento}` : "Selecciona un asiento para ver el precio"}
|
{precioAsiento !== null
|
||||||
|
? `Precio: $${precioAsiento}`
|
||||||
|
: "Selecciona un asiento para ver el precio"}
|
||||||
</Label>
|
</Label>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-end gap-2 mt-4">
|
<div className="flex justify-end gap-2 mt-4">
|
||||||
|
@ -194,4 +202,4 @@ function Informacion() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Informacion;
|
export default Informacion;
|
||||||
|
|
|
@ -1,12 +1,27 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import Link from "next/link";
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
} from "@/components/ui/select";
|
||||||
|
|
||||||
export default function Carrito() {
|
export default function Carrito() {
|
||||||
const [carrito, setCarrito] = useState([]);
|
const [carrito, setCarrito] = useState([]);
|
||||||
const [showDialog, setShowDialog] = useState(false);
|
const [showDialog, setShowDialog] = useState(false);
|
||||||
|
const [showPaymentDialog, setShowPaymentDialog] = useState(false);
|
||||||
const [conciertoAEliminar, setConciertoAEliminar] = useState(null);
|
const [conciertoAEliminar, setConciertoAEliminar] = useState(null);
|
||||||
|
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState("");
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -16,7 +31,9 @@ export default function Carrito() {
|
||||||
|
|
||||||
const eliminarConcierto = () => {
|
const eliminarConcierto = () => {
|
||||||
if (conciertoAEliminar !== null) {
|
if (conciertoAEliminar !== null) {
|
||||||
const nuevoCarrito = carrito.filter((_, index) => index !== conciertoAEliminar);
|
const nuevoCarrito = carrito.filter(
|
||||||
|
(_, index) => index !== conciertoAEliminar
|
||||||
|
);
|
||||||
localStorage.setItem("carrito", JSON.stringify(nuevoCarrito));
|
localStorage.setItem("carrito", JSON.stringify(nuevoCarrito));
|
||||||
setCarrito(nuevoCarrito);
|
setCarrito(nuevoCarrito);
|
||||||
setShowDialog(false);
|
setShowDialog(false);
|
||||||
|
@ -27,7 +44,26 @@ export default function Carrito() {
|
||||||
return carrito.reduce((total, item) => total + (item.precio || 0), 0);
|
return carrito.reduce((total, item) => total + (item.precio || 0), 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
const pagar = async () => {
|
const handlePagar = async () => {
|
||||||
|
if (!selectedPaymentMethod) {
|
||||||
|
alert("Por favor seleccione un método de pago");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Aquí iría la lógica de pago
|
||||||
|
alert("Pago realizado con éxito");
|
||||||
|
localStorage.removeItem("carrito");
|
||||||
|
setCarrito([]);
|
||||||
|
setShowPaymentDialog(false);
|
||||||
|
router.push("/");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error en el pago:", error);
|
||||||
|
alert("Hubo un problema al realizar el pago");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/*const pagar = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/comprar-boletos", {
|
const response = await fetch("/api/comprar-boletos", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -49,63 +85,134 @@ export default function Carrito() {
|
||||||
console.error("Error en la compra:", error);
|
console.error("Error en la compra:", error);
|
||||||
alert("Hubo un problema al realizar la compra");
|
alert("Hubo un problema al realizar la compra");
|
||||||
}
|
}
|
||||||
};
|
};*/
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="p-4 max-w-3xl mx-auto bg-gray-900 text-white rounded-lg shadow-lg">
|
<div className="w-screen flex flex-col items-center justify-center">
|
||||||
<h2 className="text-2xl font-bold mb-4">Carrito de Compras</h2>
|
<div className="w-[80%] my-5">
|
||||||
{carrito.length === 0 ? (
|
<Link href={"/"}>
|
||||||
<p className="text-gray-400">No hay conciertos en el carrito.</p>
|
<Button className="bg-blue-500 hover:bg-blue-600">Volver</Button>
|
||||||
) : (
|
</Link>
|
||||||
carrito.map((item, index) => (
|
</div>
|
||||||
<div key={index} className="border p-4 mb-2 rounded-lg bg-gray-800">
|
<div className="mx-10 py-5 px-10 text-black rounded-lg shadow-lg w-[80%]">
|
||||||
<p className="text-lg font-semibold text-white">{item.nombre}</p>
|
<h2 className="text-2xl font-bold mb-4">Carrito de Compras</h2>
|
||||||
<p className="text-sm text-gray-300">Fecha: {item.fecha}</p>
|
{carrito.length === 0 ? (
|
||||||
<p className="text-sm text-gray-300">Asiento: {item.asiento}</p>
|
<p className="text-gray-400">No hay conciertos en el carrito.</p>
|
||||||
<p className="text-sm text-gray-300">Precio: ${item.precio}</p>
|
) : (
|
||||||
<Button
|
carrito.map((item, index) => (
|
||||||
className="mt-2 bg-red-500 hover:bg-red-600"
|
<div key={index} className="border p-4 mb-2 rounded-lg bg-gray-800">
|
||||||
onClick={() => {
|
<p className="text-lg font-semibold text-white">{item.nombre}</p>
|
||||||
setConciertoAEliminar(index);
|
<p className="text-sm text-gray-300">Fecha: {item.fecha}</p>
|
||||||
setShowDialog(true);
|
<p className="text-sm text-gray-300">Asiento: {item.asiento}</p>
|
||||||
}}
|
<p className="text-sm text-gray-300">Precio: ${item.precio}</p>
|
||||||
>
|
<Button
|
||||||
Eliminar
|
className="mt-2 bg-red-500 hover:bg-red-600"
|
||||||
</Button>
|
onClick={() => {
|
||||||
</div>
|
setConciertoAEliminar(index);
|
||||||
))
|
setShowDialog(true);
|
||||||
)}
|
}}
|
||||||
|
>
|
||||||
|
Eliminar
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
{carrito.length > 0 && (
|
||||||
|
<>
|
||||||
|
<p className="text-xl font-bold text-white mt-4">
|
||||||
|
Total: ${calcularTotal()}
|
||||||
|
</p>
|
||||||
|
<div className="flex justify-between mt-4">
|
||||||
|
<Button
|
||||||
|
className="bg-gray-500 hover:bg-gray-600"
|
||||||
|
onClick={() => router.push("/")}
|
||||||
|
>
|
||||||
|
Cancelar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
className="bg-green-500 hover:bg-green-600"
|
||||||
|
onClick={() => setShowPaymentDialog(true)}
|
||||||
|
>
|
||||||
|
Pagar
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
{carrito.length > 0 && (
|
<Dialog open={showDialog} onOpenChange={setShowDialog}>
|
||||||
<>
|
{/* ...existing delete dialog code... */}
|
||||||
<p className="text-xl font-bold text-white mt-4">Total: ${calcularTotal()}</p>
|
</Dialog>
|
||||||
<div className="flex justify-between mt-4">
|
|
||||||
<Button
|
|
||||||
className="bg-gray-500 hover:bg-gray-600"
|
|
||||||
onClick={() => router.push("/")}
|
|
||||||
>
|
|
||||||
Cancelar
|
|
||||||
</Button>
|
|
||||||
<Button className="bg-green-500 hover:bg-green-600" onClick={pagar}>Pagar</Button>
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Dialog open={showDialog} onOpenChange={setShowDialog}>
|
{/* Dialog para pago */}
|
||||||
<DialogContent>
|
<Dialog open={showPaymentDialog} onOpenChange={setShowPaymentDialog}>
|
||||||
<DialogHeader>
|
<DialogContent className="bg-white text-black">
|
||||||
<DialogTitle>¿Estás seguro de eliminar este concierto?</DialogTitle>
|
<DialogHeader>
|
||||||
</DialogHeader>
|
<DialogTitle className="text-xl font-bold">
|
||||||
<div className="flex justify-end gap-2 mt-4">
|
Seleccione método de pago
|
||||||
<Button variant="outline" onClick={() => setShowDialog(false)}>
|
</DialogTitle>
|
||||||
Cancelar
|
</DialogHeader>
|
||||||
</Button>
|
<div className="space-y-4">
|
||||||
<Button className="bg-red-500 hover:bg-red-600" onClick={eliminarConcierto}>
|
<Select onValueChange={setSelectedPaymentMethod}>
|
||||||
Eliminar
|
<SelectTrigger>
|
||||||
</Button>
|
<SelectValue placeholder="Seleccione método de pago" />
|
||||||
</div>
|
</SelectTrigger>
|
||||||
</DialogContent>
|
<SelectContent>
|
||||||
</Dialog>
|
<SelectItem value="tarjeta">
|
||||||
|
Tarjeta de Crédito/Débito
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value="paypal">PayPal</SelectItem>
|
||||||
|
<SelectItem value="transferencia">
|
||||||
|
Transferencia Bancaria
|
||||||
|
</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
|
||||||
|
<div className="mt-4">
|
||||||
|
<p className="font-semibold">
|
||||||
|
Total a pagar: ${calcularTotal()}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-2 mt-4">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => setShowPaymentDialog(false)}
|
||||||
|
>
|
||||||
|
Cancelar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
className="bg-green-500 hover:bg-green-600 text-white"
|
||||||
|
onClick={handlePagar}
|
||||||
|
disabled={!selectedPaymentMethod}
|
||||||
|
>
|
||||||
|
Confirmar Pago
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
<Dialog open={showDialog} onOpenChange={setShowDialog}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>
|
||||||
|
¿Estás seguro de eliminar este concierto?
|
||||||
|
</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="flex justify-end gap-2 mt-4">
|
||||||
|
<Button variant="outline" onClick={() => setShowDialog(false)}>
|
||||||
|
Cancelar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
className="bg-red-500 hover:bg-red-600"
|
||||||
|
onClick={eliminarConcierto}
|
||||||
|
>
|
||||||
|
Eliminar
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue