informe-comercial-alfa/app/m/components/WhatsAppRegistro.js

68 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useEffect, useState } from "react";
import { registrarWhatsAppAction } from "../campoActions";
import { FRASES } from "../../components/actividades/GestionarActividad";
import { useRouter } from "next/navigation";
// Al volver de WhatsApp (la app vuelve a estar visible) ofrece registrar el contacto como una Llamada ya realizada,
// ligada a la oportunidad (o a la cuenta), con asunto «WhatsApp – …» y el resultado. «Ahora no» la descarta.
const CLAVE = "campo.wa";
export default function WhatsAppRegistro() {
const router = useRouter();
const [p, setP] = useState(null);
const [nota, setNota] = useState("");
const [trab, setTrab] = useState(false);
const [error, setError] = useState("");
const [ok, setOk] = useState(false);
useEffect(() => {
const revisar = () => {
if (document.visibilityState !== "visible") return;
try {
const raw = sessionStorage.getItem(CLAVE);
if (!raw) return;
const d = JSON.parse(raw);
const edad = Date.now() - d.ts;
if (edad > 6 * 3600 * 1000) return sessionStorage.removeItem(CLAVE);
if (edad < 3000) return; // todavía no salió a WhatsApp
setP(d);
} catch { /* ignorar */ }
};
document.addEventListener("visibilitychange", revisar);
window.addEventListener("campo:wa-regreso", revisar); // simulación en pruebas
return () => { document.removeEventListener("visibilitychange", revisar); window.removeEventListener("campo:wa-regreso", revisar); };
}, []);
if (!p && !ok) return null;
const cerrar = () => { try { sessionStorage.removeItem(CLAVE); } catch { /* ignorar */ } setP(null); setOk(false); setNota(""); setError(""); };
async function guardar() {
setTrab(true); setError("");
const r = await registrarWhatsAppAction({
referenteTipo: p.referenteTipo, referenteId: p.referenteId, resultado: nota,
objetivo: p.referenteTipo === "account" ? "Prospección" : "Negociación",
});
setTrab(false);
if (!r.ok) return setError(r.error);
setOk(true); setP(null);
try { sessionStorage.removeItem(CLAVE); } catch { /* ignorar */ }
router.refresh();
setTimeout(() => setOk(false), 3500);
}
if (ok) return <div className="m-toast" role="status">✓ Registrado como llamada por WhatsApp</div>;
return (
<div className="m-bd" style={{ zIndex: 80 }}>
<div className="m-sheet" role="dialog" aria-modal="true" aria-label="Registrar contacto por WhatsApp">
<div className="m-grab" aria-hidden="true" />
<div className="m-sh-h"><div><div className="m-sh-t">¿Registrar este contacto por WhatsApp?</div>
<div className="m-sh-s">{[p.contacto, p.cliente, p.oppNombre].filter(Boolean).join(" · ")}</div></div></div>
<textarea className="m-ta" rows={3} maxLength={1500} value={nota} placeholder="Resultado: ¿qué pasó y qué sigue?" onChange={(e) => setNota(e.target.value)} autoFocus />
<div className="m-chips-w">{FRASES.Llamada.map((f) => <button key={f} type="button" className="m-chip" onClick={() => setNota((n) => (n.trim() ? `${n.trim()}. ${f}` : f))}>{f}</button>)}</div>
{error && <p className="m-err">{error}</p>}
<div className="m-fila-btn fin">
<button type="button" className="m-btn" onClick={cerrar}>Ahora no</button>
<button type="button" className="m-btn pri" disabled={trab} onClick={guardar}>{trab ? "Guardando…" : "Registrar"}</button>
</div>
</div>
</div>
);
}