Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Navigation from './Navigation.js';
  3. import {Redirect} from 'react-router-dom';
  4. import qs from 'qs';
  5.  
  6. class Product extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. data: null,
  11. productID: props.match.params.id,
  12. product: null,
  13. productPrice: null,
  14. productDescription: null,
  15. productName: null,
  16. productQuantity: 0,
  17. alsoBoughtData: null,
  18. alsoBought: null,
  19. redirect: false
  20. };
  21.  
  22. this.addToCart = this.addToCart.bind(this);
  23. this.changeProductWeight = this.changeProductWeight.bind(this);
  24. }
  25. componentDidMount() {
  26.  
  27. this.getToken();
  28. }
  29.  
  30.  
  31. componentWillMount() {
  32. if(sessionStorage.getItem('token')){
  33. console.log("ja existe token");
  34. }
  35. else{
  36. this.setState({redirect: true});
  37. }
  38. }
  39.  
  40. getToken() {
  41. var obj;
  42. fetch('http://localhost:2018/WebApi/token', {
  43. method: 'POST',
  44. headers: {
  45. 'Content-Type': 'application/x-www-form-urlencoded'
  46. },
  47. body: qs.stringify({
  48. username: 'FEUP',
  49. password: 'qualquer1',
  50. company: 'FRUITSHOP',
  51. instance: 'Default',
  52. grant_type: 'password',
  53. Line: 'professional'
  54. })
  55. }).then(response => response.json())
  56. .then(function(data){
  57. obj = JSON.parse(JSON.stringify(data));
  58. })
  59. .then(() => {
  60. //this.validateUser(obj.access_token);
  61. this.productExists(obj.access_token);
  62. })
  63. .then(() => {
  64. //this.validateUser(obj.access_token);
  65. this.getAlsoBought(obj.access_token);
  66. });
  67. }
  68.  
  69. productExists(token){
  70. var baseExists ='http://localhost:2018/WebApi/Base/Artigos/Existe/'
  71. var existsURL = baseExists+this.state.productID;
  72. console.log(existsURL);
  73. var obj;
  74. fetch(existsURL, {
  75. method: 'GET',
  76. headers: {
  77. 'Authorization': 'Bearer ' + token
  78. },
  79. }).then(response => response.json())
  80. .then(function(data){
  81. obj = JSON.parse(JSON.stringify(data));
  82. console.log(obj);
  83. })
  84. .then(() => {
  85. if(obj===true){
  86. this.getProduct(token);
  87. }
  88. else{
  89. console.log("artigo nao existe");
  90. }
  91. });
  92.  
  93. }
  94.  
  95. getProduct(token) {
  96.  
  97. var baseURL = 'http://localhost:2018/WebApi/Base/Artigos/Edita/';
  98. var newURL = baseURL + this.state.productID;
  99. var obj;
  100. var desc;
  101. console.log(newURL);
  102. fetch(newURL, {
  103. method: 'GET',
  104. headers: {
  105. 'Content-Type': 'application/x-www-form-urlencoded',
  106. 'cache-control': 'no-cache',
  107. 'Authorization': 'Bearer ' + token
  108. },
  109. }).then(response => response.json())
  110. .then(function(data){
  111. obj = JSON.parse(JSON.stringify(data));
  112. //console.log('prod:'+obj);
  113. desc=obj.Observacoes;
  114. console.log("description:"+desc);
  115. obj=obj.Descricao;
  116.  
  117. })
  118. .then(product => this.setState({ product }))
  119. .then(() => {
  120. this.getProductPrice(token);
  121. this.setState({ productName: obj })
  122. this.setState({ productDescription: desc })
  123. });
  124.  
  125.  
  126. //Base/Artigos/DaValorAtributo/MCA/Descricao
  127. }
  128.  
  129. getAlsoBought(token){
  130. let query = JSON.stringify("Select Distinct linha.Artigo from LinhasDoc AS linha Inner Join LinhasDoc AS doc ON linha.IdCabecDoc=doc.IdCabecDoc WHERE doc.Artigo = " + "\'" + this.state.productID + "\'" + " AND linha.Artigo != " + "\'" + this.state.productID + "\'");
  131. console.log(query);
  132. fetch('http://localhost:2018/WebApi/Administrador/Consulta', {
  133. method: 'POST',
  134. headers: {
  135. 'Content-Type': 'application/json',
  136. 'cache-control': 'no-cache',
  137. 'Authorization': 'Bearer '+token
  138. },
  139. body: query,
  140. }).then(response => response.json())
  141. .then(alsoBoughtData => this.setState({ alsoBoughtData }))
  142. .then(() => {
  143. this.buildAlsoBought();
  144. });
  145. }
  146.  
  147. buildAlsoBought(){
  148. var alsoBought = [];
  149. console.log("hello");
  150. if (this.state.alsoBoughtData) {
  151. let obj =JSON.parse(JSON.stringify(this.state.alsoBoughtData));
  152. let products = obj.DataSet.Table;
  153. alsoBought = products;
  154. }
  155. this.setState({alsoBought: alsoBought});
  156. console.log(alsoBought);
  157. }
  158.  
  159. getProductPrice(token){
  160. var base='http://localhost:2018/WebApi/Base/ArtigosPrecos/Edita/';
  161. var priceURL = base+this.state.productID+'/EUR/UN';
  162. var price;
  163. console.log(priceURL);
  164. fetch(priceURL, {
  165. method: 'GET',
  166. headers: {
  167. 'Authorization': 'Bearer ' + token,
  168.  
  169. },
  170. }).then(response => response.json())
  171. .then(function(data){
  172. price = JSON.parse(JSON.stringify(data));
  173. price = price.PVP1;
  174. console.log("pvp1:"+price);
  175. })
  176. .then( () => {
  177. this.setState({ productPrice: price })
  178. });
  179. }
  180.  
  181. addToCart(event) {
  182. let productJSON = this.createProductJSON();
  183.  
  184. if(sessionStorage.getItem('cart')){
  185. let cart = JSON.parse(sessionStorage.getItem('cart'));
  186. console.log(cart);
  187. cart.push(productJSON);
  188. console.log(cart);
  189. sessionStorage.setItem('cart', JSON.stringify(cart));
  190. }
  191. else{
  192. let cart = [];
  193. cart.push(productJSON);
  194. console.log(cart);
  195. sessionStorage.setItem('cart', JSON.stringify(cart));
  196. }
  197. event.preventDefault();
  198. }
  199.  
  200. createProductJSON() {
  201. let productJSON = {};
  202. productJSON.id = this.state.productID;
  203. productJSON.name = this.state.productName;
  204. productJSON.description = this.state.productDescription;
  205. productJSON.price = this.state.productPrice;
  206. productJSON.quantity = this.state.productQuantity;
  207.  
  208. return productJSON;
  209. }
  210.  
  211. changeProductWeight(event){
  212. this.setState({productQuantity: event.target.value});
  213. }
  214.  
  215. render() {
  216.  
  217. if(this.state.redirect){
  218. return (<Redirect to={'/login'}/>)
  219. }
  220. var alsoBought;
  221.  
  222. if(this.state.alsoBought != null){
  223. console.log(this.state.alsoBought);
  224. alsoBought = this.state.alsoBought.map(product =>{
  225. return (
  226. <div key={product.Artigo} className="col-lg-2">
  227. <a href={"../products/" + product.Artigo}>
  228. <img className="img-fluid" width="200" height="160" src={"../img/" + product.Artigo + ".png"} alt="Product" />
  229. </a>
  230. </div>
  231. )
  232. })
  233. }
  234.  
  235. return (
  236. <div>
  237. <Navigation />
  238. <div className="container mt-5" >
  239. <div className="row">
  240. <div className="col-lg-4">
  241. <img className="img-fluid" src={'../img/' + this.state.productID + '.png'} alt="img"></img>
  242. </div>
  243. <div className="col-lg-8">
  244. <div className="container">
  245. <h3>{this.state.productName}</h3>
  246. <p className="product-description">
  247. {this.state.productDescription}
  248. </p>
  249.  
  250. <div className="product price">
  251. <h4>Preço:</h4>
  252. <h4>{this.state.productPrice} € <small className="text-muted">/kg</small></h4>
  253. </div>
  254. </div>
  255. <div className="form-container">
  256. <form className="form-horizontal" onSubmit={this.addToCart}>
  257. <div className="form-group">
  258. <div className="row">
  259. <label className="control-label col-lg-3">Quantidade em kg:</label>
  260. <div className="col-lg-2">
  261. <input type="number" min="0.5" max="10" step="0.5" className="form-control" id="quantity" placeholder="Kg"
  262. name="quantity" value={this.state.productWeight} onChange={this.changeProductWeight}></input>
  263. </div>
  264. <div className="col-lg-4">
  265. <button type="submit" className="btn btn-primary"> Comprar </button>
  266. </div>
  267. </div>
  268. </div>
  269. </form>
  270. </div>
  271. </div>
  272. </div>
  273. <p></p>
  274. <div className="mb-3">
  275. <h5>Clientes que compraram isto também compraram:</h5>
  276. <hr/>
  277. <div className="row">
  278. {alsoBought}
  279. </div>
  280. </div>
  281. </div>
  282.  
  283. </div>
  284. );
  285. }
  286. }
  287.  
  288. export default Product;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement