diff --git a/src/app/(Operador)/AgregarEquipo/page.tsx b/src/app/(Operador)/AgregarEquipo/page.tsx index a66ffa1..7562249 100644 --- a/src/app/(Operador)/AgregarEquipo/page.tsx +++ b/src/app/(Operador)/AgregarEquipo/page.tsx @@ -1,385 +1,11 @@ "use client"; -import { useState, useEffect } from "react"; -import axios from "axios"; -import Cookies from "js-cookie"; -import toast from "react-hot-toast"; -import "../../styles/layout/agregarEquipo.scss"; -import { useSearchParams } from "next/navigation"; +import dynamic from "next/dynamic"; + +const AgregarEquipo = dynamic(() => import("@/components/AgregarEquipo"), { + ssr: false, +}); export default function Page() { - const searchParams = useSearchParams(); - const inventario = searchParams.get("equipoId"); - - const [formData, setFormData] = useState({ - serie: "", - marca: "", - modelo: "", - tipoEquipo: "", - estado: "", - sistemaOperativo: "", - procesador: "", - tipoUso: "", - observaciones: "", - adscripcion: "", - lugar: "", - responsable: "", - }); - - formData.serie = inventario ? inventario : ""; - - // Tipos de datos - interface TipoUso { - id_tipo_uso: number; - tipo_uso: string; - } - - interface Estado { - id_estado: number; - estado: string; - } - - interface TipoEquipo { - id_tipo_de_equipo: number; - tipo_equipo: string; - } - - interface SistemaOperativo { - id_sistema_operativo: number; - sistema_operativo: string; - } - - interface Procesador { - id_procesador: number; - procesador: string; - } - - interface Marca { - id_marca: number; - tipo_marca: string; - } - - interface Adscripcion { - id_adscripcion: number; - adscripcion: string; - } - - // Estados - const [tiposUso, setTiposUso] = useState([]); - const [marcas, setMarcas] = useState([]); - const [estados, setEstados] = useState([]); - const [adscripciones, setAdscripciones] = useState([]); - const [tiposEquipo, setTiposEquipo] = useState([]); - const [sistemasOperativos, setSistemasOperativos] = useState< - SistemaOperativo[] - >([]); - const [procesadores, setProcesadores] = useState([]); - - const [suggestions, setSuggestions] = useState({ - adscripcion: [] as string[], - }); - - const mostrarCamposComputadora = formData.tipoEquipo !== "Impresora"; - const api_url = process.env.NEXT_PUBLIC_API_URL; - - useEffect(() => { - const fetchData = async () => { - const token = Cookies.get("token"); - const headers = { Authorization: `Bearer ${token}` }; - - try { - const [ - usosRes, - marcasRes, - estadosRes, - adscripcionesRes, - tiposEquipoRes, - sistemasOperativosRes, - procesadoresRes, - ] = await Promise.all([ - axios.get(`${api_url}/equipos/usos`, { headers }), - axios.get(`${api_url}/equipos/marcas`, { headers }), - axios.get(`${api_url}/equipos/estados`, { headers }), - axios.get(`${api_url}/equipos/adscripciones`, { headers }), - axios.get(`${api_url}/equipos/tipos-equipo`, { headers }), - axios.get(`${api_url}/equipos/sistemas-operativos`, { headers }), - axios.get(`${api_url}/equipos/procesadores`, { headers }), - ]); - - setTiposUso(usosRes.data); - setMarcas(marcasRes.data); - setEstados(estadosRes.data); - setAdscripciones(adscripcionesRes.data); - setTiposEquipo(tiposEquipoRes.data); - setSistemasOperativos(sistemasOperativosRes.data); - setProcesadores(procesadoresRes.data); - console.log(adscripciones); - } catch (err) { - if (axios.isAxiosError(err)) { - if (err.response) { - toast.error( - err.response.data?.message || "No se pudo conectar con el API" - ); - } else if (err.request) { - toast.error("No se pudo conectar con el servidor"); - } else { - toast.error("Ocurrió un error inesperado"); - } - } else { - toast.error("Ocurrió un error inesperado"); - } - } - }; - - fetchData(); - }, [api_url]); - - const handleInputChange = (field: string, value: string) => { - setFormData((prev) => ({ ...prev, [field]: value })); - }; - - const handleSelectSuggestion = (field: string, value: string) => { - setFormData((prev) => ({ ...prev, [field]: value })); - setSuggestions((prev) => ({ ...prev, [field]: [] })); - }; - - const handleGuardar = async () => { - try { - const token = Cookies.get("token"); - const headers = { Authorization: `Bearer ${token}` }; - await axios.post(`${api_url}/equipos/crear`, formData, { headers }); - toast.success("Equipo guardado correctamente"); - setFormData({ - serie: "", - marca: "", - modelo: "", - tipoEquipo: "", - estado: "", - sistemaOperativo: "", - procesador: "", - tipoUso: "", - observaciones: "", - adscripcion: "", - lugar: "", - responsable: "", - }); - } catch (err) { - toast.error("Error al guardar el equipo"); - } - }; - - const handleCancelar = () => { - toast.error("Acción cancelada"); - }; - - return ( -
-
-

Agregar Nuevo Equipo

-
- {/* Columna Izquierda */} -
-
- - handleInputChange("serie", e.target.value)} - /> -
- -
- - -
- -
- - handleInputChange("modelo", e.target.value)} - /> -
- -
- - -
- -
- - -
-
- - {/* Columna Centro */} -
-
- - -
- - {mostrarCamposComputadora && ( - <> -
- - -
- -
- - -
- - )} - -
- - - handleInputChange("adscripcion", e.target.value) - } - /> - {suggestions.adscripcion.length > 0 && ( -
    - {suggestions.adscripcion.map((s) => ( -
  • handleSelectSuggestion("adscripcion", s)} - > - {s} -
  • - ))} -
- )} -
-
- -
-
- -