@@ -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