-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExercisesList.jsx
73 lines (63 loc) · 2.31 KB
/
ExercisesList.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
import React, { Component } from 'react';
import { Link } from "react-router-dom";
import axios from "axios";
const Exercise = props => (
<tr>
<td>{props.exercise.username}</td>
<td>{props.exercise.description}</td>
<td>{props.exercise.duration}</td>
<td>{props.exercise.date.substring(0,10)}</td>
<td>
<button className="btn btn-secondary"><Link to={"/edit/"+props.exercise._id} style={{color:"white"}}>Edit</Link></button> | <button className="btn btn-danger" onClick={() => {props.deleteExercise(props.exercise._id) }}>Delete</button>
</td>
</tr>
)
class ExercisesList extends Component {
constructor(props) {
super(props);
this.state = {
exercises: []
}
this.deleteExercise = this.deleteExercise.bind(this);
}
componentDidMount() {
axios.get('http://localhost:5000/exercises/')
.then(res => {
this.setState({ exercises: res.data })
})
.catch(error => console.log(error));
}
deleteExercise(id) {
axios.delete('http://localhost:5000/exercises/' +id)
// .then(res => console.log(res.data));
.then(res => alert(res.data));
this.setState({ exercises: this.state.exercises.filter(el => el._id !== id)})
}
exercisesList() {
return this.state.exercises.map(currentexercise => {
return <Exercise exercise={currentexercise} deleteExercise={this.deleteExercise} key={currentexercise._id} />
})
}
render() {
return (
<div className="container">
<h3>Logged Exercises</h3>
<table className="table">
<thead className="thead-light">
<tr>
<th>Username</th>
<th>Description</th>
<th>Duration</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{this.exercisesList()}
</tbody>
</table>
</div>
);
}
}
export default ExercisesList;