Guest User

Untitled

a guest
Jan 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import $ from "jquery";
  3. import UserStore from "../../stores/UserStore";
  4. import * as UserActions from "../../actions/UserActions";
  5. import AddUser from "./AddUser";
  6.  
  7. $.DataTable = require("datatables.net");
  8.  
  9. class UserList extends Component {
  10. constructor() {
  11. super();
  12. this.getUsers = this.getUsers.bind(this);
  13. this.state = {
  14. users: UserStore.getAll()
  15. };
  16. this.loadUsers();
  17. }
  18.  
  19. componentDidMount() {
  20. $(document).ready(function() {
  21. $("#example").DataTable({
  22. ordering: true
  23. });
  24. });
  25. }
  26.  
  27. componentWillMount() {
  28. UserStore.on("change", this.getUsers);
  29. }
  30.  
  31. componentWillUnmount() {
  32. UserStore.removeListener("change", this.getUsers);
  33. }
  34.  
  35. getUsers() {
  36. console.log(" get users called");
  37. this.setState({
  38. users: UserStore.getAll()
  39. });
  40. }
  41.  
  42. loadUsers() {
  43. UserActions.getUsersList();
  44. }
  45.  
  46. render() {
  47. const userlistitem = this.state.users.map((user, index) => (
  48. <tr key={index}>
  49. <th scope="row">{index}</th>
  50. <td>{user.name}</td>
  51. <td>{user.username}</td>
  52. <td>{user.email}</td>
  53. <td>{user.dob}</td>
  54. <td>{user.address}</td>
  55. <td>{user.mobile}</td>
  56. <td>{user.branch}</td>
  57. </tr>
  58. ));
  59. return (
  60. <div
  61. style={{
  62. marginTop: 80,
  63. marginLeft: 150,
  64. marginRight: 150
  65. }}
  66. >
  67. <div className="card text-white bg-info mb-3">
  68. <div className="card-body">
  69. <div className="d-flex justify-content-between">
  70. <h5>User List</h5>
  71. <div>
  72. <button
  73. style={{
  74. marginTop: 10
  75. }}
  76. type="button"
  77. className="btn btn-light "
  78. data-toggle="modal"
  79. data-target="#exampleModalCenter"
  80. >
  81. Add New User
  82. </button>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87.  
  88. <table id="example" className="table table-bordered table-striped ">
  89. <thead className="thead-dark">
  90. <tr>
  91. <th scope="col">#</th>
  92. <th scope="col">Name</th>
  93. <th scope="col">User Name</th>
  94. <th scope="col">Email</th>
  95. <th scope="col">DOB</th>
  96. <th scope="col">Address</th>
  97. <th scope="col">Mobile</th>
  98. <th scope="col">Branch</th>
  99. </tr>
  100. </thead>
  101. <tbody>{userlistitem}</tbody>
  102. </table>
  103. <AddUser />
  104. </div>
  105. );
  106. }
  107. }
  108.  
  109. export default UserList;
  110.  
  111. import React, { Component } from "react";
  112. import DatePicker from "react-datepicker";
  113. import "react-datepicker/dist/react-datepicker.css";
  114. import "../../css/datepicker.css";
  115. import * as UserActions from "../../actions/UserActions";
  116.  
  117. class AddUser extends Component {
  118. constructor(props) {
  119. super(props);
  120. this.state = {
  121. branch: "",
  122. name: "",
  123. username: "",
  124. mobile: "",
  125. email: "",
  126. address: "",
  127. dob: new Date()
  128. };
  129. this.handleInputChange = this.handleInputChange.bind(this);
  130. this.handledatepickerchange = this.handledatepickerchange.bind(this);
  131. }
  132.  
  133. handleInputChange(event) {
  134. const target = event.target;
  135. const value = target.value;
  136. const name = target.name;
  137. this.setState({ [name]: value });
  138. }
  139. handledatepickerchange(event) {
  140. this.setState({ dob: event });
  141. }
  142.  
  143. createUser = () => {
  144. console.log("this is:", this);
  145. const user = {
  146. branch: this.state.branch,
  147. name: this.state.name,
  148. username: this.state.username,
  149. mobile: this.state.mobile,
  150. email: this.state.email,
  151. address: this.state.address,
  152. dob: this.state.dob
  153. };
  154. UserActions.createNewUser(user);
  155. this.setState({
  156. branch: "",
  157. name: "",
  158. username: "",
  159. mobile: "",
  160. email: "",
  161. address: "",
  162. dob: new Date()
  163. });
  164. };
  165.  
  166. render() {
  167. return (
  168. <div
  169. className="modal fade"
  170. id="exampleModalCenter"
  171. tabIndex="-1"
  172. role="dialog"
  173. aria-labelledby="exampleModalCenterTitle"
  174. aria-hidden="true"
  175. >
  176. <div className="modal-dialog modal-dialog-centered" role="document">
  177. <div className="modal-content">
  178. <div className="modal-header">
  179. <h5 className="modal-title" id="exampleModalCenterTitle">
  180. Add New User
  181. </h5>
  182. <button
  183. type="button"
  184. className="close"
  185. data-dismiss="modal"
  186. aria-label="Close"
  187. >
  188. <span aria-hidden="true">&times;</span>
  189. </button>
  190. </div>
  191. <div className="modal-body">
  192. <div className="input-group mb-3">
  193. <input
  194. name="name"
  195. type="text"
  196. className="form-control"
  197. placeholder="Name"
  198. aria-label="Name"
  199. aria-describedby="button-addon2"
  200. value={this.state.name}
  201. onChange={this.handleInputChange}
  202. />
  203. </div>
  204. <div className="input-group mb-3">
  205. <input
  206. name="username"
  207. type="text"
  208. className="form-control"
  209. placeholder="User Name"
  210. aria-label="User Name"
  211. aria-describedby="button-addon2"
  212. value={this.state.username}
  213. onChange={this.handleInputChange}
  214. />
  215. </div>
  216. <div className="input-group mb-3">
  217. <input
  218. name="email"
  219. type="email"
  220. className="form-control"
  221. placeholder="Email"
  222. aria-label="Email"
  223. aria-describedby="button-addon2"
  224. value={this.state.email}
  225. onChange={this.handleInputChange}
  226. />
  227. </div>
  228. <div className="input-group mb-3">
  229. <input
  230. name="address"
  231. type="text"
  232. className="form-control"
  233. placeholder="Address"
  234. aria-label="Address"
  235. aria-describedby="button-addon2"
  236. value={this.state.address}
  237. onChange={this.handleInputChange}
  238. />
  239. </div>
  240. <div className="input-group mb-3">
  241. <input
  242. name="branch"
  243. type="text"
  244. className="form-control"
  245. placeholder="Branch"
  246. aria-label="Branch"
  247. aria-describedby="button-addon2"
  248. value={this.state.branch}
  249. onChange={this.handleInputChange}
  250. />
  251. </div>
  252. <div className="input-group mb-3">
  253. <DatePicker
  254. name="dob"
  255. type="text"
  256. className="form-control"
  257. placeholder="DOB"
  258. aria-label="DOB"
  259. aria-describedby="button-addon2"
  260. selected={this.state.dob}
  261. onChange={this.handledatepickerchange}
  262. />
  263. <p
  264. style={{
  265. marginTop: "5px",
  266. fontWeight: "200",
  267. marginLeft: "10px"
  268. }}
  269. >
  270. Date of Birth(dd/mm/yyyy)
  271. </p>
  272. </div>
  273. <div className="input-group mb-3">
  274. <input
  275. name="mobile"
  276. type="number"
  277. className="form-control"
  278. placeholder="Mobile No."
  279. aria-label="Mobile No."
  280. aria-describedby="button-addon2"
  281. value={this.state.mobile}
  282. onChange={this.handleInputChange}
  283. />
  284. </div>
  285. </div>
  286.  
  287. <div className="modal-footer">
  288. <br />
  289. <br />
  290. <button
  291. type="button"
  292. className="btn btn-secondary"
  293. data-dismiss="modal"
  294. >
  295. Close
  296. </button>
  297. <button
  298. type="button"
  299. className="btn btn-primary"
  300. data-dismiss="modal"
  301. onClick={this.createUser}
  302. >
  303. Save
  304. </button>
  305. </div>
  306. </div>
  307. </div>
  308. </div>
  309. );
  310. }
  311. }
  312.  
  313. export default AddUser;
  314.  
  315. import dispatcher from "../dispatcher/dispatcher";
  316. import { BASE_URL } from "../utils/AppConstants";
  317.  
  318. export function getUsersList() {
  319. console.log("getting the data! ");
  320. fetch(BASE_URL + "/users")
  321. .then(res => res.json())
  322. .then(
  323. result => {
  324. console.log("res " + result);
  325. dispatcher.dispatch({ type: "RECEIVE_USERS", users: result });
  326. },
  327. // Note: it's important to handle errors here instead of a catch() block so that
  328. // we don't swallow exceptions from actual bugs in components.
  329. error => {
  330. // here manage error and close loading;
  331. console.log("getting error " + error);
  332. }
  333. );
  334. }
  335.  
  336. export function createNewUser(user) {
  337. console.log("post the data!");
  338. fetch(BASE_URL + "/saveuser", {
  339. method: "POST",
  340. headers: {
  341. Accept: "application/json",
  342. "Content-Type": "application/json"
  343. },
  344. body: JSON.stringify(user)
  345. })
  346. .then(res => res.json())
  347. .then(
  348. result => {
  349. dispatcher.dispatch({ type: "CREATE_USER", newUser: user });
  350. },
  351. // Note: it's important to handle errors here instead of a catch() block so that
  352. // we don't swallow exceptions from actual bugs in components.
  353. error => {
  354. // here manage error and close loading;
  355. console.log("getting error " + error);
  356. }
  357. );
  358. }
  359.  
  360. import { EventEmitter } from "events";
  361. import dispatcher from "../dispatcher/dispatcher";
  362.  
  363. class UserStore extends EventEmitter {
  364. constructor() {
  365. super();
  366. dispatcher.register(this.handleActions.bind(this));
  367. this.users = [
  368. {
  369. branch: "19",
  370. name: "Javcbvcsim11",
  371. username: "zxcv",
  372. mobile: "5645654",
  373. email: "demo@gmail.com111",
  374. address: "Demo vcbvcbAddress1",
  375. dob: "2020-11-06T00:00:00.000+0000"
  376. }
  377. ];
  378. }
  379.  
  380. createUser(newUser) {
  381. this.users.push(newUser);
  382. console.log("new users lenght " + this.users.lenght);
  383. this.emit("change");
  384. }
  385.  
  386. getAll() {
  387. return this.users;
  388. }
  389. handleActions(action) {
  390. switch (action.type) {
  391. case "RECEIVE_USERS": {
  392. this.users = action.users;
  393. this.emit("change");
  394. break;
  395. }
  396. case "CREATE_USER": {
  397. this.createUser(action.newUser);
  398. break;
  399. }
  400. }
  401. }
  402. }
  403.  
  404. export default new UserStore();
Add Comment
Please, Sign In to add comment