Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. import React from 'react'
  2. import {BrowserRouter, Link, Route, Switch} from 'react-router-dom'
  3. import ManageProduct from './ManageProduct.jsx';
  4. import Products from './Products.jsx';
  5. import ProductType from './ProductType.jsx';
  6. import ManageProductType from './ManageProductType.jsx';
  7.  
  8. const url = 'http://13.251.156.195:8080/products'
  9. export default class Student extends React.Component {
  10.  
  11. constructor(props) {
  12. super(props)
  13. this.state = {
  14. students: [],
  15. id: '',
  16. name: '',
  17. addNew: true,
  18. showGrid: true
  19. }
  20.  
  21.  
  22. }
  23.  
  24. fetchData() {
  25.  
  26. fetch(url)
  27. .then(res => res.json())
  28. .then(json => this.setState({ students: json }))
  29. }
  30.  
  31. componentDidMount() {
  32. this.fetchData()
  33. }
  34.  
  35. handleChange(e) {
  36. var obj = {}
  37. obj[e.target.name] = e.target.value
  38. this.setState(obj)
  39. }
  40.  
  41. save() {
  42. if(this.state.addNew === true){
  43. fetch(url, {
  44. method: 'post',
  45. headers: {
  46. 'Content-Type': 'application/json',
  47. 'Accept': 'application/json'
  48. },
  49. body: JSON.stringify({ id: this.state.id, name: this.state.name })
  50. }).then(res => res.json())
  51. .then(json => this.fetchData())
  52. }
  53. else{
  54. fetch(url, {
  55. method: 'put',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. 'Accept': 'application/json'
  59. },
  60. body: JSON.stringify({ id: this.state.id, name: this.state.name })
  61. }).then(res => res.json())
  62. .then(json => this.fetchData())
  63. }
  64.  
  65. }
  66.  
  67. delete(id){
  68. if(confirm('Do you want to delete?')){
  69. fetch(url + "/"+id, {
  70. method: 'delete',
  71. }).then(res=>res.json())
  72. .then(json=>this.fetchData())
  73. }
  74.  
  75. }
  76.  
  77. add(id, name){
  78. this.setState({id: '', name: '', addNew: true})
  79. }
  80.  
  81. edit(id, name){
  82. this.setState({id: id, name: name, addNew: false})
  83. }
  84.  
  85. render() {
  86. return (
  87.  
  88. <div>
  89. <BrowserRouter>
  90. <ul>
  91.  
  92. <h2> <Link to='/'>Home</Link></h2>
  93. <h3> Products</h3>
  94. <li><Link to='/Products'>Display Products list </Link></li>
  95.  
  96.  
  97. <li><Link to='/ManageProduct'> Manage Products</Link></li>
  98.  
  99. <h3> Products Types</h3>
  100. <li><Link to='ProductPage'>dontuseyet</Link></li>
  101. <li><Link to='ProductType'>Display Product Type List</Link></li>
  102. <li><Link to='ManageProductType'>Manage Product Type </Link></li>
  103. </ul>
  104.  
  105.  
  106. <Route path='/Products' render={()=>
  107. <div>
  108. <h1>Products List</h1>
  109. <Products/>
  110. </div>
  111. }/>
  112.  
  113. <Route path='/ManageProduct' render={()=>
  114. <div>
  115. <h1>Manage Products</h1>
  116. <ManageProduct/>
  117. </div>
  118. }/>
  119.  
  120. <Route path='/ProductType' render={()=>
  121. <div>
  122. <h1>Product Types List</h1>
  123. <ProductType/>
  124. </div>
  125. }/>
  126.  
  127. <Route path='/ManageProductType' render={()=>
  128. <div>
  129. <h1>Product Types List</h1>
  130. <ManageProductType/>
  131. </div>
  132. }/>
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. </BrowserRouter>
  142.  
  143.  
  144.  
  145.  
  146. </div>
  147. )
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement