xonecas.io
3517
Mt

meal_tracker / commit 82894e0

Add frontend for filtering

Implement a reduced query set for filtering.

xonecas2020-03-06 82894e0d2386749bcd123098f18b893be753c895 parent 5b52312

19 files changed

bmi_api/migrations/0001_initial.py+6−4
@@ -1,8 +1,10 @@
1-# Generated by Django 3.0.4 on 2020-03-06 11:05
1+# Generated by Django 3.0.4 on 2020-03-06 16:06
22
3+import datetime
34 from django.conf import settings
45 from django.db import migrations, models
56 import django.db.models.deletion
7+import django.utils.timezone
68
79
810 class Migration(migrations.Migration):
@@ -19,15 +21,15 @@ class Migration(migrations.Migration):
1921 fields=[
2022 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
2123 ('max_calories', models.IntegerField(default=2000)),
22- ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
24+ ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='preferences', to=settings.AUTH_USER_MODEL)),
2325 ],
2426 ),
2527 migrations.CreateModel(
2628 name='Meal',
2729 fields=[
2830 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
29- ('date', models.DateField(auto_now_add=True, verbose_name='Meal date')),
30- ('time', models.TimeField(auto_now_add=True, verbose_name='Meal time')),
31+ ('date', models.DateField(default=datetime.date.today, verbose_name='Meal date')),
32+ ('time', models.TimeField(default=django.utils.timezone.now, verbose_name='Meal time')),
3133 ('text', models.TextField(verbose_name='Meal description or title')),
3234 ('calories', models.IntegerField()),
3335 ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='meals', to=settings.AUTH_USER_MODEL)),
bmi_api/migrations/0002_auto_20200306_1115.py+0−21
@@ -1,21 +0,0 @@
1-# Generated by Django 3.0.4 on 2020-03-06 11:15
2-
3-from django.conf import settings
4-from django.db import migrations, models
5-import django.db.models.deletion
6-
7-
8-class Migration(migrations.Migration):
9-
10- dependencies = [
11- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12- ('bmi_api', '0001_initial'),
13- ]
14-
15- operations = [
16- migrations.AlterField(
17- model_name='userpreferences',
18- name='user',
19- field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='preferences', to=settings.AUTH_USER_MODEL),
20- ),
21- ]
bmi_api/models.py+4−2
@@ -1,5 +1,7 @@
1+from datetime import date
12 from django.contrib.auth.models import User
23 from django.db import models
4+from django.utils import timezone
35
46
57 class UserPreferences(models.Model):
@@ -9,8 +11,8 @@ class UserPreferences(models.Model):
911
1012 class Meal(models.Model):
1113 owner = models.ForeignKey('auth.User', related_name='meals', on_delete=models.CASCADE)
12- date = models.DateField("Meal date", auto_now_add=True)
13- time = models.TimeField("Meal time", auto_now_add=True)
14+ date = models.DateField("Meal date", default=date.today)
15+ time = models.TimeField("Meal time", default=(lambda: timezone.now().time()))
1416 text = models.TextField("Meal description or title")
1517 calories = models.IntegerField()
1618
bmi_api/permissions.py+7−1
@@ -10,4 +10,10 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
1010 if request.method in permissions.SAFE_METHODS:
1111 return True
1212
13- return obj.owner == request.user
13+ if hasattr(obj, 'owner'):
14+ return obj.owner == request.user
15+
16+ if hasattr(obj, 'user'):
17+ return obj.user == request.user
18+
19+ return obj == request.user
bmi_api/serializers.py+4−0
@@ -60,3 +60,7 @@ class MealSerializer(serializers.ModelSerializer):
6060 model = Meal
6161 fields = '__all__'
6262 ordering = ['created']
63+ extra_kwargs = {
64+ 'date': {'required': False},
65+ 'time': {'required': False},
66+ }
bmi_api/views.py+23−2
@@ -32,7 +32,7 @@ def login(request):
3232
3333
3434 class UserViewSet(viewsets.ModelViewSet):
35- permission_classes = [permissions.IsAdminUser, IsOwnerOrReadOnly]
35+ permission_classes = [IsOwnerOrReadOnly]
3636 queryset = User.objects.all()
3737 serializer_class = UserSerializer
3838
@@ -45,9 +45,30 @@ class UserViewSet(viewsets.ModelViewSet):
4545
4646
4747 class MealViewSet(viewsets.ModelViewSet):
48- permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
4948 queryset = Meal.objects.all()
49+ permission_classes = [IsOwnerOrReadOnly]
5050 serializer_class = MealSerializer
5151
52+ def get_queryset(self):
53+ if self.request.user.is_anonymous:
54+ return Meal.objects.none()
55+
56+ queryset = Meal.objects.filter(owner=self.request.user)
57+
58+ query_params = self.request.query_params
59+
60+ from_date = query_params.get('date_from', None)
61+ to_date = query_params.get('date_to', None)
62+ from_time = query_params.get('time_from', None)
63+ to_time = query_params.get('time_to', None)
64+
65+ if from_date is not None and to_date is not None:
66+ queryset = queryset.filter(date__lte=from_date, date__gte=to_date)
67+
68+ if from_time is not None and to_time is not None:
69+ queryset = queryset.filter(time__lte=from_time, time__gte=to_time)
70+
71+ return queryset
72+
5273 def perform_create(self, serializer):
5374 serializer.save(owner=self.request.user)
bmi_track/settings.py+0−2
@@ -46,8 +46,6 @@ INSTALLED_APPS = [
4646 REST_FRAMEWORK = {
4747 'DEFAULT_AUTHENTICATION_CLASSES': (
4848 'rest_framework.authentication.TokenAuthentication',
49- 'rest_framework.authentication.BasicAuthentication',
50- 'rest_framework.authentication.SessionAuthentication',
5149 ),
5250 }
5351
bmi_ui/src/components/FilterForm.js+94−0
@@ -0,0 +1,94 @@
1+import React from 'react';
2+import { Button, Form, FormGroup, Input, Label } from 'reactstrap';
3+import axios from 'axios';
4+import { API_URL } from '../constants';
5+
6+
7+class UserForm extends React.Component {
8+ constructor(props) {
9+ super(props);
10+
11+ this.state = {
12+ date_from: '',
13+ date_to: '',
14+ time_from: '',
15+ time_to: ''
16+ };
17+ }
18+
19+ onChange = e => {
20+ this.setState({ [e.target.name]: e.target.value });
21+ };
22+
23+ filter = e => {
24+ console.log(this.props.user.token)
25+ e && e.preventDefault()
26+
27+ var params = {}
28+ if (this.state.date_from !== "") params.date_from = this.state.date_from;
29+ if (this.state.date_to !== "") params.date_to = this.state.date_to;
30+ if (this.state.time_from !== "") params.time_from = this.state.time_from;
31+ if (this.state.time_to !== "") params.time_to = this.state.time_to;
32+
33+ axios.get(API_URL + 'meals', {
34+ params: params,
35+ crossdomain: true,
36+ headers: { 'Authorization': 'Token ' + this.props.user.token }
37+ }).then((response) => {
38+ this.props.toggle();
39+ this.props.update(response.data);
40+ });
41+
42+ };
43+
44+ defaultIfEmpty = value => {
45+ return value === "" ? "" : value;
46+ };
47+
48+ render() {
49+ return (
50+ <Form onSubmit={this.filter}>
51+ <FormGroup>
52+ <Label for="date_from">Start date:</Label>
53+ <Input
54+ type="date"
55+ name="date_from"
56+ onChange={this.onChange}
57+ value={this.defaultIfEmpty(this.state.date_from)}
58+ />
59+ </FormGroup>
60+ <FormGroup>
61+ <Label for="date_to">End date:</Label>
62+ <Input
63+ type="date"
64+ name="date_to"
65+ onChange={this.onChange}
66+ value={this.defaultIfEmpty(this.state.date_to)}
67+ />
68+ </FormGroup>
69+ <FormGroup>
70+ <Label for="date_from">Start time:</Label>
71+ <Input
72+ type="time"
73+ name="time_from"
74+ onChange={this.onChange}
75+ value={this.defaultIfEmpty(this.state.time_from)}
76+ />
77+ </FormGroup>
78+ <FormGroup>
79+ <Label for="date_from">End time:</Label>
80+ <Input
81+ type="time"
82+ name="time_to"
83+ onChange={this.onChange}
84+ value={this.defaultIfEmpty(this.state.time_to)}
85+ />
86+ </FormGroup>
87+
88+ <Button className="float-right" color="primary">Filter</Button>
89+ </Form>
90+ )
91+ }
92+}
93+
94+export default UserForm;
bmi_ui/src/components/FilterModal.js+49−0
@@ -0,0 +1,49 @@
1+import React, { Component, Fragment } from "react";
2+import { Button, Modal, ModalHeader, ModalBody } from "reactstrap";
3+import FilterForm from './FilterForm';
4+
5+
6+class FilterModal extends Component {
7+ constructor(props) {
8+ super(props);
9+
10+ this.state = {
11+ modal: false
12+ }
13+ }
14+
15+ toggle = (e) => {
16+ e && e.preventDefault();
17+
18+ this.setState(previous => ({
19+ modal: !previous.modal
20+ }));
21+ };
22+
23+ render() {
24+ return (
25+ <Fragment>
26+ <Button
27+ className="mr-3"
28+ onClick={this.toggle}
29+ size="sm"
30+ outline
31+ color="primary">
32+ Filter
33+ </Button>
34+ <Modal isOpen={this.state.modal} toggle={this.toggle}>
35+ <ModalHeader toggle={this.toggle}>Filter</ModalHeader>
36+ <ModalBody>
37+ <FilterForm
38+ toggle={this.toggle}
39+ user={this.props.user}
40+ update={this.props.update}
41+ />
42+ </ModalBody>
43+ </Modal>
44+ </Fragment>
45+ );
46+ }
47+}
48+
49+export default FilterModal;
bmi_ui/src/components/Header.js+9−3
@@ -1,5 +1,5 @@
11 import React from 'react';
2-import { Nav, Navbar, NavbarBrand, NavItem } from 'reactstrap';
2+import { Nav, Navbar, NavbarBrand, NavItem, NavbarText } from 'reactstrap';
33 import UserModal from './UserModal';
44 import LoginModal from './LoginModal';
55
@@ -8,7 +8,13 @@ const Header = (props) => {
88 if (!props.user) {
99 login = (
1010 <NavItem>
11- <LoginModal user={props.user} />
11+ <LoginModal user={props.user} update={props.update} />
12+ </NavItem>
13+ );
14+ } else {
15+ login = (
16+ <NavItem>
17+ <NavbarText className="text-light p-1">{props.user.username}</NavbarText>
1218 </NavItem>
1319 );
1420 }
@@ -19,7 +25,7 @@ const Header = (props) => {
1925 <Nav className="ml-auto" navbar>
2026 {login}
2127 <NavItem className="ml-1">
22- <UserModal create={!props.user} user={props.user} />
28+ <UserModal create={!props.user} user={props.user} update={props.update} />
2329 </NavItem>
2430 </Nav>
2531 </Navbar>
bmi_ui/src/components/Home.js+64−13
@@ -2,44 +2,95 @@ import React, { Fragment, Component } from "react";
22 import axios from 'axios';
33 import { API_URL } from '../constants';
44 import Header from './Header';
5-import { Col, Container, Row } from "reactstrap";
5+import MealsTable from './MealsTable';
6+import MealModal from './MealModal';
7+import FilterModal from './FilterModal';
8+import { Jumbotron, Col, Container, Row } from "reactstrap";
69
710
811 class Home extends Component {
9- state = {
10- user: false,
11- meals: []
12+ constructor(props) {
13+ super(props)
14+
15+ this.state = {
16+ user: false,
17+ meals: []
18+ }
1219 }
1320
14- componentDidMount() {
15- this.getMeals()
21+ setUser = (u) => {
22+ this.setState({ user: u });
23+ this.getMeals();
24+ };
25+
26+ setMeals = (m) => {
27+ this.setState({ meals: m });
1628 }
1729
1830 getMeals = () => {
19- axios.get(API_URL + 'meals', { crossdomain: true }).then(res => {
31+ axios.get(
32+ API_URL + 'meals',
33+ { crossdomain: true, headers: { Authorization: 'Token ' + this.state.user.token }}
34+ ).then(res => {
2035 this.setState({
21- user: this.state.user,
2236 meals: res.data
2337 });
2438 })
2539 };
2640
2741 render() {
28- return (
29- <Fragment>
30- <Header user={this.state.user} />
42+ var container = '';
43+
44+ if (this.state.user) {
45+ container = (
3146 <Container style={{ marginTop: "20px" }}>
3247 <Row>
3348 <Col>
34- A list of meals will go here, with edit + delete
49+ <div className="mb-3 float-right">
50+ <FilterModal
51+ user={this.state.user}
52+ update={this.setMeals.bind(this)}
53+ />
54+ </div>
3555 </Col>
3656 </Row>
3757 <Row>
3858 <Col>
39- Add new meal cta
59+ <MealsTable
60+ user={this.state.user}
61+ meals={this.state.meals}
62+ update={this.getMeals.bind(this)}
63+ />
64+ </Col>
65+ </Row>
66+ <Row>
67+ <Col>
68+ <div className="float-right">
69+ <MealModal
70+ create={true}
71+ user={this.state.user}
72+ update={this.getMeals.bind(this)}
73+ />
74+ </div>
4075 </Col>
4176 </Row>
4277 </Container>
78+ );
79+ } else {
80+ container = (
81+ <div>
82+ <Jumbotron>
83+ <h1 className="display-3">Meal track</h1>
84+ <p className="lead">Please login or create an account to start</p>
85+ </Jumbotron>
86+ </div>
87+ );
88+ }
89+
90+ return (
91+ <Fragment>
92+ <Header user={this.state.user} update={this.setUser.bind(this)} />
93+ {container}
4394 </Fragment>
4495 );
4596 }
bmi_ui/src/components/LoginForm.js+13−7
@@ -5,19 +5,25 @@ import { API_URL } from '../constants';
55
66
77 class UserForm extends React.Component {
8- state = {
9- username: '',
10- password: ''
11- };
8+ constructor(props) {
9+ super(props)
10+
11+ this.state = {
12+ username: '',
13+ password: ''
14+ };
15+ }
1216
1317 onChange = e => {
1418 this.setState({ [e.target.name]: e.target.value });
1519 };
1620
1721 login = e => {
18- e.preventDefault();
19- axios.post(API_URL + 'login/', this.state, { crossdomain: true }).then(() => {
20- this.props.toggle(e);
22+ e && e.preventDefault();
23+
24+ axios.post(API_URL + 'login/', this.state, { crossdomain: true }).then((response) => {
25+ this.props.toggle();
26+ this.props.update(response.data);
2127 });
2228 };
2329
bmi_ui/src/components/LoginModal.js+7−2
@@ -4,8 +4,12 @@ import LoginForm from './LoginForm';
44
55
66 class UserModal extends Component {
7- state = {
8- modal: false
7+ constructor(props) {
8+ super(props)
9+
10+ this.state = {
11+ modal: false
12+ }
913 }
1014
1115 toggle = (e) => {
@@ -26,6 +30,7 @@ class UserModal extends Component {
2630 <ModalBody>
2731 <LoginForm
2832 toggle={this.toggle}
33+ update={this.props.update}
2934 />
3035 </ModalBody>
3136 </Modal>
bmi_ui/src/components/MealForm.js+103−0
@@ -0,0 +1,103 @@
1+import React from 'react';
2+import { Button, Form, FormGroup, Input, Label } from 'reactstrap';
3+import axios from 'axios';
4+import { API_URL } from '../constants';
5+
6+
7+class MealForm extends React.Component {
8+ constructor(props) {
9+ super(props);
10+
11+ const meal = props.meal || {};
12+
13+ this.state ={
14+ id: meal.id,
15+ text: meal.text || '',
16+ calories: meal.calories || 0,
17+ date: meal.date,
18+ time: meal.time
19+ };
20+ }
21+
22+ onChange = e => {
23+ this.setState({ [e.target.name]: e.target.value });
24+ };
25+
26+ create = e => {
27+ e.preventDefault();
28+
29+ axios.post(API_URL + 'meals/', this.state, {
30+ crossdomain: true,
31+ headers: { 'Authorization': 'Token ' + this.props.user.token }
32+ }).then((response) => {
33+ this.props.toggle();
34+ this.props.update();
35+ });
36+ };
37+
38+ edit = e => {
39+ e.preventDefault();
40+
41+ if (this.state.date === "") delete this.state.date;
42+ if (this.state.time === "") delete this.state.time;
43+
44+ axios.put(API_URL + 'meals/' + this.state.id + '/', this.state, {
45+ crossdomain: true,
46+ headers: { 'Authorization': 'Token ' + this.props.user.token }
47+ }).then((response) => {
48+ this.props.toggle();
49+ this.props.update();
50+ });
51+ };
52+
53+ defaultIfEmpty = value => {
54+ return value === "" ? "" : value;
55+ };
56+
57+ render() {
58+ return (
59+ <Form onSubmit={this.props.meal? this.edit: this.create}>
60+ <FormGroup>
61+ <Label for="text">Title:</Label>
62+ <Input
63+ type="text"
64+ name="text"
65+ onChange={this.onChange}
66+ value={this.defaultIfEmpty(this.state.text)}
67+ />
68+ </FormGroup>
69+ <FormGroup>
70+ <Label for="text">Calories:</Label>
71+ <Input
72+ type="text"
73+ name="calories"
74+ onChange={this.onChange}
75+ value={this.defaultIfEmpty(this.state.calories)}
76+ />
77+ </FormGroup>
78+ <FormGroup>
79+ <Label for="text">Date (empty for today):</Label>
80+ <Input
81+ type="date"
82+ name="date"
83+ onChange={this.onChange}
84+ value={this.defaultIfEmpty(this.state.date)}
85+ />
86+ </FormGroup>
87+ <FormGroup>
88+ <Label for="time">Time (empty for right now):</Label>
89+ <Input
90+ type="time"
91+ name="time"
92+ onChange={this.onChange}
93+ value={this.defaultIfEmpty(this.state.time)}
94+ />
95+ </FormGroup>
96+
97+ <Button className="float-right" color="primary">Save</Button>
98+ </Form>
99+ )
100+ }
101+}
102+
103+export default MealForm;
bmi_ui/src/components/MealModal.js+63−0
@@ -0,0 +1,63 @@
1+import React, { Component, Fragment } from "react";
2+import { Button, Modal, ModalHeader, ModalBody } from "reactstrap";
3+import MealForm from './MealForm';
4+
5+
6+class MealModal extends Component {
7+ constructor(props) {
8+ super(props);
9+
10+ this.state = {
11+ modal: false
12+ }
13+ }
14+
15+ toggle = (e) => {
16+ e && e.preventDefault();
17+
18+ this.setState(previous => ({
19+ modal: !previous.modal
20+ }));
21+ };
22+
23+ render() {
24+ const create = this.props.create;
25+
26+ var title = "Editing meal";
27+ var button = <Button onClick={this.toggle} size="sm" outline className="mr-3 float-right">Edit</Button>;
28+
29+ if (create) {
30+ title = 'Creating meal';
31+
32+ button = (
33+ <Button
34+ color='primary'
35+ size="sm"
36+ outline
37+ onClick={this.toggle}
38+ className="mr-3 float-right"
39+ >Add new meal</Button>
40+ );
41+ }
42+
43+ return (
44+ <Fragment>
45+ {button}
46+ <Modal isOpen={this.state.modal} toggle={this.toggle}>
47+ <ModalHeader toggle={this.toggle}>{title}</ModalHeader>
48+
49+ <ModalBody>
50+ <MealForm
51+ toggle={this.toggle}
52+ meal={this.props.meal}
53+ user={this.props.user}
54+ update={this.props.update}
55+ />
56+ </ModalBody>
57+ </Modal>
58+ </Fragment>
59+ );
60+ }
61+}
62+
63+export default MealModal;
bmi_ui/src/components/MealsTable.js+67−0
@@ -0,0 +1,67 @@
1+import React, { Component } from 'react';
2+import { Table, Button } from 'reactstrap';
3+import MealModal from './MealModal';
4+import axios from 'axios';
5+import { API_URL } from '../constants';
6+
7+
8+class MealsTable extends Component {
9+
10+ delete = (e, value) => {
11+ e && e.preventDefault();
12+
13+ const mealId = e.currentTarget.getAttribute('data-meal-id');
14+
15+ axios.delete(API_URL + 'meals/' + mealId + '/', {
16+ crossdomain: true,
17+ headers: { Authorization: 'Token ' + this.props.user.token }
18+ }).then((response) => {
19+ this.props.update()
20+ });
21+ };
22+
23+ render() {
24+ return (
25+ <Table>
26+ <thead>
27+ <tr>
28+ <th>#</th>
29+ <th>Title</th>
30+ <th>Calories</th>
31+ <th>Date</th>
32+ <th>Time</th>
33+ <th></th>
34+ </tr>
35+ </thead>
36+ <tbody>
37+ {this.props.meals.map((m, i) =>
38+ <tr key={i}>
39+ <th scope="row" className="pt-3">{i + 1}</th>
40+ <th className="pt-3">{m.text}</th>
41+ <th className="pt-3">{m.calories}</th>
42+ <th className="pt-3">{m.date}</th>
43+ <th className="pt-3">{m.time}</th>
44+ <th>
45+ <MealModal
46+ meal={m}
47+ user={this.props.user}
48+ update={this.props.update}
49+ />
50+ <Button
51+ color="danger"
52+ outline
53+ data-meal-id={m.id}
54+ onClick={this.delete.bind(this)}
55+ size="sm"
56+ className="mr-2 float-right"
57+ >Delete</Button>
58+ </th>
59+ </tr>
60+ )}
61+ </tbody>
62+ </Table>
63+ );
64+ }
65+}
66+
67+export default MealsTable;
bmi_ui/src/components/UserForm.js+23−18
@@ -5,20 +5,14 @@ import { API_URL } from '../constants';
55
66
77 class UserForm extends React.Component {
8- state = {
9- id: 0,
10- username: '',
11- preferences: { max_calories: 0 }
12- };
8+ constructor(props) {
9+ super(props);
1310
14- componentDidMount() {
15- if (this.props.user) {
16- this.setState({
17- id: this.props.user.id,
18- username: this.props.user.username,
19- preferences: this.props.user.preferences
20- });
21- }
11+ this.state ={
12+ id: this.props.user.id || '',
13+ username: this.props.user.username || '',
14+ preferences: this.props.user.preferences || { max_calories: 0 }
15+ };
2216 }
2317
2418 onChange = e => {
@@ -31,15 +25,26 @@ class UserForm extends React.Component {
3125
3226 createUser = e => {
3327 e.preventDefault();
28+
3429 axios.post(API_URL + 'users/', this.state, { crossdomain: true }).then(() => {
35- this.props.toggle();
30+ axios.post(API_URL + 'login/', this.state, { crossdomain: true }).then((res) => {
31+ this.props.toggle();
32+ this.props.update(res.data);
33+ });
3634 });
3735 };
3836
3937 editUser = e => {
4038 e.preventDefault();
41- axios.put(API_URL + 'users/' + this.state.id, this.state, { crossdomain: true }).then(() => {
39+
40+ axios.put(API_URL + 'users/' + this.state.id + '/', this.state, {
41+ crossdomain: true,
42+ headers: { 'Authorization': 'Token ' + this.props.user.token }
43+ }).then((response) => {
44+ const token = this.props.user.token;
45+ response.data['token'] = token;
4246 this.props.toggle();
47+ this.props.update(response.data);
4348 });
4449 };
4550
@@ -56,7 +61,7 @@ class UserForm extends React.Component {
5661 type="text"
5762 name="username"
5863 onChange={this.onChange}
59- value={this.defaultIfEmpty(this.state.name)}
64+ value={this.defaultIfEmpty(this.state.username)}
6065 />
6166 </FormGroup>
6267 <FormGroup>
@@ -65,7 +70,7 @@ class UserForm extends React.Component {
6570 type="password"
6671 name="password"
6772 onChange={this.onChange}
68- value={this.defaultIfEmpty(this.state.name)}
73+ value={this.defaultIfEmpty(this.state.password)}
6974 />
7075 </FormGroup>
7176 <FormGroup>
@@ -74,7 +79,7 @@ class UserForm extends React.Component {
7479 type="text"
7580 name="caloriesPerDay"
7681 onChange={this.onCaloriesChange}
77- value={this.defaultIfEmpty(this.state.name)}
82+ value={this.defaultIfEmpty(this.state.preferences.max_calories)}
7883 />
7984 </FormGroup>
8085
bmi_ui/src/components/UserModal.js+9−4
@@ -4,12 +4,16 @@ import UserForm from './UserForm';
44
55
66 class UserModal extends Component {
7- state = {
8- modal: false
7+ constructor(props) {
8+ super(props);
9+
10+ this.state = {
11+ modal: false
12+ }
913 }
1014
1115 toggle = (e) => {
12- e.preventDefault();
16+ e && e.preventDefault();
1317
1418 this.setState(previous => ({
1519 modal: !previous.modal
@@ -44,7 +48,8 @@ class UserModal extends Component {
4448 <ModalBody>
4549 <UserForm
4650 toggle={this.toggle}
47- user={this.props.student}
51+ user={this.props.user}
52+ update={this.props.update}
4853 />
4954 </ModalBody>
5055 </Modal>
db.sqlite3+0−0
Binary file, not shown.