create and update machine
This commit is contained in:
@@ -10,7 +10,7 @@ interface Data {
|
|||||||
id_plataforma: number;
|
id_plataforma: number;
|
||||||
id_area_ubicacion: number;
|
id_area_ubicacion: number;
|
||||||
ubicacion: string;
|
ubicacion: string;
|
||||||
ip:string
|
ip: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Plataforma {
|
interface Plataforma {
|
||||||
@@ -38,6 +38,7 @@ export default function Equipos() {
|
|||||||
const [idArea, setIdArea] = useState<number | "">("");
|
const [idArea, setIdArea] = useState<number | "">("");
|
||||||
|
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
|
const [isCreating, setIsCreating] = useState(false);
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -70,7 +71,7 @@ export default function Equipos() {
|
|||||||
ubicacion: ubicacion,
|
ubicacion: ubicacion,
|
||||||
id_plataforma: idPlataforma,
|
id_plataforma: idPlataforma,
|
||||||
id_area_ubicacion: idArea,
|
id_area_ubicacion: idArea,
|
||||||
ip:data.ip
|
ip: data.ip,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Actualiza el estado local
|
// 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 (
|
return (
|
||||||
<form className="containerForm" onSubmit={handleSubmit}>
|
<form className="containerForm" onSubmit={handleSubmit}>
|
||||||
<div className="groupInput">
|
{!isCreating && (
|
||||||
<input
|
<div className="groupInput">
|
||||||
placeholder="Número de Equipo a buscar..."
|
<input
|
||||||
value={id}
|
placeholder="Número de Equipo a buscar..."
|
||||||
onChange={(e) => setId(e.target.value)}
|
value={id}
|
||||||
/>
|
onChange={(e) => setId(e.target.value)}
|
||||||
|
/>
|
||||||
<button type="submit" className="button buttonSearch">
|
<button type="submit" className="button buttonSearch">
|
||||||
Buscar
|
Buscar
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{data && (
|
{data && (
|
||||||
<div>
|
<div>
|
||||||
@@ -156,7 +211,7 @@ export default function Equipos() {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="button buttonSearch"
|
className="button buttonSearch"
|
||||||
onClick={handleSave}
|
onClick={isCreating ? handleCreate : handleSave}
|
||||||
>
|
>
|
||||||
Guardar
|
Guardar
|
||||||
</button>
|
</button>
|
||||||
@@ -164,14 +219,22 @@ export default function Equipos() {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="button buttonCancel"
|
className="button buttonCancel"
|
||||||
onClick={() => setIsEditing(false)}
|
onClick={() => {
|
||||||
|
setIsEditing(false);
|
||||||
|
setIsCreating(false);
|
||||||
|
setData(null);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
Cancelar
|
Cancelar
|
||||||
</button>
|
</button>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<button type="button" className="button buttonCharge">
|
<button
|
||||||
|
type="button"
|
||||||
|
className="button buttonCharge"
|
||||||
|
onClick={handleNuevo}
|
||||||
|
>
|
||||||
Nuevo
|
Nuevo
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
|||||||
Reference in New Issue
Block a user