35 lines
1.4 KiB
JavaScript
35 lines
1.4 KiB
JavaScript
|
|
"use client";
|
||
|
|
import { useRouter, usePathname, useSearchParams } from "next/navigation";
|
||
|
|
import { useEffect } from "react";
|
||
|
|
|
||
|
|
// Selector compacto de propietario (solo gerente y asistente). «Todos» = el equipo de ventas. Se recuerda en localStorage.
|
||
|
|
export default function OwnerM({ owners, actual }) {
|
||
|
|
const router = useRouter();
|
||
|
|
const path = usePathname();
|
||
|
|
const sp = useSearchParams();
|
||
|
|
useEffect(() => {
|
||
|
|
try {
|
||
|
|
const g = localStorage.getItem("campo.owner");
|
||
|
|
if (g && !sp.get("owner") && g !== "todos" && owners.some((o) => o.email === g)) {
|
||
|
|
const p = new URLSearchParams(sp.toString());
|
||
|
|
p.set("owner", g);
|
||
|
|
router.replace(`${path}?${p.toString()}`);
|
||
|
|
}
|
||
|
|
} catch { /* sin almacenamiento */ }
|
||
|
|
// solo al montar
|
||
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
|
|
}, []);
|
||
|
|
const cambiar = (v) => {
|
||
|
|
try { localStorage.setItem("campo.owner", v || "todos"); } catch { /* ignorar */ }
|
||
|
|
const p = new URLSearchParams(sp.toString());
|
||
|
|
if (v) p.set("owner", v); else p.delete("owner");
|
||
|
|
router.push(`${path}${p.toString() ? `?${p.toString()}` : ""}`);
|
||
|
|
};
|
||
|
|
return (
|
||
|
|
<select className="m-owner" value={actual || ""} onChange={(e) => cambiar(e.target.value)} aria-label="Propietario">
|
||
|
|
<option value="">Todos (equipo)</option>
|
||
|
|
{owners.map((o) => <option key={o.email} value={o.email}>{o.name}</option>)}
|
||
|
|
</select>
|
||
|
|
);
|
||
|
|
}
|