summaryrefslogtreecommitdiff
path: root/client/components/App.jsx
blob: 5a026b44232c0f51bbc971e8b7b29d369139de29 (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
55
56
import React from 'react'
import LoggedOutView from './LoggedOutView.jsx'
import CalorieView from './CalorieView.jsx'

import feathers from 'feathers/client'
import feathersHooks from 'feathers-hooks'
import feathersRest from 'feathers-rest/client'
import feathersAuthentication from 'feathers-authentication/client'
import superagent from 'superagent'

const rest = feathersRest(window.location.origin)

const client = feathers()
              .configure(feathersHooks())
              .configure(feathersAuthentication({ storage: localStorage }))
              .configure(rest.superagent(superagent))

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