clean up saveTenant function
This commit is contained in:
		
							parent
							
								
									f6ce86909f
								
							
						
					
					
						commit
						dc65cdda4f
					
				| 
						 | 
					@ -98,38 +98,39 @@ const Inquilinos = () => {
 | 
				
			||||||
    value: item._id,
 | 
					    value: item._id,
 | 
				
			||||||
  }))
 | 
					  }))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function registrarInquilino() {
 | 
					  const saveTenant = () => {
 | 
				
			||||||
    let newTenant = {
 | 
					    if (tenant.email && tenant.number_house) {
 | 
				
			||||||
      _id: null,
 | 
					      let _tenants = [...tenants]
 | 
				
			||||||
      dni: '',
 | 
					      let _tenant = { ...tenant }
 | 
				
			||||||
      name: '',
 | 
					      _tenant.community_id = communityId
 | 
				
			||||||
      last_name: '',
 | 
					      console.log(_tenant)
 | 
				
			||||||
      email: document.getElementById('correo_electronico').value,
 | 
					 | 
				
			||||||
      phone: '',
 | 
					 | 
				
			||||||
      password: '',
 | 
					 | 
				
			||||||
      community_id: document.getElementById('numero_vivienda').value,
 | 
					 | 
				
			||||||
      community_name: '',
 | 
					 | 
				
			||||||
      number_house: 'Sin número de vivienda',
 | 
					 | 
				
			||||||
      date_entry: new Date(),
 | 
					 | 
				
			||||||
      user_type: '3',
 | 
					 | 
				
			||||||
      status: '1',
 | 
					 | 
				
			||||||
      status_text: '',
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fetch('http://localhost:4000/api/createUser', {
 | 
					      fetch(`http://localhost:4000/user/createUser`, {
 | 
				
			||||||
      method: 'POST',
 | 
					        cache: 'no-cache',
 | 
				
			||||||
      cache: 'no-cache',
 | 
					        method: 'POST',
 | 
				
			||||||
      body: JSON.stringify(newTenant),
 | 
					        body: JSON.stringify(tenant),
 | 
				
			||||||
      headers: {
 | 
					        headers: {
 | 
				
			||||||
        'Content-Type': 'application/json',
 | 
					          'Content-Type': 'application/json',
 | 
				
			||||||
      },
 | 
					        },
 | 
				
			||||||
    }).then((response) => {
 | 
					      })
 | 
				
			||||||
      if (response.ok) {
 | 
					        .then((response) => {
 | 
				
			||||||
        alert('Inquilino registrado correctamente')
 | 
					          if (response.status !== 201)
 | 
				
			||||||
      } else {
 | 
					            console.log(`Hubo un error en el servicio: ${response.status}`)
 | 
				
			||||||
        alert('Error al registrar inquilino')
 | 
					          else return response.json()
 | 
				
			||||||
      }
 | 
					        })
 | 
				
			||||||
    })
 | 
					        .then(() => {
 | 
				
			||||||
 | 
					          _tenants.push(_tenant)
 | 
				
			||||||
 | 
					          toast.current.show({
 | 
				
			||||||
 | 
					            severity: 'success',
 | 
				
			||||||
 | 
					            summary: 'Éxito',
 | 
				
			||||||
 | 
					            detail: 'Inquilino creado',
 | 
				
			||||||
 | 
					            life: 3000,
 | 
				
			||||||
 | 
					          })
 | 
				
			||||||
 | 
					          setTenants(_tenants)
 | 
				
			||||||
 | 
					          setTenant(emptyTenant)
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .catch((error) => console.log(`Ocurrió un error: ${error}`))
 | 
				
			||||||
 | 
					    } else setSubmitted(true)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const deleteTenant = () => {
 | 
					  const deleteTenant = () => {
 | 
				
			||||||
| 
						 | 
					@ -418,6 +419,13 @@ const Inquilinos = () => {
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const onInputChange = (e) => {
 | 
				
			||||||
 | 
					    const value = (e.target && e.target.value) || ''
 | 
				
			||||||
 | 
					    let _tenant = { ...tenant }
 | 
				
			||||||
 | 
					    _tenant[`${name}`] = value
 | 
				
			||||||
 | 
					    setTenant(_tenant)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <div className='grid'>
 | 
					    <div className='grid'>
 | 
				
			||||||
      <div className='col-12'>
 | 
					      <div className='col-12'>
 | 
				
			||||||
| 
						 | 
					@ -618,7 +626,7 @@ const Inquilinos = () => {
 | 
				
			||||||
                onChange={(e) => setCommunityId(e.value)}
 | 
					                onChange={(e) => setCommunityId(e.value)}
 | 
				
			||||||
              />
 | 
					              />
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <Button label='Registrar' onClick={registrarInquilino} />
 | 
					            <Button label='Registrar' onClick={saveTenant} />
 | 
				
			||||||
          </div>
 | 
					          </div>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue