@@ -3,25 +3,40 @@ import "./CRUDSalonModal.css";
33import CRUDSalonForm from "./CRUDSalonForm" ;
44
55function EditarSalonModal ( { onClose, newSalon, setNewSalon, handleEdit } ) {
6- const defaultSalon = {
7- nombre : "" ,
8- descripcion : "" ,
9- mnemonico : "" ,
10- ubicacion : "" ,
11- capacidad : 0 ,
12- recursos : [ ] ,
6+ const [ isInitialized , setIsInitialized ] = useState ( false ) ;
7+ const [ tempSalon , setTempSalon ] = useState ( {
8+ mnemonico : newSalon ?. mnemonico || "" ,
9+ nombre : newSalon ?. nombre || "" ,
10+ descripcion : newSalon ?. descripcion || "" ,
11+ ubicacion : newSalon ?. ubicacion || "" ,
12+ capacidad : newSalon ?. capacidad || 0 ,
13+ recursos : newSalon ?. recursos || [ { nombre : "" , cantidad : 1 , especificaciones : [ ] , activo : true } ] ,
14+ } ) ;
15+
16+ const isFormComplete = ( ) => {
17+ return (
18+ tempSalon . mnemonico . trim ( ) !== "" &&
19+ tempSalon . nombre . trim ( ) !== "" &&
20+ tempSalon . descripcion . trim ( ) !== "" &&
21+ tempSalon . ubicacion . trim ( ) !== "" &&
22+ tempSalon . capacidad > 0
23+ ) ;
1324 } ;
14- const [ tempSalon , setTempSalon ] = useState ( newSalon || defaultSalon ) ;
1525
1626 useEffect ( ( ) => {
17- if ( newSalon ) {
27+ if ( ! isInitialized && newSalon ) {
1828 setTempSalon ( newSalon ) ;
29+ setIsInitialized ( true ) ;
1930 }
20- } , [ newSalon ] ) ;
31+ } , [ newSalon , isInitialized ] ) ;
2132
2233 const handleGuardar = ( ) => {
23- setNewSalon ( tempSalon ) ;
24- handleEdit ( ) ;
34+ const updatedSalon = {
35+ ...tempSalon ,
36+ recursos : tempSalon . recursos ?. length > 0 ? tempSalon . recursos : [ { nombre : "" , cantidad : 1 , especificaciones : [ ] , activo : true } ] ,
37+ } ;
38+ setNewSalon ( updatedSalon ) ;
39+ handleEdit ( updatedSalon ) ;
2540 } ;
2641
2742 return (
@@ -36,7 +51,7 @@ function EditarSalonModal({ onClose, newSalon, setNewSalon, handleEdit }) {
3651 />
3752 < div className = "modal-buttons" >
3853 < button className = "cancel-button" onClick = { onClose } > Cancelar</ button >
39- < button className = "save-button" onClick = { handleGuardar } > Guardar</ button >
54+ < button className = "save-button" onClick = { handleGuardar } disabled = { ! isFormComplete ( ) } > Guardar</ button >
4055 </ div >
4156 </ div >
4257 </ div >
0 commit comments