Advertisement
conformist

instant

Mar 17th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import axios from 'axios'
  4. import {BrowserRouter as Router, Route, Link} from 'react-router-dom'
  5. import createHistory from 'history/createBrowserHistory'
  6.  
  7. const history = createHistory()
  8.  
  9. let getUsers = function(){
  10.   return axios.get('http://localhost:8080/instant/api/?page=' + page)
  11. }
  12.  
  13. let getUser = function(id){
  14.   return axios.get('http://localhost:8080/instant/api/' + id)
  15. }
  16.  
  17. let User = function(props){
  18.   return (
  19.     <div className='user'>
  20.       <div>Name: <Link to={'/profile/' + props.user.id}>{props.user.full_name}</Link></div>
  21.     </div>
  22.   )
  23. }
  24.  
  25. let Users = React.createClass({
  26.   getInitialState: function(){
  27.     return {
  28.       link: 'http://localhost:8080/instant/api',
  29.       users: [],
  30.       previous: '',
  31.       next: '',
  32.       count: 0
  33.     }
  34.   },
  35.   componentDidMount: function(){
  36.     axios.get(this.state.link).then(xhr => {
  37.       this.setState({
  38.         users: xhr.data.results,
  39.         previous: xhr.data.previous,
  40.         next: xhr.data.next,
  41.         count: xhr.data.count
  42.       });
  43.     })
  44.   },
  45.   render: function(){
  46.     return (
  47.       <div>
  48.         <h1>Our users are (overall count: {this.state.count}):</h1>
  49.         <hr />
  50.         {this.state.users.map(user => {
  51.           return <User user={user} key={user.id} />
  52.         })}
  53.       </div>
  54.     );
  55.   }
  56. })
  57.  
  58. let UserProfile = React.createClass({
  59.   getInitialState: function() {
  60.     return {
  61.       name: null,
  62.       age: null,
  63.       birth: null
  64.     }
  65.   },
  66.   componentDidMount: function() {
  67.     getUser(this.props.match.params.id).then(xhr => {
  68.       this.setState({
  69.         name: xhr.data.full_name,
  70.         age: xhr.data.age,
  71.         birth: xhr.data.birth
  72.       })
  73.     })
  74.   },
  75.   render: function() {
  76.     return (
  77.       <div>
  78.         <h3>User Profile: {this.state.name}</h3>
  79.         <div>Age: {this.state.age}</div>
  80.         <div>Birth: {this.state.birth}</div>
  81.         <div>
  82.           <button onClick={history.goBack}>back</button>
  83.         </div>
  84.       </div>
  85.     )
  86.   }
  87. })
  88.  
  89. ReactDOM.render((
  90.   <Router>
  91.     <div>
  92.       <Route exact path="/" component={Users} />
  93.       <Route path="/profile/:id" component={UserProfile} />
  94.     </div>
  95.   </Router>
  96. ), document.querySelector('#container'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement