create and update machine
This commit is contained in:
@@ -10,7 +10,7 @@ interface Data {
|
||||
id_plataforma: number;
|
||||
id_area_ubicacion: number;
|
||||
ubicacion: string;
|
||||
ip:string
|
||||
ip: string;
|
||||
}
|
||||
|
||||
interface Plataforma {
|
||||
@@ -38,6 +38,7 @@ export default function Equipos() {
|
||||
const [idArea, setIdArea] = useState<number | "">("");
|
||||
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [isCreating, setIsCreating] = useState(false);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
@@ -70,7 +71,7 @@ export default function Equipos() {
|
||||
ubicacion: ubicacion,
|
||||
id_plataforma: idPlataforma,
|
||||
id_area_ubicacion: idArea,
|
||||
ip:data.ip
|
||||
ip: data.ip,
|
||||
});
|
||||
|
||||
// Actualiza el estado local
|
||||
@@ -90,19 +91,73 @@ export default function Equipos() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleNuevo = async () => {
|
||||
try {
|
||||
const [plataformaRes, areaRes] = await Promise.all([
|
||||
axios.get(`${envConfig.apiUrl}/alumno-inscrito/plataforma`),
|
||||
axios.get(`${envConfig.apiUrl}/area-ubicacion`),
|
||||
]);
|
||||
|
||||
setPlataforma(plataformaRes.data);
|
||||
setArea(areaRes.data);
|
||||
|
||||
setData({
|
||||
id_equipo: 0,
|
||||
nombre_equipo: "",
|
||||
ubicacion: "",
|
||||
id_plataforma: 0,
|
||||
id_area_ubicacion: 0,
|
||||
ip: "",
|
||||
});
|
||||
|
||||
setNombre("");
|
||||
setUbicacion("");
|
||||
setIdPlataforma("");
|
||||
setIdArea("");
|
||||
|
||||
setIsCreating(true);
|
||||
setIsEditing(true);
|
||||
} catch (error) {
|
||||
toast.error("Error al preparar formulario");
|
||||
}
|
||||
};
|
||||
|
||||
const handleCreate = async () => {
|
||||
try {
|
||||
await axios.post(`${envConfig.apiUrl}/equipo`, {
|
||||
nombre_equipo: nombre,
|
||||
ubicacion,
|
||||
id_plataforma: idPlataforma,
|
||||
id_area_ubicacion: idArea,
|
||||
ip: "0.0.0.0",
|
||||
});
|
||||
|
||||
toast.success("Equipo creado correctamente");
|
||||
|
||||
setIsCreating(false);
|
||||
setIsEditing(false);
|
||||
setData(null);
|
||||
} catch (error: any) {
|
||||
const message =
|
||||
error?.response?.data?.message || "Error al crear el equipo";
|
||||
toast.error(message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form className="containerForm" onSubmit={handleSubmit}>
|
||||
<div className="groupInput">
|
||||
<input
|
||||
placeholder="Número de Equipo a buscar..."
|
||||
value={id}
|
||||
onChange={(e) => setId(e.target.value)}
|
||||
/>
|
||||
|
||||
<button type="submit" className="button buttonSearch">
|
||||
Buscar
|
||||
</button>
|
||||
</div>
|
||||
{!isCreating && (
|
||||
<div className="groupInput">
|
||||
<input
|
||||
placeholder="Número de Equipo a buscar..."
|
||||
value={id}
|
||||
onChange={(e) => setId(e.target.value)}
|
||||
/>
|
||||
<button type="submit" className="button buttonSearch">
|
||||
Buscar
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{data && (
|
||||
<div>
|
||||
@@ -156,7 +211,7 @@ export default function Equipos() {
|
||||
<button
|
||||
type="button"
|
||||
className="button buttonSearch"
|
||||
onClick={handleSave}
|
||||
onClick={isCreating ? handleCreate : handleSave}
|
||||
>
|
||||
Guardar
|
||||
</button>
|
||||
@@ -164,14 +219,22 @@ export default function Equipos() {
|
||||
<button
|
||||
type="button"
|
||||
className="button buttonCancel"
|
||||
onClick={() => setIsEditing(false)}
|
||||
onClick={() => {
|
||||
setIsEditing(false);
|
||||
setIsCreating(false);
|
||||
setData(null);
|
||||
}}
|
||||
>
|
||||
Cancelar
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button type="button" className="button buttonCharge">
|
||||
<button
|
||||
type="button"
|
||||
className="button buttonCharge"
|
||||
onClick={handleNuevo}
|
||||
>
|
||||
Nuevo
|
||||
</button>
|
||||
<button
|
||||
|
||||
Reference in New Issue
Block a user