xonecas.io
3517
Mt

meal_tracker / files

meal_tracker / bmi_ui / src / components / Home.js

2.2 KB
1import React, { Fragment, Component } from 'react'
2import axios from 'axios'
3import { API_URL } from '../constants'
4import Header from './Header'
5import MealsTable from './MealsTable'
6import MealModal from './MealModal'
7import FilterModal from './FilterModal'
8import { Jumbotron, Col, Container, Row } from 'reactstrap'
9
10class Home extends Component {
11  constructor (props) {
12    super(props)
13
14    this.state = {
15      user: false,
16      meals: []
17    }
18  }
19
20  setUser (u) {
21    this.setState({ user: u })
22    this.getMeals()
23  }
24
25  setMeals (m) {
26    this.setState({ meals: m })
27  }
28
29  getMeals () {
30    axios.get(
31      API_URL + 'meals',
32      { crossdomain: true, headers: { Authorization: 'Token ' + this.state.user.token } }
33    ).then(res => {
34      this.setState({
35        meals: res.data
36      })
37    })
38  }
39
40  render () {
41    let container = ''
42
43    if (this.state.user) {
44      container = (
45        <Container style={{ marginTop: '20px' }}>
46          <Row>
47            <Col>
48              <div className="mb-3 float-right">
49                <FilterModal
50                  user={this.state.user}
51                  update={this.setMeals.bind(this)}
52                />
53              </div>
54            </Col>
55          </Row>
56          <Row>
57            <Col>
58              <MealsTable
59                user={this.state.user}
60                meals={this.state.meals}
61                update={this.getMeals.bind(this)}
62              />
63            </Col>
64          </Row>
65          <Row>
66            <Col>
67              <div className="float-right">
68                <MealModal
69                  create={true}
70                  user={this.state.user}
71                  update={this.getMeals.bind(this)}>
72                </MealModal>
73              </div>
74            </Col>
75          </Row>
76        </Container>
77      )
78    } else {
79      container = (
80        <div>
81          <Jumbotron>
82            <h1 className="display-3">Meal track</h1>
83            <p className="lead">Please login or create an account to start</p>
84          </Jumbotron>
85        </div>
86      )
87    }
88
89    return (
90      <Fragment>
91        <Header user={this.state.user} update={this.setUser.bind(this)} />
92        {container}
93      </Fragment>
94    )
95  }
96}
97
98export default Home