59 lines
2.2 KiB
JavaScript
59 lines
2.2 KiB
JavaScript
|
|
"use client";
|
||
|
|
import { useEffect } from "react";
|
||
|
|
import { useRouter, usePathname, useSearchParams } from "next/navigation";
|
||
|
|
import { imprimirPlan } from "./imprimir";
|
||
|
|
|
||
|
|
const addDays = (iso, n) => {
|
||
|
|
const d = new Date(iso + "T12:00:00Z");
|
||
|
|
d.setUTCDate(d.getUTCDate() + n);
|
||
|
|
return d.toISOString().slice(0, 10);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Selector de fecha + botones de PDF del Plan del Día. La fecha vive en la URL (`fecha`), igual que
|
||
|
|
// el resto de filtros, así el enlace se puede compartir. El PDF sale del diálogo de impresión del
|
||
|
|
// navegador (ver imprimir.js).
|
||
|
|
export default function PlanControles({ fecha, hoy, tituloPdf, tituloPdfEquipo, conEquipo }) {
|
||
|
|
const router = useRouter();
|
||
|
|
const pathname = usePathname();
|
||
|
|
const searchParams = useSearchParams();
|
||
|
|
|
||
|
|
function irA(nueva) {
|
||
|
|
const params = new URLSearchParams(searchParams.toString());
|
||
|
|
if (!nueva || nueva === hoy) params.delete("fecha");
|
||
|
|
else params.set("fecha", nueva);
|
||
|
|
const qs = params.toString();
|
||
|
|
router.push(qs ? `${pathname}?${qs}` : pathname, { scroll: false });
|
||
|
|
}
|
||
|
|
|
||
|
|
// Si por algún motivo el diálogo se cierra sin disparar afterprint, no se deja el modo impresión puesto.
|
||
|
|
useEffect(() => () => document.body.classList.remove("plan-print", "plan-print-equipo"), []);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="plan-controles no-print">
|
||
|
|
<button type="button" className="ag-nav-b" aria-label="Día anterior" onClick={() => irA(addDays(fecha, -1))}>
|
||
|
|
←
|
||
|
|
</button>
|
||
|
|
<input
|
||
|
|
type="date"
|
||
|
|
className="plan-fecha"
|
||
|
|
value={fecha}
|
||
|
|
onChange={(e) => e.target.value && irA(e.target.value)}
|
||
|
|
/>
|
||
|
|
<button type="button" className="ag-nav-b" aria-label="Día siguiente" onClick={() => irA(addDays(fecha, 1))}>
|
||
|
|
→
|
||
|
|
</button>
|
||
|
|
<button type="button" className="ag-nav-b" onClick={() => irA(hoy)} disabled={fecha === hoy}>
|
||
|
|
Hoy
|
||
|
|
</button>
|
||
|
|
<button type="button" className="rep-btn" onClick={() => imprimirPlan({ titulo: tituloPdf })}>
|
||
|
|
🖨 Exportar PDF
|
||
|
|
</button>
|
||
|
|
{conEquipo && (
|
||
|
|
<button type="button" className="rep-btn" onClick={() => imprimirPlan({ titulo: tituloPdfEquipo, equipo: true })}>
|
||
|
|
🖨 PDF del equipo
|
||
|
|
</button>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|