summaryrefslogtreecommitdiff
path: root/client/components/App.jsx
blob: 99c2c113985ba27293abfbf8a3a7fd48b9c30ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react'
import LoggedOutView from './LoggedOutView.jsx'
import MealView from './MealView.jsx'

import client from '../client'

export default class App extends React.Component {
  constructor() {
    super()
    this.state = {
      ready: false,
      loggedIn: false,
      user: {},
    }
    client.authenticate()
      .then(user => {
        this.setState({
          ready: true,
          loggedIn: true,
          user: user.data
        })
      })
      .catch(error => {
        this.setState({ ready: true })
        console.error(error)
      })
  }
  logOut() {
    this.setState({
      user: null,
      loggedIn: false,
    })
  }
  render() {
    if (this.state.ready) {
      if (this.state.loggedIn) {
        return (
          <MealView user={this.state.user} />
        )
      }
      else {
        return (
          <LoggedOutView />
        )
      }
    }
    else {
      return (
        <div>Loading...</div>
      )
    }
  }
}