Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import "./App.css";
  3. import axios from "axios";
  4. import Button from "react-bootstrap/Button";
  5. import DropdownButton from "react-bootstrap/DropdownButton";
  6. import Dropdown from "react-bootstrap/Dropdown";
  7. import FormControl from "react-bootstrap/FormControl";
  8. import InputGroup from "react-bootstrap/InputGroup";
  9.  
  10. axios.defaults.withCredentials = true;
  11.  
  12. class App extends Component {
  13. constructor(props) {
  14. super();
  15. this.toggle = this.toggle.bind(this);
  16. this.select = this.select.bind(this);
  17. this.fetchData();
  18. }
  19.  
  20. state = {
  21. test: [],
  22. from: 0,
  23. size: 20,
  24. namePrefix: "",
  25. sizeOfAll: 0,
  26. numberOfPages: 1,
  27. offset: 0,
  28. currentPage: 1,
  29. dropdownOpen: false,
  30. valueOfDropdown: "20"
  31. };
  32.  
  33. componentDidMount() {
  34. this.fetchData();
  35. }
  36.  
  37. nextPage = () => {
  38. const nextPageNumber = this.state.from + this.state.size;
  39. //rozwiazanie problemu asynchronicznosci przez callback
  40. this.setState(
  41. {
  42. from: nextPageNumber
  43. },
  44. () => {
  45. this.fetchData();
  46. }
  47. );
  48. };
  49.  
  50. lastPage = () => {
  51. const lastPageNumber =
  52. this.state.numberOfPages * this.state.size - this.state.size;
  53. //rozwiazanie problemu asynchronicznosci przez callback
  54. this.setState(
  55. {
  56. from: lastPageNumber
  57. },
  58. () => {
  59. this.fetchData();
  60. }
  61. );
  62. };
  63. firstPage = () => {
  64. const firstPageNumber = 0;
  65. //rozwiazanie problemu asynchronicznosci przez callback
  66. this.setState(
  67. {
  68. from: firstPageNumber
  69. },
  70. () => {
  71. this.fetchData();
  72. }
  73. );
  74. };
  75.  
  76. prevPage = () => {
  77. if (this.state.from !== 0) {
  78. const prevPageNumber = this.state.from - this.state.size;
  79. this.setState(
  80. {
  81. from: prevPageNumber
  82. },
  83. () => {
  84. this.fetchData();
  85. }
  86. );
  87. }
  88. };
  89.  
  90. numberOfRecords = nrRec => {
  91. this.setState(
  92. {
  93. size: nrRec
  94. },
  95. () => {
  96. this.fetchData();
  97. }
  98. );
  99. };
  100.  
  101. inputSearch = e => {
  102. const valueOfInput = e.target.value;
  103. this.setState(
  104. {
  105. from: 0,
  106. namePrefix: valueOfInput
  107. },
  108. () => {
  109. this.fetchData();
  110. }
  111. );
  112. };
  113.  
  114. async fetchData() {
  115. let data = await axios
  116. .create({ withCredentials: true })
  117. .get("http://10.132.15.238:8777/console/properties/?", {
  118. params: {
  119. from: this.state.from,
  120. size: this.state.size,
  121. namePrefix: this.state.namePrefix
  122. },
  123. headers: { Authorization: "Basic YTph" }
  124. });
  125. this.setState({
  126. test: data.data.properties,
  127. sizeOfAll: data.data.size,
  128. currentPage: Math.ceil(this.state.from / this.state.size) + 1,
  129. numberOfPages: Math.ceil(this.state.sizeOfAll / this.state.size)
  130. });
  131. }
  132.  
  133. handlePageClick = data => {
  134. let selected = data.selected;
  135. let offset = Math.ceil(selected * this.state.size);
  136.  
  137. this.setState({ offset: offset }, () => {
  138. this.fetchData();
  139. });
  140. };
  141.  
  142. toggle() {
  143. this.setState({
  144. dropdownOpen: !this.state.dropdownOpen
  145. });
  146. }
  147.  
  148. select(event) {
  149. this.setState(
  150. {
  151. dropdownOpen: !this.state.dropdownOpen,
  152. valueOfDropdown: event.target.innerText,
  153. size: event.target.innerText
  154. },
  155. () => {
  156. this.fetchData();
  157. }
  158. );
  159. }
  160.  
  161. render() {
  162. return (
  163. <div className="App">
  164. <div className="PrevNextNr">
  165. <Button onClick={this.firstPage} className="btn" variant="primary">
  166. First
  167. </Button>
  168. <Button onClick={this.prevPage} className="btn" variant="primary">
  169. Previous
  170. </Button>
  171. <Button onClick={this.nextPage} className="btn" variant="primary">
  172. Next
  173. </Button>
  174. <Button onClick={this.lastPage} className="btn" variant="primary">
  175. Last
  176. </Button>
  177. <h3>
  178. {this.state.currentPage + " out of " + this.state.numberOfPages}
  179. </h3>
  180. <DropdownButton
  181. isOpen={this.state.dropdownOpen}
  182. toggle={this.toggle}
  183. id="dropdown-basic-button"
  184. title={"Nr of records: " + this.state.valueOfDropdown}
  185. >
  186. <Dropdown.Item onClick={this.select}>10</Dropdown.Item>
  187. <Dropdown.Item onClick={this.select}>20</Dropdown.Item>
  188. <Dropdown.Item onClick={this.select}>50</Dropdown.Item>
  189. <Dropdown.Item onClick={this.select}>100</Dropdown.Item>
  190. </DropdownButton>
  191. </div>
  192. <div></div>
  193. <InputGroup onChange={e => this.inputSearch(e)} className="mb-3">
  194. <InputGroup.Prepend>
  195. <InputGroup.Text id="basic-addon1">Search:</InputGroup.Text>
  196. </InputGroup.Prepend>
  197. <FormControl
  198. placeholder="What are you looking for?"
  199. aria-label="Username"
  200. aria-describedby="basic-addon1"
  201. />
  202. <InputGroup.Append>
  203. <InputGroup.Text id="basic-addon2">
  204. Found: {this.state.sizeOfAll}
  205. </InputGroup.Text>
  206. </InputGroup.Append>
  207. </InputGroup>
  208. <table>
  209. <tbody>
  210. <tr>
  211. <td>property name</td>
  212. <td>property value</td>
  213. </tr>
  214. {this.state.test.map((item, index) => (
  215. <tr key={index}>
  216. <td>{item.property_name}</td>
  217. <td>{item.property_value}</td>
  218. </tr>
  219. ))}
  220. </tbody>
  221. </table>
  222. </div>
  223. );
  224. }
  225. }
  226.  
  227. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement