import { useEffect, useMemo, useState } from 'react'; import type { FormEvent } from 'react'; import { Link, useNavigate } from 'react-router'; import { Alert, LoadingBlock, errorMessage } from '../components/Feedback'; import { Icon } from '../components/Icon'; import { listRoles } from '../lib/api'; import type { AdministrativeRole } from '../lib/api'; import { createUserProfile } from '../lib/userProfileApi'; export function NewUserPage() { const navigate = useNavigate(); const [roles, setRoles] = useState([]); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(''); const [roleIds, setRoleIds] = useState([]); useEffect(() => { listRoles().then(setRoles).catch((requestError) => setError(errorMessage(requestError))).finally(() => setLoading(false)); }, []); const inspectorSelected = useMemo( () => roles.some((role) => role.code === 'inspector' && roleIds.includes(role.id)), [roles, roleIds], ); const toggleRole = (id: string) => setRoleIds((current) => current.includes(id) ? current.filter((value) => value !== id) : [...current, id]); const submit = async (event: FormEvent) => { event.preventDefault(); setError(''); const data = new FormData(event.currentTarget); const email = String(data.get('email') ?? '').trim(); if (inspectorSelected && !email) { setError('El email es obligatorio para usuarios con rol Inspector porque allí recibirán la documentación de sus inspecciones.'); return; } setSubmitting(true); try { const created = await createUserProfile({ username: String(data.get('username')), email: email || null, dni: String(data.get('dni') ?? '').trim() || null, phone: String(data.get('phone') ?? '').trim() || null, jobTitle: String(data.get('jobTitle') ?? '').trim() || null, employeeNumber: String(data.get('employeeNumber') ?? '').trim() || null, firstName: String(data.get('firstName')), lastName: String(data.get('lastName')), password: String(data.get('password')), mustChangePassword: data.get('mustChangePassword') === 'on', roleIds, }); navigate(`/admin/users/${created.id}`, { replace: true, state: { created: true } }); } catch (requestError) { setError(errorMessage(requestError)); } finally { setSubmitting(false); } }; return
Usuarios/Nuevo usuario
NUEVO ACCESO

Crear usuario

Además del acceso, registrá los datos personales que identifican al agente y permiten las comunicaciones oficiales.

{error && {error}} {loading ? :

Datos personales

Nombre y apellido identifican al agente. DNI, contacto y función quedan asociados a su historial operativo.

Acceso y contacto

El email del Inspector se utiliza también como destinatario de la documentación al cerrar la inspección.

{inspectorSelected &&

Inspector: este email recibirá copia de las Actas/Informe correspondientes al cierre de la inspección.

}

Seguridad

Roles

{roles.map((role) => )}
Cancelar
}
; }