Files

142 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2025-09-22 16:53:34 -06:00
"use client";
2026-01-29 12:35:31 -06:00
import { useEffect, useState } from "react";
2026-03-02 10:34:42 -06:00
import { envConfig } from "@/app/lib/config";
2025-09-22 16:53:34 -06:00
2026-01-29 12:35:31 -06:00
import axios from "axios";
2026-02-19 18:56:56 -06:00
import Swal from "sweetalert2";
2026-03-02 10:34:42 -06:00
import Cookies from "js-cookie";
import "@/app/globals.css";
2025-10-01 10:12:41 -06:00
2025-09-23 18:36:21 -06:00
export default function Areas() {
2026-01-29 12:35:31 -06:00
const [areas, setAreas] = useState([]);
const [selectedArea, setSelectedArea] = useState("");
const [modo, setModo] = useState<"Activo" | "Mantenimiento" | null>(
"Mantenimiento",
);
2026-03-02 10:34:42 -06:00
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-01-29 12:35:31 -06:00
useEffect(() => {
axios
2026-03-02 10:34:42 -06:00
.get(`${envConfig.apiUrl}/area-ubicacion`,
{ headers }
)
2026-01-29 12:35:31 -06:00
.then((data) => {
setAreas(data.data);
})
.catch((error) => {
console.error("Error al traer las áreas:", error);
});
}, []);
2025-09-22 16:53:34 -06:00
2026-02-11 16:42:41 -06:00
const handleActualizar = async (e: React.FormEvent<HTMLFormElement>) => {
2025-09-22 16:53:34 -06:00
e.preventDefault();
2026-02-11 16:42:41 -06:00
if (!selectedArea) {
2026-02-20 14:33:08 -05:00
2026-02-19 18:56:56 -06:00
Swal.fire({
2026-02-20 14:33:08 -05:00
title: "Selecciona un área antes de actualizar!",
icon: "error",
draggable: true,
});
return;
}
2026-02-11 16:42:41 -06:00
if (!modo) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Selecciona un modo!",
icon: "error",
draggable: true,
});
2026-02-11 16:42:41 -06:00
return;
}
try {
await axios.patch(
`${envConfig.apiUrl}/equipo/actualizar-area`,
{
id_area_ubicacion: Number(selectedArea),
activo: modo === "Activo",
2026-03-02 10:34:42 -06:00
}, { headers }
2026-02-11 16:42:41 -06:00
);
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Equipos actualizados correctamente!",
icon: "success",
draggable: true,
});
2026-02-11 16:42:41 -06:00
} catch (error) {
console.error(error);
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Error al actualizar los equipos!",
icon: "error",
draggable: true,
});
2026-02-11 16:42:41 -06:00
}
2025-09-22 16:53:34 -06:00
};
return (
<form className="containerForm" onSubmit={handleActualizar}>
2025-10-02 20:47:38 -06:00
<label className="label">Áreas</label>
2026-01-29 12:35:31 -06:00
2025-10-02 20:47:38 -06:00
<div className="groupInput">
2026-01-29 12:35:31 -06:00
<select
id="areaSelect"
value={selectedArea}
onChange={(e) => setSelectedArea(e.target.value)}
>
<option value="">-- Selecciona una opción --</option>
2026-02-11 16:42:41 -06:00
{areas.map((area: any) => (
<option
key={area.id_area_ubicacion}
value={area.id_area_ubicacion}
>
2026-01-29 12:35:31 -06:00
{area.area}
</option>
))}
2026-02-11 16:42:41 -06:00
2025-09-22 16:53:34 -06:00
</select>
2026-01-29 12:35:31 -06:00
<div className="radio-grid">
<label className="radio-card">
<input
2026-01-29 12:35:31 -06:00
type="radio"
name="modo"
checked={modo === "Activo"}
onChange={() => setModo("Activo")}
/>
<span className="radio-ui"></span>
<span className="radio-text">Activo</span>
2025-09-22 16:53:34 -06:00
</label>
2026-01-29 12:35:31 -06:00
<label className="radio-card">
<input
2026-01-29 12:35:31 -06:00
type="radio"
name="modo"
checked={modo === "Mantenimiento"}
onChange={() => setModo("Mantenimiento")}
/>
<span className="radio-ui"></span>
<span className="radio-text">Mantenimiento</span>
2025-09-22 16:53:34 -06:00
</label>
</div>
<button
className="button buttonSearch"
type="submit"
style={{ marginTop: "15px" }}
>
2025-09-22 16:53:34 -06:00
Actualizar
</button>
</div>
</form>
);
}