76 lines
3.8 KiB
JavaScript
76 lines
3.8 KiB
JavaScript
|
|
import Link from "next/link";
|
||
|
|
import { Suspense } from "react";
|
||
|
|
import { contextoCampo } from "../../lib/campoPagina";
|
||
|
|
import { listarCuentas, oportunidadesPorCuenta } from "../../lib/campoServer";
|
||
|
|
import { resolveOwnerId } from "../../lib/queries";
|
||
|
|
import { CATEGORIA_COLOR } from "../../lib/campo";
|
||
|
|
import { money } from "../../lib/constants";
|
||
|
|
import LoginPrompt from "../../components/LoginPrompt";
|
||
|
|
import ErrorNotice from "../../components/ErrorNotice";
|
||
|
|
import OwnerM from "../components/OwnerM";
|
||
|
|
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
const FILTROS = [["todas", "Todas"], ["clientes", "Clientes"], ["opp", "Con oportunidad"], ["alta", "💎 Diamante · Platinum"]];
|
||
|
|
|
||
|
|
// Mismo alcance que /cuentas: el ejecutivo ve sus cuentas; gerente y asistente eligen propietario o buscan por nombre.
|
||
|
|
export default async function Cuentas({ searchParams }) {
|
||
|
|
const sp = await searchParams;
|
||
|
|
const c = await contextoCampo(sp);
|
||
|
|
if (!c) return <LoginPrompt />;
|
||
|
|
const q = String(sp.q || "").trim().slice(0, 60);
|
||
|
|
const filtro = FILTROS.some(([k]) => k === sp.f) ? sp.f : "todas";
|
||
|
|
const top = Math.min(600, Math.max(120, Number(sp.n) || 120));
|
||
|
|
let res = null;
|
||
|
|
let opps = { m: new Map(), nombres: new Map() };
|
||
|
|
let pideBuscar = false;
|
||
|
|
try {
|
||
|
|
const token = c.session.accessToken;
|
||
|
|
const ownerId = c.unico ? await resolveOwnerId(c.unico, token) : null;
|
||
|
|
if (!c.unico && q.length < 2) pideBuscar = true;
|
||
|
|
else {
|
||
|
|
opps = await oportunidadesPorCuenta(token, ownerId);
|
||
|
|
res = await listarCuentas(token, { ownerId, q, filtro, top, opps });
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
return <ErrorNotice error={e} />;
|
||
|
|
}
|
||
|
|
const href = (extra) => {
|
||
|
|
const p = new URLSearchParams();
|
||
|
|
if (typeof sp.owner === "string") p.set("owner", sp.owner);
|
||
|
|
if (q) p.set("q", q);
|
||
|
|
if (filtro !== "todas") p.set("f", filtro);
|
||
|
|
for (const [k, v] of Object.entries(extra)) { if (v == null) p.delete(k); else p.set(k, v); }
|
||
|
|
return `/m/cuentas${p.toString() ? `?${p.toString()}` : ""}`;
|
||
|
|
};
|
||
|
|
return (
|
||
|
|
<>
|
||
|
|
{c.selector && <Suspense fallback={null}><OwnerM owners={c.owners} actual={c.ownerActual} /></Suspense>}
|
||
|
|
<form method="get" className="m-busca" action="/m/cuentas">
|
||
|
|
{typeof sp.owner === "string" && <input type="hidden" name="owner" value={sp.owner} />}
|
||
|
|
{filtro !== "todas" && <input type="hidden" name="f" value={filtro} />}
|
||
|
|
<input className="m-in" type="search" name="q" defaultValue={q} placeholder="🔍 Buscar cuenta o contacto…" aria-label="Buscar cuenta o contacto" />
|
||
|
|
<button type="submit" className="m-btn">Buscar</button>
|
||
|
|
</form>
|
||
|
|
<div className="m-chips" role="group" aria-label="Filtrar cuentas">
|
||
|
|
{FILTROS.map(([k, l]) => <Link key={k} href={href({ f: k === "todas" ? null : k, n: null })} className={`m-chip${filtro === k ? " on" : ""}`}>{l}</Link>)}
|
||
|
|
</div>
|
||
|
|
{pideBuscar && <p className="m-vacio">Elige un ejecutivo arriba o busca una cuenta por nombre (mínimo 2 letras).</p>}
|
||
|
|
{res && !res.cuentas.length && <p className="m-vacio">{q ? `Sin cuentas para «${q}».` : "No hay cuentas con este filtro."}</p>}
|
||
|
|
{res?.cuentas.map((a) => {
|
||
|
|
const o = opps.m.get(a.id);
|
||
|
|
return (
|
||
|
|
<Link key={a.id} href={`/m/cuentas/${a.id}`} className="m-cta">
|
||
|
|
<span className="c">
|
||
|
|
<b>{a.nombre}</b>
|
||
|
|
<small>{[a.esCliente ? "Cliente" : "Prospecto", o ? `${o.n} oportunidad${o.n === 1 ? "" : "es"} · ${money(o.mrr)}` : null, !c.unico ? a.owner : null].filter(Boolean).join(" · ")}</small>
|
||
|
|
</span>
|
||
|
|
{a.categoria && <span className="m-cat" style={{ background: CATEGORIA_COLOR[a.categoria] }}>{a.categoria}</span>}
|
||
|
|
</Link>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
{res?.hayMas && <Link className="m-mas" href={href({ n: String(top + 120) })}>Mostrar más cuentas</Link>}
|
||
|
|
</>
|
||
|
|
);
|
||
|
|
}
|