Skip to content
Snippets Groups Projects
Select Git revision
  • 07ba46a7cdf273a8dbe3a143906e55a5dd0d445d
  • master default protected
2 results

borrowform.jsx

Blame
  • borrowform.jsx 3.39 KiB
    import React, { Component } from 'react'
    import './forms.css'
    import axios from 'axios';
    
    export default class Borrow extends Component{
       constructor(props){
          super(props);
          this.state = {
             User_id: '',
             name: '',
            book_id: '',
            object :{} ,
            not_borrowed: true ,
            exist: false  ,
               
           }
           
          this.handleChange=this.handleChange.bind(this);
          this.borrow=this.borrow.bind(this);
          this.check = this.check.bind(this);
          this.check1 = this.check1.bind(this);
          this.check2 = this.check2.bind(this);      
       }
       componentWillMount(){
        this.setState({User_id:this.props.uid,name:this.props.uname});
       
    }
       handleChange (event) {
          this.setState({ [event.target.name]: event.target.value});
         
       }
       check2(event){
          axios.get('http://localhost:5000/borrower/'+this.state.User_id)
          .then(response => {
             console.log(response.data)
              this.setState({object:response.data})
              this.check1();
          })
          event.preventDefault();
       }
       check1(){
          axios.get('http://localhost:5000/book')
          .then(resp => {
              resp.data.map((obj)=> {
                 if(obj._id===this.state.book_id){
                    this.setState({exist:true})
                 }
              })
              this.check();
          })
         
       }
       check(){
          
          axios.get('http://localhost:5000/borrower')
          .then(resp => {
             resp.data.map((obj)=>
             { 
                if(obj.book_id===this.state.book_id){
                      this.setState({not_borrowed:false})      
                }
             })
             this.borrow();
          })
         
       }
       borrow(){
         if(this.state.exist){
            console.log('exist : '+this.state.exist)
          if(this.state.not_borrowed){
            console.log('not borrowed : '+this.state.not_borrowed)
            
          console.log(this.state.object)
          if(this.state.object===null){
             const user =  {
                user_id: this.state.User_id,
                user_name: this.state.name,
                book_id:this.state.book_id
           }
             axios.post('http://localhost:5000/borrower/add',user)
            .then(window.alert("Borrowed Successfully!"))
            .catch(console.error())
            this.setState({exist:false})
          }
         else{
          const user =  {
             user_id: this.state.User_id,
             user_name: this.state.name,
             book_id:this.state.book_id
        }
         axios.post('http://localhost:5000/borrower/update/'+this.state.User_id,user)
        .then(window.alert("Borrowed Successfully!"))
        .catch(console.error())
        this.setState({exist:false})
         }
          }
       else {
          window.alert(this.state.book_id+' is already borrowed!')
          this.setState({not_borrowed:true})
       }
        
         }
       else {
          window.alert(this.state.book_id+' doesnt exist! Use search button to find books')
         
       }
         
        }
       render(){
          return(
             <div>
             <h3 style={{fontFamily:"Roboto",marginLeft:"20px"}}>
                <b>BORROW BOOK</b>
             </h3>
             <form>
             
             <ul style={{listStyle:"none"}}>
             <li><div>
             <input type="text" placeholder="Book_id" name="book_id" onChange={this.handleChange} required/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            
             </div></li>    
             <li><div>
             <input type="submit" value="Borrow" onClick={this.check2}/></div></li>
             </ul>
            
            </form>
            </div>
     );
       }
    }