Last active
May 22, 2019 17:21
-
-
Save beaucarnes/f0c161a4945e0695a863baf3cae830f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import DatePicker from 'react-datepicker'; | |
| import "react-datepicker/dist/react-datepicker.css"; | |
| import axios from 'axios'; | |
| export default class EditExercise extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.onChangeUsername = this.onChangeUsername.bind(this); | |
| this.onChangeDescription = this.onChangeDescription.bind(this); | |
| this.onChangeDuration = this.onChangeDuration.bind(this); | |
| this.onChangeDate = this.onChangeDate.bind(this); | |
| this.onSubmit = this.onSubmit.bind(this); | |
| this.state = { | |
| username: '', | |
| description: '', | |
| duration: 0, | |
| date: new Date(), | |
| users: [] | |
| } | |
| } | |
| componentDidMount() { | |
| axios.get('http://localhost:5000/exercises/'+this.props.match.params.id) | |
| .then(response => { | |
| this.setState({ | |
| username: response.data.username, | |
| description: response.data.description, | |
| duration: response.data.duration, | |
| date: new Date(response.data.date) | |
| }) | |
| }) | |
| .catch(function (error) { | |
| console.log(error); | |
| }) | |
| axios.get('http://localhost:5000/users/') | |
| .then(response => { | |
| this.setState({ users: response.data.map(user => user.username) }); | |
| }) | |
| .catch((error) => { | |
| console.log(error); | |
| }) | |
| } | |
| onChangeUsername(e) { | |
| this.setState({ | |
| username: e.target.value | |
| }); | |
| } | |
| onChangeDescription(e) { | |
| this.setState({ | |
| description: e.target.value | |
| }); | |
| } | |
| onChangeDuration(e) { | |
| this.setState({ | |
| duration: e.target.value | |
| }); | |
| } | |
| onChangeDate(date) { | |
| this.setState({ | |
| date: date | |
| }); | |
| } | |
| onSubmit(e) { | |
| e.preventDefault(); | |
| const exercise = { | |
| username: this.state.username, | |
| description: this.state.description, | |
| duration: this.state.duration, | |
| date: this.state.date, | |
| }; | |
| console.log(exercise); | |
| axios.post('http://localhost:5000/exercises/update/'+this.props.match.params.id, exercise) | |
| .then(res => console.log(res.data)); | |
| window.location = '/'; | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| <h3>Edit Exercise Log</h3> | |
| <form onSubmit={this.onSubmit}> | |
| <div className="form-group"> | |
| <label>Username: </label> | |
| <select ref="userInput" | |
| className="form-control" | |
| value={this.state.username} | |
| onChange={this.onChangeUsername}> | |
| { | |
| this.state.users.map(function(user) { | |
| return <option | |
| key={user} | |
| value={user}>{user} | |
| </option>; | |
| }) | |
| } | |
| </select> | |
| </div> | |
| <div className="form-group"> | |
| <label>Description: </label> | |
| <input type="text" | |
| required | |
| className="form-control" | |
| value={this.state.description} | |
| onChange={this.onChangeDescription} | |
| /> | |
| </div> | |
| <div className="form-group"> | |
| <label>Duration (in minutes): </label> | |
| <input | |
| type="text" | |
| className="form-control" | |
| value={this.state.duration} | |
| onChange={this.onChangeDuration} | |
| /> | |
| </div> | |
| <div className="form-group"> | |
| <label>Date: </label> | |
| <DatePicker | |
| selected={this.state.date} | |
| onChange={this.onChangeDate} | |
| /> | |
| </div> | |
| <div className="form-group"> | |
| <input type="submit" value="Edit Exercise Log" className="btn btn-primary" /> | |
| </div> | |
| </form> | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment