diff --git a/frontend/src/features/createUser/SeccionRol.tsx b/frontend/src/features/createUser/SeccionRol.tsx index 492d54b..84e6350 100644 --- a/frontend/src/features/createUser/SeccionRol.tsx +++ b/frontend/src/features/createUser/SeccionRol.tsx @@ -22,40 +22,43 @@ interface Props { users: User[]; } - export function SeccionRol({ formData, onChange, disable, users }: Props) { const ROLE_LIMITS: Record = { - Presidente: 1, - Tesorero: 1, - Secretario: 1, - Vocal: 3, - Vicepresidente: 1, - Fiscal: 1, + Presidente: 1, + Tesorero: 1, + Secretario: 1, + Vocal: 3, + Vicepresidente: 1, + Fiscal: 1, - - DuenoDeCasa: Infinity, -}; + DuenoDeCasa: Infinity, + }; - const getRoleCounts = (users: User[]) =>{ - return users.reduce((acc,user)=>{ - acc[user.rol] = (acc[user.rol] || 0)+1; - return acc; - },{}as Record); + const getRoleCounts = (users: User[]) => { + return users + .filter((user) => user.estado) + .reduce( + (acc, user) => { + acc[user.rol] = (acc[user.rol] || 0) + 1; + return acc; + }, + {} as Record, + ); }; const getRoleOptions = (users: User[]) => { - const counts = getRoleCounts(users); + const counts = getRoleCounts(users); - return ROLES.map((role) => { - const limit = ROLE_LIMITS[role.rol] ?? 1; - const current = counts[role.rol] ?? 0; + return ROLES.map((role) => { + const limit = ROLE_LIMITS[role.rol] ?? 1; + const current = counts[role.rol] ?? 0; - return { - ...role, - disabled: current >= limit, - }; - }); -}; -const availableRoles = getRoleOptions(users); + return { + ...role, + disabled: current >= limit, + }; + }); + }; + const availableRoles = getRoleOptions(users); return (
@@ -76,6 +79,7 @@ const availableRoles = getRoleOptions(users); {availableRoles.map((r) => ( ))} diff --git a/frontend/src/features/createUser/useCreateUserForm.tsx b/frontend/src/features/createUser/useCreateUserForm.tsx index a2447f4..1f8a4b9 100644 --- a/frontend/src/features/createUser/useCreateUserForm.tsx +++ b/frontend/src/features/createUser/useCreateUserForm.tsx @@ -12,6 +12,7 @@ export function useCreateUserForm( initialData?: CreateUserFormData, mode?: string, ) { + console.log(isSuperAdmin); const [formData, setFormData] = useState( initialData ?? INITIAL_FORM_DATA, ); @@ -26,26 +27,23 @@ export function useCreateUserForm( }; const handleCelularChange = (e: React.ChangeEvent) => { - let value = e.target.value; + let value = e.target.value; - value = value.replace(/\D/g, ""); + value = value.replace(/\D/g, ""); - if (value.startsWith("504")) { - value = value.slice(3); - } + if (value.startsWith("504")) { + value = value.slice(3); + } - - value = value.substring(0, 8); + value = value.substring(0, 8); - const formatted = value.length - ? `+504 ${value}` - : "+504 "; + const formatted = value.length ? `+504 ${value}` : "+504 "; - setFormData((prev) => ({ - ...prev, - telefono: formatted, - })); -}; + setFormData((prev) => ({ + ...prev, + telefono: formatted, + })); + }; const handleDniChange = (e: React.ChangeEvent) => { let value = e.target.value.replace(/\D/g, ""); @@ -108,7 +106,7 @@ export function useCreateUserForm( if (!formData.dni.trim()) return "El numero de identificacion es obligatorio"; - const needsAddress = !isSuperAdmin || formData.rol === "DuenoDeCasa"; + const needsAddress = formData.rol === "DuenoDeCasa"; if (needsAddress) { if (!formData.domicilios.calle) return "La calle es obligatoria"; if (!formData.domicilios.codigoBloque) return "El bloque es obligatorio"; @@ -202,7 +200,7 @@ export function useCreateUserForm( } const { data } = response; - + onUserCreated(data); if (onClose) onClose(); } catch (err: any) { diff --git a/frontend/src/features/create_user_form.tsx b/frontend/src/features/create_user_form.tsx index b5eed92..6bf8e35 100644 --- a/frontend/src/features/create_user_form.tsx +++ b/frontend/src/features/create_user_form.tsx @@ -50,6 +50,9 @@ export default function CreateUserForm({ : "Detalles de Usuario"; const viewOnly = mode === "view"; + const hasPresidente = users.some( + (user) => user.rol === "Presidente" && user.estado, + ); return ( <> @@ -88,15 +91,16 @@ export default function CreateUserForm({ onSubmit={handleSubmit} className="flex flex-col gap-6 px-6 py-6 w-full max-w-125" > - {isSuperAdmin && ( - - )} - + {isSuperAdmin || + (!hasPresidente && ( + + ))} + {mode !== "create" && ( { setUsers((prev) => - prev.map((user) => - user.idUsuario === updatedUser.idUsuario - ? updatedUser - : user - ) - ); + prev.map((user) => + user.idUsuario === updatedUser.idUsuario ? updatedUser : user, + ), + ); }; useEffect(() => {