282 lines
8.3 KiB
TypeScript
282 lines
8.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { ChevronLeft, CalendarX2, RotateCw } from "lucide-react";
|
|
import { formatearFechaLarga, fechaISOLocal } from "@/lib/fechas";
|
|
|
|
interface SelectorHoraProps {
|
|
/** Fecha elegida en la etapa anterior. */
|
|
fecha: Date;
|
|
/** Se llama al confirmar un bloque de horario "HH:mm". */
|
|
onSeleccionar: (hora: string) => void;
|
|
/** Volver a la etapa de elegir fecha. */
|
|
onVolver: () => void;
|
|
/** Aviso opcional (p. ej. cuando un horario se ocupó y hay que reelegir). */
|
|
aviso?: string | null;
|
|
/** Texto del botón de confirmación (visual). */
|
|
etiquetaConfirmar?: string;
|
|
}
|
|
|
|
/**
|
|
* Etapa "hora": consulta la disponibilidad real del día a /api/disponibilidad
|
|
* y muestra solo los bloques libres. Maneja carga, vacío y error.
|
|
*
|
|
* El usuario marca un horario y lo confirma con el botón ("Continuar"); ahí se
|
|
* dispara onSeleccionar con el mismo "HH:mm" del backend.
|
|
*/
|
|
export default function SelectorHora({
|
|
fecha,
|
|
onSeleccionar,
|
|
onVolver,
|
|
aviso = null,
|
|
etiquetaConfirmar = "Continuar",
|
|
}: SelectorHoraProps) {
|
|
const [horarios, setHorarios] = useState<string[]>([]);
|
|
const [cargando, setCargando] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
// Cambiar este contador fuerza un reintento del fetch.
|
|
const [intento, setIntento] = useState(0);
|
|
// Horario marcado antes de confirmar (barra "Continuar").
|
|
const [slotElegido, setSlotElegido] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let activo = true;
|
|
|
|
async function cargar() {
|
|
setCargando(true);
|
|
setError(null);
|
|
try {
|
|
const res = await fetch(
|
|
`/api/disponibilidad?fecha=${fechaISOLocal(fecha)}`
|
|
);
|
|
const data = await res.json().catch(() => null);
|
|
if (!res.ok || !data) {
|
|
throw new Error(
|
|
data?.error ?? "No se pudieron cargar los horarios disponibles."
|
|
);
|
|
}
|
|
if (activo) setHorarios(Array.isArray(data.horarios) ? data.horarios : []);
|
|
} catch (e) {
|
|
if (activo) {
|
|
setError(
|
|
e instanceof Error
|
|
? e.message
|
|
: "Ocurrió un error al cargar los horarios."
|
|
);
|
|
}
|
|
} finally {
|
|
if (activo) setCargando(false);
|
|
}
|
|
}
|
|
|
|
cargar();
|
|
return () => {
|
|
activo = false;
|
|
};
|
|
}, [fecha, intento]);
|
|
|
|
return (
|
|
<div style={{ display: "flex", flexDirection: "column", gap: 20 }}>
|
|
<button type="button" className="btn-ghost" onClick={onVolver}>
|
|
<ChevronLeft size={16} strokeWidth={2.2} />
|
|
Volver al calendario
|
|
</button>
|
|
|
|
{/* Aviso (p. ej. tras un horario que se ocupó) */}
|
|
{aviso && (
|
|
<div
|
|
role="alert"
|
|
style={{
|
|
background: "rgba(232,43,123,0.12)",
|
|
border: "1px solid rgba(232,43,123,0.32)",
|
|
borderRadius: 14,
|
|
padding: "12px 14px",
|
|
fontSize: 13.5,
|
|
lineHeight: 1.5,
|
|
color: "rgba(248,232,240,0.82)",
|
|
}}
|
|
>
|
|
{aviso}
|
|
</div>
|
|
)}
|
|
|
|
<div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
|
|
<span className="eyebrow">Selecciona un horario</span>
|
|
<h2
|
|
className="serif"
|
|
style={{
|
|
margin: 0,
|
|
fontWeight: 600,
|
|
fontSize: 24,
|
|
letterSpacing: "-0.01em",
|
|
color: "#FBEFF6",
|
|
}}
|
|
>
|
|
{formatearFechaLarga(fecha)}
|
|
</h2>
|
|
</div>
|
|
|
|
{/* Estado: cargando */}
|
|
{cargando && (
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
gap: 16,
|
|
padding: "60px 0",
|
|
minHeight: 220,
|
|
}}
|
|
>
|
|
<div style={{ display: "flex", gap: 7 }}>
|
|
<span className="cdot" />
|
|
<span className="cdot" />
|
|
<span className="cdot" />
|
|
</div>
|
|
<span
|
|
style={{ fontSize: 14.5, fontWeight: 600, color: "rgba(248,232,240,0.55)" }}
|
|
>
|
|
Cargando horarios…
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Estado: error con reintento */}
|
|
{!cargando && error && (
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
alignItems: "center",
|
|
gap: 14,
|
|
padding: "48px 24px",
|
|
minHeight: 220,
|
|
textAlign: "center",
|
|
}}
|
|
>
|
|
<p style={{ margin: 0, fontSize: 15, fontWeight: 600, color: "#FF9E7A" }}>
|
|
{error}
|
|
</p>
|
|
<button
|
|
type="button"
|
|
className="btn-ghost"
|
|
onClick={() => setIntento((n) => n + 1)}
|
|
>
|
|
<RotateCw size={16} strokeWidth={2.2} />
|
|
Reintentar
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Estado: sin horarios */}
|
|
{!cargando && !error && horarios.length === 0 && (
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
gap: 12,
|
|
padding: "52px 24px",
|
|
minHeight: 220,
|
|
textAlign: "center",
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
width: 54,
|
|
height: 54,
|
|
borderRadius: "50%",
|
|
background: "rgba(255,255,255,0.05)",
|
|
border: "1px solid rgba(255,255,255,0.12)",
|
|
display: "flex",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
}}
|
|
>
|
|
<CalendarX2 size={24} style={{ color: "rgba(248,232,240,0.45)" }} aria-hidden />
|
|
</div>
|
|
<p style={{ margin: 0, fontSize: 15, fontWeight: 700, color: "#FBEFF6" }}>
|
|
No hay horarios disponibles este día
|
|
</p>
|
|
<p style={{ margin: 0, fontSize: 14, fontWeight: 500, color: "rgba(248,232,240,0.7)" }}>
|
|
Vuelve al calendario y elige otra fecha.
|
|
</p>
|
|
<button
|
|
type="button"
|
|
className="btn-ghost"
|
|
onClick={onVolver}
|
|
style={{ marginTop: 4 }}
|
|
>
|
|
<ChevronLeft size={16} strokeWidth={2.2} />
|
|
Elegir otra fecha
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Estado: lista de horarios disponibles */}
|
|
{!cargando && !error && horarios.length > 0 && (
|
|
<>
|
|
<div
|
|
className="slot-grid"
|
|
style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 12 }}
|
|
>
|
|
{horarios.map((hora) => (
|
|
<button
|
|
key={hora}
|
|
type="button"
|
|
className={"slot" + (slotElegido === hora ? " slot--sel" : "")}
|
|
onClick={() => setSlotElegido(hora)}
|
|
>
|
|
{hora}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{/* Barra de confirmación */}
|
|
{slotElegido && (
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
gap: 16,
|
|
background: "rgba(255,255,255,0.05)",
|
|
border: "1px solid rgba(255,255,255,0.10)",
|
|
borderRadius: 14,
|
|
padding: "14px 16px",
|
|
marginTop: 4,
|
|
}}
|
|
>
|
|
<div style={{ display: "flex", flexDirection: "column", gap: 2 }}>
|
|
<span
|
|
style={{
|
|
fontSize: 11,
|
|
fontWeight: 700,
|
|
letterSpacing: "0.1em",
|
|
textTransform: "uppercase",
|
|
color: "rgba(248,232,240,0.55)",
|
|
}}
|
|
>
|
|
Horario seleccionado
|
|
</span>
|
|
<span style={{ fontSize: 16, fontWeight: 700, color: "#FBEFF6" }}>
|
|
{slotElegido}
|
|
</span>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="btn-primary"
|
|
onClick={() => onSeleccionar(slotElegido)}
|
|
>
|
|
{etiquetaConfirmar}
|
|
</button>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|