Guest User

Untitled

a guest
Apr 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // tag::vars[]
  4. const React = require('react');
  5. const ReactDOM = require('react-dom');
  6. const client = require('./client');
  7. // end::vars[]
  8.  
  9. // tag::app[]
  10. class App extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {books: []};
  14. }
  15.  
  16. componentDidMount() {
  17. client({method: 'GET', path: 'api'}).done(response => {
  18. console.info("componentDidMount response: %o", response);
  19. this.setState({books: response.entity});
  20. });
  21. }
  22.  
  23. handleDeleteElement = id => {
  24. console.log("id:", id);
  25. // "id: 1", but http://localhost:8080/main/book/[object%20Object]/delete
  26. client({method: 'POST', path: 'http://localhost:8080/main/book/1/delete'}).done(response => {
  27. client({method: 'GET', path: 'api'}).done(response => {
  28. console.info("handleDeleteElement response: %o", response);
  29. this.setState({books: response.entity});
  30. });
  31. });
  32. };
  33.  
  34. render() {
  35. return (
  36. <BookList books={this.state.books}/>
  37. )
  38. }
  39. }
  40. // end::app[]
  41.  
  42. // tag::book-list[]
  43. class BookList extends React.Component{
  44. render() {
  45. var books = this.props.books.map(book =>
  46. <Book key={book.id} book={book}/>
  47. );
  48. return (
  49. <table id="example" className="display nowrap" style={{width: '100%'}}>
  50. <tbody>
  51. <tr>
  52. <th>Название</th>
  53. <th>Автор</th>
  54. <th>Издательство</th>
  55. <th>Год издания</th>
  56. </tr>
  57. {books}
  58. </tbody>
  59. </table>
  60. )
  61. }
  62. }
  63. // end::book-list[]
  64.  
  65. // tag::book[]
  66. class Book extends React.Component{
  67. render() {
  68. const { id, name, author, publishing_house, the_year_of_publishing } = this.props.book;
  69.  
  70. return (
  71. <tr>
  72. <td><a href={"http://localhost:8080/main/book/book?id=" + id}>{name}</a></td>
  73. <td onClick={() => { App.handleDeleteElement(id) }}>{author}</td>
  74. <td>{publishing_house}</td>
  75. <td>{the_year_of_publishing}</td>
  76. </tr>
  77. )
  78. }
  79. }
  80. // end::book[]
  81.  
  82. // tag::render[]
  83. ReactDOM.render(
  84. <App />,
  85. document.getElementById('react')
  86. )
  87. // end::render[]
Add Comment
Please, Sign In to add comment