diff --git a/Frontend/src/components/FormularioAviso.tsx b/Frontend/src/components/FormularioAviso.tsx index eb89d7e..b057a9b 100644 --- a/Frontend/src/components/FormularioAviso.tsx +++ b/Frontend/src/components/FormularioAviso.tsx @@ -110,27 +110,18 @@ export default function FormularioAviso({ // Contacto const [contactData, setContactData] = useState({ - phone: (user as any)?.phoneNumber || (user as any)?.phone || "", - email: user?.email || "", + // Si es admin Y no está editando, los campos de contacto empiezan vacíos. + // Si no es admin, usa los datos del usuario logueado. + phone: + isAdmin && !editId + ? "" + : (user as any)?.phoneNumber || (user as any)?.phone || "", + email: isAdmin && !editId ? "" : user?.email || "", showPhone: true, allowWhatsApp: true, showEmail: true, }); - // Sincronizar contacto cuando el usuario cargue - useEffect(() => { - if (user && !editId && !targetUser && !ghostUser.email) { - setContactData((prev) => ({ - ...prev, - email: user.email || prev.email, - phone: (user as any).phone || (user as any).phoneNumber || prev.phone, - showPhone: true, - allowWhatsApp: true, - showEmail: true, - })); - } - }, [user, editId]); - // --- EFECTOS DE CARGA --- // 1. Cargar Marcas @@ -312,25 +303,38 @@ export default function FormularioAviso({ setUserSearch(u.name); setShowUserSuggestions(false); - // Actualizamos los datos de contacto con los del usuario seleccionado + const hasPhone = u.phone && u.phone.trim() !== "" && u.phone.trim() !== "0"; + setContactData((prev) => ({ ...prev, email: u.email, - // Si u.phone viene undefined (porque el backend no lo manda o es null), usamos string vacío phone: u.phone || "", + // Si no tiene teléfono, los checks empiezan desactivados. + // Si tiene, respetamos la preferencia anterior o por defecto activamos. + showPhone: hasPhone ? prev.showPhone : false, + allowWhatsApp: hasPhone ? prev.allowWhatsApp : false, })); }; // Sincronizar datos de usuario fantasma con datos de contacto useEffect(() => { - if (!targetUser && (ghostUser.email || ghostUser.phone)) { + // Este efecto solo se aplica si el admin está usando el panel de "Crear Nuevo" + if (isAdmin && !targetUser && !editId) { + const phoneExists = ghostUser.phone && ghostUser.phone.trim() !== ""; + const emailExists = ghostUser.email && ghostUser.email.trim() !== ""; + setContactData((prev) => ({ ...prev, - email: ghostUser.email, phone: ghostUser.phone, + email: ghostUser.email, + // Si hay teléfono, respetamos la preferencia anterior. Si no hay, forzamos a 'false'. + showPhone: phoneExists ? prev.showPhone : false, + allowWhatsApp: phoneExists ? prev.allowWhatsApp : false, + // Si hay email, respetamos la preferencia. Si no, forzamos a 'false'. + showEmail: emailExists ? prev.showEmail : false, })); } - }, [ghostUser, targetUser]); + }, [ghostUser, targetUser, isAdmin, editId]); // Detección automática de usuario existente al escribir en "Crear Nuevo" useEffect(() => { @@ -745,6 +749,14 @@ export default function FormularioAviso({ onClick={() => { setTargetUser(null); setUserSearch(""); + setContactData((prev) => ({ + ...prev, + email: "", + phone: "", + showPhone: false, + allowWhatsApp: false, + showEmail: false, + })); }} className="text-amber-400 hover:text-white text-xs font-bold" > @@ -1345,9 +1357,9 @@ export default function FormularioAviso({ { const val = e.target.value; - // Si borra el teléfono, desactivamos los checks asociados setContactData({ ...contactData, phone: val, @@ -1355,8 +1367,12 @@ export default function FormularioAviso({ allowWhatsApp: val ? contactData.allowWhatsApp : false, }); }} - className="w-full bg-black/40 border border-white/10 rounded-lg p-3 text-xs text-white outline-none focus:border-blue-500 mb-3" - placeholder="+54 9 221 ..." + className="w-full bg-black/40 border border-white/10 rounded-lg p-3 text-xs text-white outline-none focus:border-blue-500 mb-3 disabled:cursor-not-allowed disabled:opacity-50" + placeholder={ + isAdmin + ? "Selecciona o crea un vendedor arriba" + : "+54 9 221 ..." + } />
@@ -1424,7 +1440,8 @@ export default function FormularioAviso({ Email
- {contactData.email} + {contactData.email || + (isAdmin && "Selecciona o crea un vendedor")}
{/* Check Mostrar Email */} @@ -1436,6 +1453,7 @@ export default function FormularioAviso({ type="checkbox" className="hidden" checked={contactData.showEmail} + disabled={!contactData.email} onChange={(e) => setContactData({ ...contactData,