Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import Axios from "axios";
  3.  
  4.  
  5. class ListCoursesComponent extends Component {
  6. constructor(props) {
  7. super(props)
  8. this.state = {
  9. courses: [],
  10. message: null
  11. }
  12. this.refreshCourses = this.refreshCourses.bind(this);
  13. this.deleteCourseClicked = this.deleteCourseClicked.bind(this);
  14. this.retrieveCoursesFromAPI = this.retrieveCoursesFromAPI.bind(this);
  15. this.deleteCourse = this.deleteCourse.bind(this);
  16. }
  17. componentDidUpdate() {
  18. console.log(this.state.courses);
  19. }
  20. componentDidMount() {
  21. this.refreshCourses();
  22. }
  23. retrieveCoursesFromAPI(){
  24. return Axios.get(`http://localhost:8080/courses`);
  25. }
  26. deleteCourse(id){
  27. return Axios.delete(`http://localhost:8080/courses/${id}`);
  28. }
  29. refreshCourses() {
  30. retrieveCoursesFromAPI()
  31. .then(
  32. response => {
  33. this.setState({ courses: response.data })
  34. }
  35. )
  36. }
  37. deleteCourseClicked(id){
  38. deleteCourse(id)
  39. .then(
  40. () => {
  41. this.refreshCourses()
  42. }
  43. )
  44. }
  45. render() {
  46. return (
  47. <div className="container">
  48. <table className="table">
  49. <thead>
  50. <tr>
  51. <th>Id</th>
  52. <th>Description</th>
  53. <th>Delete</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. {
  58. this.state.courses.map(
  59. course =>
  60. <tr key={course.id}>
  61. <td>{course.id}</td>
  62. <td>{course.description}</td>
  63. <td><button className="btn btn-warning" onClick={() => this.deleteCourseClicked(course.id)}>Delete</button></td>
  64. </tr>
  65. )
  66. }
  67. </tbody>
  68. </table>
  69. </div>
  70. )
  71. }
  72. }
  73.  
  74. export default ListCoursesComponent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement