Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions frontend/src/features/createUser/SeccionRol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,43 @@ interface Props {
users: User[];
}


export function SeccionRol({ formData, onChange, disable, users }: Props) {
const ROLE_LIMITS: Record<string, number> = {
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<string,number>);
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<string, number>,
);
};
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 (
<div>
Expand All @@ -76,6 +79,7 @@ const availableRoles = getRoleOptions(users);
{availableRoles.map((r) => (
<option key={r.value} value={r.rol} disabled={r.disabled}>
{r.label}
{r.disabled ? " (No disponible)" : ""}
</option>
))}
</select>
Expand Down
32 changes: 15 additions & 17 deletions frontend/src/features/createUser/useCreateUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function useCreateUserForm(
initialData?: CreateUserFormData,
mode?: string,
) {
console.log(isSuperAdmin);
const [formData, setFormData] = useState<CreateUserFormData>(
initialData ?? INITIAL_FORM_DATA,
);
Expand All @@ -26,26 +27,23 @@ export function useCreateUserForm(
};

const handleCelularChange = (e: React.ChangeEvent<HTMLInputElement>) => {
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<HTMLInputElement>) => {
let value = e.target.value.replace(/\D/g, "");
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -202,7 +200,7 @@ export function useCreateUserForm(
}

const { data } = response;

onUserCreated(data);
if (onClose) onClose();
} catch (err: any) {
Expand Down
22 changes: 13 additions & 9 deletions frontend/src/features/create_user_form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -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 && (
<SeccionRol
formData={formData}
onChange={handleInputChange}
disable={viewOnly}
users={users}
/>
)}

{isSuperAdmin ||
(!hasPresidente && (
<SeccionRol
formData={formData}
onChange={handleInputChange}
disable={viewOnly}
users={users}
/>
))}

{mode !== "create" && (
<SeccionEstado
formData={formData}
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/features/gestionUsuarios/GestionarUsuarios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ export default function GestionarUsuarios() {
};
const handleEditarUsuario = (updatedUser: User) => {
setUsers((prev) =>
prev.map((user) =>
user.idUsuario === updatedUser.idUsuario
? updatedUser
: user
)
);
prev.map((user) =>
user.idUsuario === updatedUser.idUsuario ? updatedUser : user,
),
);
};

useEffect(() => {
Expand Down
Loading