Advertisement
Guest User

MainApp.js

a guest
Jan 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import Table from "./Table";
  3. import PopUP from "./PopUP";
  4. import Pagination from "react-js-pagination";
  5. import Clienteditorwindow from "./Clienteditorwindow";
  6.  
  7. export const URL = 'https://baloofeathers.herokuapp.com/gpwebsite/';
  8.  
  9. class MainApp extends Component {
  10. constructor(props, context) {
  11. super(props, context);
  12. this.state = {
  13. showPopup: false,
  14. canEnter: (localStorage.getItem('loggedIn') || '') === 'yes',
  15. currPopup: null,
  16. showClientEditor: false,
  17. currEditor: null,
  18. currHandle: '-1',
  19. currNumOfClients: this.getFromLocalStorage(),
  20. currTextSearch: '',
  21. activePage: 1,
  22. total: null,
  23. password: '',
  24. userName: ''
  25. };
  26. this.requestId = 0;
  27. this.buildURL();
  28. }
  29.  
  30. addClientEditor = (item) => () => {
  31. this.setState({showClientEditor: true, currEditor: item});
  32. };
  33. removeClientEditor = () => {
  34. console.log(this.state.data);
  35. this.setState({showClientEditor: false, currEditor: null});
  36. };
  37. putInLocalStorage = (value) => {
  38. this.setState({currNumOfClients: value, activePage: 1}, () => {
  39. this.buildURL();
  40.  
  41. });
  42. localStorage.setItem('clientNumber', value);
  43. };
  44. getFromLocalStorage = () => {
  45.  
  46. return localStorage.getItem('clientNumber') || 10;
  47. };
  48. filterNumOfClients = (handle) => {
  49. console.log(handle);
  50. this.putInLocalStorage(handle);
  51. };
  52. changePages = (handle) => {
  53. this.setState({activePage: handle}, () => {
  54. this.buildURL();
  55. });
  56. };
  57. filtersearch = (handle) => {
  58.  
  59. this.setState({currTextSearch: handle, activePage: 1}, () => {
  60. this.buildURL();
  61. });
  62. };
  63. filterClients = (handle) => {
  64. this.setState({currHandle: handle, activePage: 1}, () => {
  65. this.buildURL();
  66. });
  67. };
  68. buildURL = () => {
  69. let url = URL + '?';
  70. const {currHandle, currNumOfClients, currTextSearch, activePage} = this.state;
  71. if (currHandle === '0' || currHandle === '1')
  72. url += "handled=" + currHandle;
  73. url += "&$limit=" + currNumOfClients;
  74. if (currTextSearch !== '')
  75. url += "&text=" + currTextSearch;
  76. url += "&$skip=" + ((activePage - 1) * (currNumOfClients));
  77. this.loadURL(url);
  78. };
  79.  
  80. loadURL = (url) => {
  81. const current = this.requestId;
  82. fetch(url)
  83. .then(response => response.json())
  84. .then(data => {
  85. if (current === this.requestId)
  86. this.setState({data})
  87. });
  88. };
  89. onLogin = () => {
  90. if (this.state.userName === "GreenPoint" && this.state.password === "ron3428") {
  91. this.setState({canEnter: true});
  92. localStorage.setItem('loggedIn', 'yes');
  93. }
  94. };
  95.  
  96. clicked = (item) => {
  97. this.setState({showPopup: true, currPopup: item})
  98. };
  99. closePopUp = (item) => {
  100. this.setState({showPopup: false, currPopup: null})
  101. };
  102.  
  103. onChangeInput = (e) => {
  104. if (e.target.id === "inputUserName") {
  105. this.setState({userName: e.target.value});
  106. } else {
  107. this.setState({password: e.target.value});
  108. }
  109. };
  110. onFilterChange = (type) => (e) => {
  111. if (type === 0) {
  112. this.filterClients(e.target.value);
  113. } else if (type === 1) {
  114. this.filterNumOfClients(e.target.value);
  115. } else {
  116. this.filtersearch(e.target.value);
  117. }
  118. };
  119.  
  120. render() {
  121. return (
  122.  
  123.  
  124. !this.state.canEnter ? <div id={"normal"}>
  125. <h1 id={"titleStart"}>התחברות</h1>
  126. <div id={"userNameConteiner"}>
  127. <p id={"startUserName"}> שם משתמש</p>
  128. <div id={"empty"}></div>
  129. <input type={"text"} id={"inputUserName"} onChange={this.onChangeInput}/>
  130. </div>
  131. <br/>
  132. <div id={"passwordContainer"}>
  133. <p id={"startPassword"}> סיסמא</p>
  134. <div id={"empty"}></div>
  135. <input type={"text"} id={"inputPassword"} onChange={this.onChangeInput}/>
  136. </div>
  137. <br/>
  138.  
  139. <button id={"submitStart"} onClick={this.onLogin}>התחברות</button>
  140.  
  141. </div> :
  142. <div style={{
  143. overflowY: 'auto'
  144. }}>
  145. {
  146.  
  147. this.state.data ?
  148. <div><p id={"clientsTotal"}> מספר לקוחות:{this.state.data.total}</p>
  149. <Table data={this.state.data}
  150. clickedfunc={this.clicked}
  151. onChangeFilter={this.onFilterChange
  152. } editClient={this.addClientEditor}
  153. value={this.getFromLocalStorage()}/>
  154. <Pagination activePage={this.state.activePage}
  155. itemsCountPerPage={this.state.currNumOfClients}
  156. totalItemsCount={this.state.data.total}
  157. pageRangeDisplayed={5}
  158. onChange={this.changePages}/>
  159. </div> : <p>loading</p>
  160. }
  161. {this.state.showPopup &&
  162. <PopUP deatils={this.state.currPopup} closeMe={this.closePopUp}/>}
  163. {this.state.showClientEditor &&
  164. <Clienteditorwindow removeEditorClient={this.removeClientEditor}
  165. deatils={this.state.currEditor}/>}
  166. </div>
  167.  
  168.  
  169. )
  170.  
  171.  
  172. }
  173. }
  174.  
  175. export default MainApp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement