-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboard.jsx
213 lines (199 loc) · 7.24 KB
/
Dashboard.jsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import BootstrapTable from 'react-bootstrap-table-next';
import axios from 'axios';
import { ProgressBar, Card, Alert, Button, Col, Form, Modal, Spinner } from 'react-bootstrap';
import 'react-bootstrap-table-next/dist/react-bootstrap-table2.min.css';
import paginationFactory from 'react-bootstrap-table2-paginator';
const quotes = [
"The last three or four reps is what makes the muscle grow. This area of pain divides a champion from someone who is not a champion.",
"Success usually comes to those who are too busy to be looking for it.",
"All progress takes place outside the comfort zone.",
"If you think lifting is dangerous, try being weak. Being weak is dangerous.",
"The only place where success comes before work is in the dictionary.",
"The clock is ticking. Are you becoming the person you want to be?",
"Whether you think you can, or you think you can't, you’re right.",
"The successful warrior is the average man, with laser-like focus.",
"You must expect great things of yourself before you can do them.",
"Action is the foundational key to all success.",
"Things may come to those who wait, but only the things left by those who hustle.",
"Well done is better than well said.",
"All our dreams can come true if we have the courage to pursue them.",
"A champion is someone who gets up when they can't.",
"What hurts today makes you stronger tomorrow.",
"If something stands between you and your success, move it. Never be denied.",
"If you want something you’ve never had, you must be willing to do something you’ve never done.",
"You have to think it before you can do it. The mind is what makes it all possible.",
"Things work out best for those who make the best of how things work out.",
"Success is walking from failure to failure with no loss of enthusiasm."
]
const NoDataIndication = () => (
<div className="spinner">
<Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>
</div>
);
const activity_factors = {
0: 1.2,
1: 1.375,
2: 1.55,
3: 1.725,
4: 1.9
}
const multiplier = {
0: -0.2,
1: -0.1,
2: 0,
3: 0.1,
4: 0.2
}
class Dashboard extends Component {
constructor(props) {
super(props)
this.state = {
columns:
[{
dataField: 'food_id',
hidden: 'true',
text: 'ID'
}, {
dataField: 'name',
text: 'Name',
sort: true
}, {
dataField: 'servings',
text: 'Amount (in g)',
sort: true
}, {
dataField: 'calories',
text: 'Calories',
sort: true
}, {
dataField: 'protein',
text: 'Protein (in g)',
sort: true
}, {
dataField: 'fat',
text: 'Fat (in g)',
sort: true
}, {
dataField: 'carbs',
text: 'Carbs (in g)',
sort: true
}],
history: [],
isLoading: true,
caloric_progress: 0,
tdee: 0,
calories: 0,
carbs: 0,
fats: 0,
protein: 0,
protein_progress: 0,
fats_progress: 0,
carbs_progress: 0,
protein_goal: 0,
fats_goal: 0,
carbs_goal: 0
}
}
getHistory = () => {
axios.get('/api/daily_analytics')
.then(({ data }) => {
this.setState({
isLoading: false,
history: data.daily_history,
caloric_progress: parseInt(100 * data.progress[0].caloric_progress),
tdee: Math.round(data.tdee),
calories: parseInt(data.progress[0].calories),
protein: parseInt(data.progress[0].protein),
fats: parseInt(data.progress[0].fats),
carbs: parseInt(data.progress[0].carbs),
protein_progress: parseInt(100 * data.progress[0].protein_progress),
carbs_progress: parseInt(100 * data.progress[0].carbs_progress),
fats_progress: parseInt(100 * data.progress[0].fats_progress),
protein_goal: Math.round(data.protein_goal),
fats_goal: Math.round(data.fats_goal),
carbs_goal: Math.round(data.carbs_goal)
});
})
.catch(error => console.log('api errors:', error))
}
componentDidMount() {
this.getHistory()
}
render() {
const { isLoading, checkedLogin, isLoggedIn, user } = this.props;
if (!checkedLogin) {
return null;
}
var pb_c = <ProgressBar now={this.state.caloric_progress} />
var pb_p = <ProgressBar now={this.state.protein_progress} />
var pb_ca = <ProgressBar now={this.state.carbs_progress} />
var pb_f = <ProgressBar now={this.state.fats_progress} />
if (this.state.caloric_progress >= 100) {
pb_c = <ProgressBar variant="success" now={this.state.caloric_progress} />
}
if (this.state.protein_progress >= 100) {
pb_p = <ProgressBar variant="success" now={this.state.protein_progress} />
}
if (this.state.carbs_progress >= 100) {
pb_ca = <ProgressBar variant="success" now={this.state.carbs_progress} />
}
if (this.state.fats_progress >= 100) {
pb_f = <ProgressBar variant="success" now={this.state.fats_progress} />
}
const noDataIndication = isLoading ? <NoDataIndication /> : null;
return (
<div className="jumbotron jumbotron-fluid bg-transparent">
<div className="container secondary-color">
<h1 className="display-4">Welcome back, {user.first_name}!</h1>
<p className="lead">
Get started by <a href='/pick_food'> adding your meals </a> for the day!
</p>
<hr className="my-4" />
<h2>Today's Meals 🍞 🍇</h2>
{this.state.history.length > 0 && (
<BootstrapTable
bootstrap4
hover
striped
noDataIndication={() => noDataIndication}
pagination={paginationFactory()}
keyField="food_id" data={ this.state.history } columns={ this.state.columns } />
)}
<Link to="/pick_food" className="btn btn-lg custom-button mr-2" role="button">Add Food</Link>
<hr className="my-4" />
<h2>Meeting Your Goals 💪</h2>
<span> Daily dose of motivation: <i> {quotes[Math.floor(Math.random() * quotes.length)]} </i> </span>
<Card style={{ 'marginTop': '10px' }}>
<Card.Header>Progress</Card.Header>
<Card.Body>
<Card.Text>
Calories: {this.state.calories || 0} / {this.state.tdee} cal
</Card.Text>
<ProgressBar>{pb_c}</ProgressBar>
<hr className="my-4" />
<Card.Text>
Protein: {this.state.protein || 0} / {this.state.protein_goal} g
</Card.Text>
<ProgressBar>{pb_p}</ProgressBar>
<hr className="my-4" />
<Card.Text>
Carbs: {this.state.carbs || 0} / {this.state.carbs_goal} g
</Card.Text>
<ProgressBar>{pb_ca}</ProgressBar>
<hr className="my-4" />
<Card.Text>
Fat: {this.state.fats || 0} / {this.state.fats_goal} g
</Card.Text>
<ProgressBar>{pb_f}</ProgressBar>
</Card.Body>
</Card>
</div>
</div>
);
}
}
export default Dashboard;