Advertisement
Qpel

praktinis 3 - done

Nov 14th, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Komanda: npm add @babel/plugin-proposal-class-properties --save
  2.  
  3.  
  4. import React, {Component} from 'react';
  5.  
  6.  
  7. class App extends Component {
  8.  
  9.     constructor(props){
  10.         super(props)
  11.         this.onHandleUsers = this.onHandleUsers.bind(this)
  12.         this.tekstas = "a";
  13.      
  14.         this.state = {
  15.             isLoaded: false,
  16.             users: []
  17.         }
  18.     }
  19.      
  20.      
  21.      
  22.      
  23.     handleUserChange = (e) => {
  24.         const {value} = e.target
  25.         this.onHandleCurrentUsers(value)
  26.         this.tekstas = this.onHandleCurrentUsers(userId)
  27.      
  28.       }
  29.      
  30.     onHandleUsers() {
  31.         fetch("http://localhost:3000/api/users")
  32.         .then(res => res.json())
  33.         .then(result => {
  34.             this.setState({
  35.                 isLoaded: true,
  36.                 users: result
  37.             })
  38.         })
  39.         .catch(e => console.log(e))
  40.     }
  41.      
  42.     onHandleCurrentUsers(userId) {
  43.         const data = {userId: userId}
  44.         fetch("http://localhost:3000/api/user", {
  45.             method: 'POST',
  46.             headers: { "Content-Type": "application/json"},
  47.             body: JSON.stringify(data)
  48.         })
  49.         .then(res => res.json())
  50.         .then(result => {
  51.             this.setState({
  52.                 isLoaded: false,
  53.                 currentUser: result
  54.             })
  55.         })
  56.         .catch(e => console.log(e))
  57.     }
  58.      
  59.      
  60.      
  61.      
  62.     render() {
  63.         const {currentUser} = this.state
  64.         return (
  65.             <div>
  66.                  
  67.                 <div>
  68.                     {
  69.                         currentUser && <div>{currentUser.name} {currentUser.surname}</div>
  70.                     }
  71.                 </div>
  72.                 <div>
  73.                    
  74.                 <form onSubmit={this.handleUserChange}>
  75.             <label>
  76.                 <input
  77.             name="userId"
  78.             type="number"
  79.             //value={this.handleUserChange}
  80.             onChange={this.handleUserChange}
  81.             />
  82.             </label>
  83.                 <input type="submit" value="Submit" />
  84.                    
  85.                     </form>
  86.                     {this.tekstas}
  87.                  </div>
  88.      
  89.                     <div> </div>  
  90.         </div>
  91.      
  92.         )
  93.     }
  94.     }
  95.      
  96.      
  97.     export default App
  98.  
  99.  
  100. UserApi:
  101.  
  102.  
  103. class UserApiController {
  104.     constructor(){
  105.         this.getUsers = this.getUsers.bind(this)
  106.         this.findUser = this.findUser.bind(this)
  107.     }
  108.     _usersList(){
  109.         return [
  110.             {id: 1, name: 'Jonas', surname: 'Grybas'},
  111.             {id: 2, name: 'Ernestas', surname: 'Arlauskas'},
  112.             {id: 3, name: 'Fetras', surname: 'Fetrovicius'},
  113.             {id: 4, name: 'Gvazdykas', surname: 'Ornamentas'},
  114.             {id: 5, name: 'Jurgis', surname: 'Jonaitis'}
  115.         ]
  116.     }
  117.  
  118.     getUsers(req, res, next){
  119.         const users = this._usersList()
  120.         return res.json(users)
  121.     }
  122.     findUser(req, res, next) {
  123.         const userId = parseInt(req.body.userId) || 0
  124.         const users = this._usersList()
  125.         const amount = users.length
  126.         let result = {}
  127.         for (let i = 0; i < amount; i++) {
  128.             let user = users[i]
  129.             if(user.id == userId){
  130.                 result = user
  131.                 break
  132.             }
  133.         }
  134.         return res.json(result)
  135.        
  136.     }
  137. }
  138.  
  139. module.exports = UserApiController
  140.  
  141.  
  142.  
  143. babelrc:
  144.  
  145. {
  146.     "presets": ["@babel/preset-env", "@babel/preset-react"],
  147.  
  148.  
  149.      "plugins": [
  150.             "@babel/plugin-proposal-class-properties"]
  151.  
  152.    
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement