Advertisement
Guest User

Untitled

a guest
Dec 21st, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import React, {Component, SyntheticEvent} from "react";
  2. import Wrapper from "../components/Wrapper";
  3. import axios from "axios";
  4. import {User} from "../../classes/user";
  5. import {Link} from "react-router-dom";
  6.  
  7.  
  8.  
  9. class Users extends Component {
  10. state = {
  11. users: [],
  12. //page: 1
  13. }
  14.  
  15. page = 1
  16. last_page = 0
  17.  
  18. componentDidMount = async () => {
  19. const response = await axios.get(`users?page=${this.page}`)
  20.  
  21. console.log(response)
  22. this.setState({
  23. users: response.data.data
  24. })
  25.  
  26. this.last_page = response.data.meta.last_page
  27. }
  28.  
  29. prev = async () => {
  30. //e.preventDefault()
  31. if (this.page === 1) return
  32. this.page--;
  33.  
  34.  
  35.  
  36. await this.componentDidMount();
  37. }
  38.  
  39. next = async (e:SyntheticEvent ) => {
  40. e.preventDefault()
  41. if (this.page === this.last_page) return
  42. this.page++;
  43.  
  44. await this.componentDidMount();
  45. }
  46.  
  47. delete = async (id: number) => {
  48.  
  49. if (window.confirm('Are you sure you want to delete this user?')) {
  50.  
  51. await axios.delete(`users/${id}`);
  52.  
  53.  
  54. this.setState({
  55. users: this.state.users.filter((u:User) => u.id !== id)
  56. })
  57. }
  58. }
  59.  
  60. render() {
  61.  
  62.  
  63. return (
  64. <Wrapper>
  65. <div className="d-flex justify-content-between flex-wrap flex-md-no-wrap align-items-center pt-3 pb-2 mb-3 border-bottom">
  66. <div className="btn-toolbar mb-2 mb-md-0">
  67. <Link to={'users/create'} className="btn btn-sm btn-outline-secondary">Add</Link>
  68. </div>
  69. </div>
  70.  
  71. <div className="table-responsive">
  72. <table className="table table-striped table-sm">
  73. <thead>
  74. <tr>
  75. <th scope="col">#</th>
  76. <th scope="col">Name</th>
  77. <th scope="col">Email</th>
  78. <th scope="col">Role</th>
  79. <th scope="col">Action</th>
  80. </tr>
  81. </thead>
  82. <tbody>
  83. {this.state.users.map(
  84. (user: User) => {
  85.  
  86. return (
  87. <tr>
  88. <td>{user.id}</td>
  89. <td>{user.first_name} {user.last_name}</td>
  90. <td>{user.email}</td>
  91. <td>{user.role.name}</td>
  92. <td>
  93. <div className="btn-group mr-2">
  94. <a href="" className="btn btn-sm btn-outline-secondary">Edit</a>
  95. <a href="" className="btn btn-sm btn-outline-secondary"
  96. onClick={() =>this.delete(user.id)}>Delete</a>
  97. </div>
  98. </td>
  99. </tr>
  100. )
  101.  
  102. }
  103. )}
  104.  
  105.  
  106. </tbody>
  107. </table>
  108. </div>
  109.  
  110. <nav>
  111. <ul className="pagination">
  112. <li className="page-item">
  113. <a href="" className="page-link" onClick={this.prev}>Previous</a>
  114. </li>
  115. <li className="page-item">
  116. <a href="" className="page-link" onClick={this.next}>Next</a>
  117. </li>
  118.  
  119. </ul>
  120. </nav>
  121. </Wrapper>
  122. )
  123. }
  124. }
  125.  
  126.  
  127. export default Users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement