"use client"; import { useEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { useRouter } from "next/navigation"; import { abrirEdicionOppAction, guardarOportunidadAction, deshacerOportunidadAction, buscarCompetidoresAction, crearNotaAction, deshacerNotaAction, } from "../campoActions"; import { FORECAST_OPC, PROB_OPC, LUZ_OPC, TIPO_OPC, MAX_ESTADO_NEGOCIACION } from "../../lib/campo"; import { ROLES } from "../../lib/participantes"; import { GC } from "../../lib/participantes"; // Botones 📝 Nota y ✎ Editar de la ficha de oportunidad, con sus hojas. Nada se gana ni se pierde desde el teléfono. // Guardar = PATCH con If-Match (etag de cuando se abrió) y «Deshacer» durante 10 minutos; el estado de la negociación // puede quedar además como nota nativa en la línea de tiempo. function Hoja({ titulo, onCerrar, children }) { return createPortal(
e.stopPropagation()}>
, document.body ); } export function BotonNota({ oppId, apagado }) { const router = useRouter(); const [abierto, setAbierto] = useState(false); const [texto, setTexto] = useState(""); const [trab, setTrab] = useState(false); const [error, setError] = useState(""); const [hecho, setHecho] = useState(null); const [deshecho, setDeshecho] = useState(false); const cerrar = () => { setAbierto(false); if (hecho) router.refresh(); setHecho(null); setDeshecho(false); setTexto(""); setError(""); }; async function guardar() { setTrab(true); setError(""); const r = await crearNotaAction({ id: oppId, texto }); setTrab(false); if (!r.ok) return setError(r.error); setHecho(r); } async function deshacer() { setTrab(true); const r = await deshacerNotaAction(hecho.accionId); setTrab(false); if (!r.ok) return setError(r.error); setDeshecho(true); } return ( <> {abierto && ( {hecho ? (

{deshecho ? "↩ Se eliminó la nota." : "✓ Nota guardada en Dynamics"}

{error &&

{error}

}
{!deshecho && }
) : ( <>