Fix: CheckBox y Estados

This commit is contained in:
2026-02-19 21:10:48 -03:00
parent 8569f57a62
commit 9e57eb7f54

View File

@@ -110,27 +110,18 @@ export default function FormularioAviso({
// Contacto // Contacto
const [contactData, setContactData] = useState({ const [contactData, setContactData] = useState({
phone: (user as any)?.phoneNumber || (user as any)?.phone || "", // Si es admin Y no está editando, los campos de contacto empiezan vacíos.
email: user?.email || "", // 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, showPhone: true,
allowWhatsApp: true, allowWhatsApp: true,
showEmail: 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 --- // --- EFECTOS DE CARGA ---
// 1. Cargar Marcas // 1. Cargar Marcas
@@ -312,25 +303,38 @@ export default function FormularioAviso({
setUserSearch(u.name); setUserSearch(u.name);
setShowUserSuggestions(false); 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) => ({ setContactData((prev) => ({
...prev, ...prev,
email: u.email, email: u.email,
// Si u.phone viene undefined (porque el backend no lo manda o es null), usamos string vacío
phone: u.phone || "", 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 // Sincronizar datos de usuario fantasma con datos de contacto
useEffect(() => { 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) => ({ setContactData((prev) => ({
...prev, ...prev,
email: ghostUser.email,
phone: ghostUser.phone, 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" // Detección automática de usuario existente al escribir en "Crear Nuevo"
useEffect(() => { useEffect(() => {
@@ -745,6 +749,14 @@ export default function FormularioAviso({
onClick={() => { onClick={() => {
setTargetUser(null); setTargetUser(null);
setUserSearch(""); setUserSearch("");
setContactData((prev) => ({
...prev,
email: "",
phone: "",
showPhone: false,
allowWhatsApp: false,
showEmail: false,
}));
}} }}
className="text-amber-400 hover:text-white text-xs font-bold" className="text-amber-400 hover:text-white text-xs font-bold"
> >
@@ -1345,9 +1357,9 @@ export default function FormularioAviso({
<input <input
type="text" type="text"
value={contactData.phone} value={contactData.phone}
disabled={isAdmin}
onChange={(e) => { onChange={(e) => {
const val = e.target.value; const val = e.target.value;
// Si borra el teléfono, desactivamos los checks asociados
setContactData({ setContactData({
...contactData, ...contactData,
phone: val, phone: val,
@@ -1355,8 +1367,12 @@ export default function FormularioAviso({
allowWhatsApp: val ? contactData.allowWhatsApp : false, 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" 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="+54 9 221 ..." placeholder={
isAdmin
? "Selecciona o crea un vendedor arriba"
: "+54 9 221 ..."
}
/> />
<div className="flex flex-col sm:flex-row gap-4"> <div className="flex flex-col sm:flex-row gap-4">
@@ -1424,7 +1440,8 @@ export default function FormularioAviso({
Email Email
</label> </label>
<div className="w-full bg-black/40 border border-white/10 rounded-lg p-3 text-xs text-gray-400 font-medium break-all mb-3 cursor-not-allowed"> <div className="w-full bg-black/40 border border-white/10 rounded-lg p-3 text-xs text-gray-400 font-medium break-all mb-3 cursor-not-allowed">
{contactData.email} {contactData.email ||
(isAdmin && "Selecciona o crea un vendedor")}
</div> </div>
{/* Check Mostrar Email */} {/* Check Mostrar Email */}
@@ -1436,6 +1453,7 @@ export default function FormularioAviso({
type="checkbox" type="checkbox"
className="hidden" className="hidden"
checked={contactData.showEmail} checked={contactData.showEmail}
disabled={!contactData.email}
onChange={(e) => onChange={(e) =>
setContactData({ setContactData({
...contactData, ...contactData,