11import React , { useState , useEffect } from "react" ;
22import "./GestionarSalones.css" ;
3- import { getSalones , agregarSalon , actualizarSalon , getSalonByMnemonico } from "../../api/salon" ;
3+ import { getSalones , actualizarSalon , getSalonByMnemonico } from "../../api/salon" ;
4+ import { agregarSalon } from "../../api/usuario/administrador" ;
45import { getRecursos , consultarRecurso , agregarRecurso , actualizarRecurso } from "../../api/recursos" ;
56import { ReactComponent as Editar } from '../../assets/icons/edit-3-svgrepo-com.svg' ;
67import { ReactComponent as Buscar } from '../../assets/icons/Search Glyph.svg' ;
@@ -11,22 +12,31 @@ import EditarSalonModal from "../../components/popup/CRUDSalonModal/EditarSalonM
1112
1213function GestionarSalones ( ) {
1314 const [ salones , setSalones ] = useState ( [ ] ) ;
15+ const [ searchTerm , setSearchTerm ] = useState ( "" ) ;
1416 const [ popup , setPopup ] = useState ( { tipo : "" } ) ;
1517 const [ recursosDisponibles , setRecursosDisponibles ] = useState ( [ ] ) ;
1618 const [ newSalon , setNewSalon ] = useState ( {
17- nombre : "" ,
18- mnemonico : "" ,
19- descripcion : "" ,
20- ubicacion : "" ,
21- capacidad : 0 ,
22- recursos : [ ]
19+ mnemonic : "" ,
20+ name : "" ,
21+ description : "" ,
22+ location : "" ,
23+ capacity : 0 ,
24+ resources : [ ]
2325 } ) ;
26+ const user = { //simulacion de usuario administrador
27+ "idInstitucional" : 1000098257 ,
28+ "nombre" : "Santiago" ,
29+ "apellido" : "Botero" ,
30+ "correoInstitucional" :
"[email protected] " , 31+ "activo" : true ,
32+ "isAdmin" : true
33+ } ;
2434
2535 const abrirPopup = ( tipo , salon = null ) => {
2636 if ( tipo === "editar-salon" && salon ) {
2737 setNewSalon ( salon ) ;
2838 } else if ( tipo === "agregar-salon" ) {
29- setNewSalon ( { nombre : "" , mnemonico : "" , descripcion : "" , ubicacion : "" , capacidad : 0 , recursos : [ ] } ) ;
39+ setNewSalon ( { mnemonic : "" , name : "" , description : "" , location : "" , capacity : 0 , resources : [ ] } ) ;
3040 }
3141 setPopup ( { tipo } ) ;
3242 } ;
@@ -40,9 +50,8 @@ function GestionarSalones() {
4050 async function cargarDatos ( ) {
4151 try {
4252 const dataSalones = await getSalones ( ) ;
43- setSalones ( dataSalones ) ;
44- const dataRecursos = await getRecursos ( ) ;
45- setRecursosDisponibles ( dataRecursos ) ;
53+ console . log ( "Datos de salones cargados:" , dataSalones ) ;
54+ setSalones ( dataSalones || [ ] ) ;
4655 } catch ( error ) {
4756 console . error ( "Error al cargar los datos" , error ) ;
4857 }
@@ -53,24 +62,32 @@ function GestionarSalones() {
5362 // Alternar estado activo/inactivo
5463 const toggleActivo = async ( salonId ) => {
5564 try {
56- setSalones ( ( prevSalones ) =>
57- prevSalones . map ( salon =>
65+ setSalones ( ( prevSalones ) =>
66+ prevSalones . map ( salon =>
5867 salon . id === salonId ? { ...salon , activo : ! salon . activo } : salon
5968 )
6069 ) ;
61-
70+
6271 const salonActualizado = salones . find ( s => s . id === salonId ) ;
6372 await actualizarSalon ( salonId , { activo : ! salonActualizado ?. activo } ) ;
6473 } catch ( error ) {
6574 console . error ( "Error al cambiar estado de salón:" , error ) ;
6675 }
67- } ;
76+ } ;
6877
6978 // Agregar un nuevo salón
7079 const handleAddSalon = async ( ) => {
7180 if ( newSalon . nombre . trim ( ) && newSalon . descripcion . trim ( ) ) {
7281 try {
73- const addedSalon = await agregarSalon ( { ...newSalon , activo : true } ) ;
82+ const formattedSalon = {
83+ mnemonic : newSalon . mnemonico ,
84+ name : newSalon . nombre ,
85+ description : newSalon . descripcion ,
86+ location : newSalon . ubicacion ,
87+ capacity : newSalon . capacidad ,
88+ resources : newSalon . recursos || [ ] ,
89+ } ;
90+ const addedSalon = await agregarSalon ( user . idInstitucional , formattedSalon ) ;
7491 setSalones ( [ ...salones , addedSalon ] ) ;
7592 cerrarPopup ( ) ;
7693 } catch ( error ) {
@@ -96,14 +113,25 @@ function GestionarSalones() {
96113 }
97114 } ;
98115
116+ // Filtrar los salones según el término de búsqueda
117+ const salonesFiltrados = salones . filter ( ( salon ) =>
118+ salon . nombre ?. toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) )
119+ ) ;
120+
99121 return (
100122 < div className = "gestionar-salones" >
101123 < div className = "top-bar" >
102124 < div className = "search-container" >
103125 < Buscar className = "search-icon" />
104- < input type = "text" className = "search-bar" placeholder = "Buscar..." />
126+ < input
127+ type = "text"
128+ className = "search-bar"
129+ placeholder = "Buscar..."
130+ value = { searchTerm }
131+ onChange = { ( e ) => setSearchTerm ( e . target . value ) } // Actualizar el estado de búsqueda
132+ />
105133 </ div >
106- < button className = "add-button" onClick = { ( ) => abrirPopup ( "agregar-salon" ) } >
134+ < button className = "add-button" onClick = { ( ) => abrirPopup ( "agregar-salon" ) } >
107135 < Puerta />
108136 < span > Agregar salón</ span >
109137 </ button >
@@ -120,7 +148,7 @@ function GestionarSalones() {
120148 </ tr >
121149 </ thead >
122150 < tbody >
123- { salones . map ( ( salon ) => (
151+ { salonesFiltrados . map ( ( salon ) => (
124152 < tr key = { salon . id } >
125153 < td > { salon . nombre } </ td >
126154 < td > { salon . descripcion } </ td >
0 commit comments