Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Navigation, Footer } from './Navigation.js';
  3. import qs from 'qs';
  4.  
  5. class HomepageSuggestions extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. data: null,
  10. };
  11. }
  12.  
  13. componentDidMount() {
  14. this.getData();
  15. }
  16.  
  17. getData() {
  18. // Fetch Products
  19. /*
  20. var obj;
  21. fetch('http://localhost:2018/WebApi/token', {
  22. method: 'POST',
  23. headers: {
  24. 'Content-Type': 'application/x-www-form-urlencoded'
  25. },
  26. body: qs.stringify({
  27. username: 'FEUP',
  28. password: 'qualquer1',
  29. company: 'FRUITS',
  30. instance: 'DEFAULT',
  31. grant_type: 'password',
  32. line: 'professional'
  33. })
  34. }).then(response => response.json())
  35. .then(function(data) {
  36. console.log(JSON.parse(JSON.stringify(data)));
  37. });
  38. .then(() => {
  39. this.getProducts(obj.access_token);
  40. });*/
  41. }
  42.  
  43. getProducts(token){
  44. fetch('http://localhost:2018/WebApi/Base/Artigos/Edita/APV', {
  45. method: 'GET',
  46. headers: {
  47. 'Content-Type': 'application/x-www-form-urlencoded',
  48. 'cache-control': 'no-cache',
  49. 'Authorization': 'Bearer '+token
  50. },
  51. }).then(response => response.json())
  52. .then(data => this.setState({ data }));
  53. }
  54.  
  55. render() {
  56. //console.log(this.state);
  57. var obj;
  58. var a;
  59. if(this.state.data){
  60. obj =JSON.parse(JSON.stringify(this.state.data));
  61. console.log(obj.Descricao);
  62. a = obj.Descricao;
  63. }
  64. var cenas ="cenas";
  65. return (
  66. <div className="mb-3">
  67. <h4>Sugestões</h4>
  68. <hr />
  69. <div className="row">
  70.  
  71. <div className="col-lg-2">
  72. <img className="img-fluid mb-2" src="/img/img-placeholder.png" alt="Product" />
  73. <h6>Name: {a}</h6>
  74. <h6>Price:</h6>
  75. </div>
  76. <div className="col-lg-2">
  77. <img className="img-fluid mb-2" src="/img/img-placeholder.png" alt="Product" />
  78. <h6>Name:</h6>
  79. <h6>Price:</h6>
  80. </div>
  81.  
  82. </div>
  83. </div>
  84. );
  85. }
  86. }
  87.  
  88. class HomepageNew extends Component {
  89. constructor(props) {
  90. super(props);
  91. this.state = {
  92. data: null,
  93. };
  94. }
  95.  
  96. componentDidMount() {
  97. this.getData();
  98. }
  99.  
  100. getData() {
  101. // Fetch Products
  102. var obj;
  103. fetch('http://localhost:2018/WebApi/token', {
  104. method: 'POST',
  105. headers: {
  106. 'Content-Type': 'application/x-www-form-urlencoded'
  107. },
  108. body: qs.stringify({
  109. username: 'FEUP',
  110. password: 'qualquer1',
  111. company: 'FRUITS',
  112. instance: 'DEFAULT',
  113. grant_type: 'password',
  114. line: 'professional'
  115. })
  116. }).then(response => response.json())
  117. .then(function(data) {
  118. obj =JSON.parse(JSON.stringify(data));
  119. })
  120. .then(() => {
  121. this.getProductsNew(obj.access_token);
  122. });
  123. }
  124.  
  125. getProductsNew(token){
  126. fetch('http://localhost:2018/WebApi/Base/Artigos/LstArtigos', {
  127. method: 'GET',
  128. headers: {
  129. 'Content-Type': 'application/x-www-form-urlencoded',
  130. 'cache-control': 'no-cache',
  131. 'Authorization': 'Bearer '+token
  132. },
  133. }).then(response => response.json())
  134. .then(data => this.setState({ data }));
  135. }
  136.  
  137. render() {
  138.  
  139. //Selecting 6 of the most recents elements
  140. var newProductsName = [];
  141. if(this.state.data){
  142. let obj =JSON.parse(JSON.stringify(this.state.data));
  143. let products = obj.DataSet.Table;
  144. let newProducts = products.slice(products.length-6, products.length);
  145. let i;
  146. for(i=0; i<newProducts.length;i++){
  147. newProductsName[i] = newProducts[i].Artigo;
  148. }
  149. }
  150.  
  151. return (
  152. <div className="mb-3">
  153. <h4>Novidades</h4>
  154. <hr />
  155. <div className="row">
  156. <div className="col-lg-2">
  157. <img className="img-fluid" src={"img/" + newProductsName[5] + ".png"} alt="Product" />
  158. </div>
  159. <div className="col-lg-2">
  160. <img className="img-fluid" src={"img/" + newProductsName[4] + ".png"} alt="Product" />
  161. </div>
  162. <div className="col-lg-2">
  163. <img className="img-fluid" src={"img/" + newProductsName[3] + ".png"} alt="Product" />
  164. </div>
  165. <div className="col-lg-2">
  166. <img className="img-fluid" src={"img/" + newProductsName[2] + ".png"} alt="Product" />
  167. </div>
  168. <div className="col-lg-2">
  169. <img className="img-fluid" src={"img/" + newProductsName[1] + ".png"} alt="Product" />
  170. </div>
  171. <div className="col-lg-2">
  172. <img className="img-fluid" src={"img/" + newProductsName[0] + ".png"} alt="Product" />
  173. </div>
  174. </div>
  175. </div>
  176. );
  177. }
  178. }
  179.  
  180. class HomepageCategory extends Component {
  181. render() {
  182. return (
  183. <div className="mb-3">
  184. <div className="row">
  185. <div className="col-lg-6">
  186. <h4>Fruta</h4>
  187. <a href="/category/fruta">
  188. <img className="img-fluid" src="/img/fruta.jpg" alt="Fruta" />
  189. </a>
  190. </div>
  191. <div className="col-lg-6">
  192. <h4>Legumes</h4>
  193. <a href="/category/legumes">
  194. <img className="img-fluid" src="/img/legumes.jpg" alt="Legumes" />
  195. </a>
  196. </div>
  197. </div>
  198. </div>
  199. )
  200. }
  201. }
  202.  
  203. class Homepage extends Component {
  204. render() {
  205. return (
  206. <div>
  207. <Navigation />
  208. <div className="container main-container mt-5 mb-3">
  209. <HomepageCategory />
  210. <HomepageSuggestions />
  211. <HomepageNew />
  212. </div>
  213. <Footer />
  214. </div>
  215. );
  216. }
  217. }
  218.  
  219. export default Homepage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement