Advertisement
varun1729

Untitled

Apr 27th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. import React from "react";
  2. import { Card, Button } from "react-bootstrap";
  3. import { useState } from "react";
  4. const products = [
  5. {
  6. p_id: 1,
  7. p_name: "Product 1",
  8. p_cost: 10.99,
  9. p_category: "Category A",
  10. p_desc: "Description for product 1",
  11. p_image: "https://m.media-amazon.com/images/I/41xNB9WGLDL._MCnd_AC_.jpg",
  12. },
  13. {
  14. p_id: 2,
  15. p_name: "Product 2",
  16. p_cost: 20.99,
  17. p_category: "Category B",
  18. p_desc: "Description for product 2",
  19. p_image: "https://m.media-amazon.com/images/I/51mwR4KYp9L._MCnd_AC_.jpg",
  20. },
  21. {
  22. p_id: 3,
  23. p_name: "Product 3",
  24. p_cost: 15.99,
  25. p_category: "Category A",
  26. p_desc: "Description for product 3",
  27. p_image: "https://m.media-amazon.com/images/I/41WVCHnJY7L._MCnd_AC_.jpg",
  28. },
  29. {
  30. p_id: 4,
  31. p_name: "Product 4",
  32. p_cost: 5.99,
  33. p_category: "Category C",
  34. p_desc: "Description for product 4",
  35. p_image: "https://m.media-amazon.com/images/I/41ar14TnikL._MCnd_AC_.jpg",
  36. },
  37. {
  38. p_id: 5,
  39. p_name: "Product 5",
  40. p_cost: 25.99,
  41. p_category: "Category B",
  42. p_desc: "Description for product 5",
  43. p_image:
  44. "https://m.media-amazon.com/images/I/717ghA3VdNL._AC_UY327_QL65_.jpg",
  45. },
  46. {
  47. p_id: 6,
  48. p_name: "Product 6",
  49. p_cost: 18.99,
  50. p_category: "Category A",
  51. p_desc: "Description for product 6",
  52. p_image:
  53. "https://m.media-amazon.com/images/I/510+3Km7MtL._AC_UY327_QL65_.jpg",
  54. },
  55. {
  56. p_id: 7,
  57. p_name: "Product 7",
  58. p_cost: 12.99,
  59. p_category: "Category C",
  60. p_desc: "Description for product 7",
  61. p_image:
  62. "https://rukminim1.flixcart.com/image/612/612/kr2e3680/smartwatch/i/i/t/1-4-rmw2008-android-ios-realme-original-imag4y6fynwz3ukt.jpeg?q=70",
  63. },
  64. {
  65. p_id: 8,
  66. p_name: "Product 8",
  67. p_cost: 9.99,
  68. p_category: "Category B",
  69. p_desc: "Description for product 8",
  70. p_image:
  71. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/m/z/6/-original-imagn8v87hfyfrdh.jpeg?q=70",
  72. },
  73. {
  74. p_id: 9,
  75. p_name: "Product 9",
  76. p_cost: 22.99,
  77. p_category: "Category A",
  78. p_desc: "Description for product 9",
  79. p_image:
  80. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/e/4/c/-original-imaghxg9hnk2bztm.jpeg?q=70",
  81. },
  82. {
  83. p_id: 10,
  84. p_name: "Product 10",
  85. p_cost: 7.99,
  86. p_category: "Category C",
  87. p_desc: "Description for product 10",
  88. p_image:
  89. "https://rukminim1.flixcart.com/image/612/612/kfeamq80/smartwatch/a/u/s/ios-mydr2hn-a-apple-original-imafvvev9qsvhhgt.jpeg?q=70",
  90. },
  91. {
  92. p_id: 11,
  93. p_name: "Product 11",
  94. p_cost: 14.99,
  95. p_category: "Category B",
  96. p_desc: "Description for product 11",
  97. p_image:
  98. "https://rukminim1.flixcart.com/image/612/612/ksxjs7k0/smartwatch/s/g/x/android-sm-r875fzkainu-samsung-yes-original-imag6dthwtevempm.jpeg?q=70",
  99. },
  100. {
  101. p_id: 12,
  102. p_name: "Product 12",
  103. p_cost: 17.99,
  104. p_category: "Category A",
  105. p_desc: "Description for product 12",
  106. p_image:
  107. "https://rukminim1.flixcart.com/image/612/612/ksxjs7k0/smartwatch/u/j/h/android-sm-r890nzkainu-samsung-yes-original-imag6dtg9shty8zf.jpeg?q=70",
  108. },
  109. {
  110. p_id: 13,
  111. p_name: "Product 13",
  112. p_cost: 11.99,
  113. p_category: "Category C",
  114. p_desc: "Description for product 13",
  115. p_image:
  116. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/g/7/l/-original-imagh932jrkynahy.jpeg?q=70",
  117. },
  118. {
  119. p_id: 14,
  120. p_name: "Product 14",
  121. p_cost: 6.99,
  122. p_category: "Category B",
  123. p_desc: "Description for product 14",
  124. p_image:
  125. "https://rukminim1.flixcart.com/image/612/612/ksxjs7k0/smartwatch/r/p/f/android-sm-r890nzsainu-samsung-yes-original-imag6dtgqd5bffrt.jpeg?q=70",
  126. },
  127. {
  128. p_id: 15,
  129. p_name: "Product 15",
  130. p_cost: 21.99,
  131. p_category: "Category A",
  132. p_desc: "Description for product 15",
  133. p_image:
  134. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/i/m/o/-original-imagkfm8nxr9bsuz.jpeg?q=70",
  135. },
  136. {
  137. p_id: 16,
  138. p_name: "Product 16",
  139. p_cost: 16.99,
  140. p_category: "Category C",
  141. p_desc: "Description for product 16",
  142. p_image:
  143. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/g/w/8/1-78-bsw052-android-ios-fire-boltt-yes-original-imaggqzn5zvjfw9m.jpeg?q=70",
  144. },
  145. {
  146. p_id: 17,
  147. p_name: "Product 17",
  148. p_cost: 8.99,
  149. p_category: "Category B",
  150. p_desc: "Description for product 17",
  151. p_image:
  152. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/s/h/x/-original-imagg5qjsgky8h8a.jpeg?q=70",
  153. },
  154. {
  155. p_id: 18,
  156. p_name: "Product 18",
  157. p_cost: 23.99,
  158. p_category: "Category A",
  159. p_desc: "Description for product 18",
  160. p_image:
  161. "https://rukminim1.flixcart.com/image/612/612/l3ahpjk0/smartwatch/c/w/8/-original-imagegyhw5gemgnv.jpeg?q=70",
  162. },
  163. {
  164. p_id: 19,
  165. p_name: "Product 19",
  166. p_cost: 13.99,
  167. p_category: "Category C",
  168. p_desc: "Description for product 19",
  169. p_image:
  170. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/g/8/3/-original-imaggtjvyydggq7k.jpeg?q=70",
  171. },
  172. {
  173. p_id: 20,
  174. p_name: "Product 20",
  175. p_cost: 19.99,
  176. p_category: "Category B",
  177. p_desc: "Description for product 20",
  178. p_image:
  179. "https://rukminim1.flixcart.com/image/612/612/xif0q/smartwatch/v/v/e/-original-imagkvyessegcmp5.jpeg?q=70",
  180. },
  181. ];
  182.  
  183. function Dashboard({ products }) {
  184. const [cartItems, setCartItems] = useState([]);
  185.  
  186. const handleAddToCart = (product) => {
  187. setCartItems([...cartItems, product]);
  188. };
  189.  
  190. const handleRemoveFromCart = (itemToRemove) => {
  191. setCartItems(cartItems.filter((item) => item.p_id !== itemToRemove.p_id));
  192. };
  193.  
  194. const calculateTotal = () => {
  195. return cartItems.reduce((acc, item) => acc + item.p_cost, 0);
  196. };
  197.  
  198. return (
  199. <div>
  200. <div className="row">
  201. <div className="col-10">
  202. <div className="row row-cols-3 my-3">
  203. {products.map((product, i) => (
  204. <div className="col my-3" key={i}>
  205. <div className="card">
  206. <div className="card-header">
  207. <img
  208. src={product.p_image}
  209. className="card-img-top"
  210. alt={product.p_name}
  211. />
  212. </div>
  213. <div className="card-body">
  214. <div className="h2 card-title">{product.p_name}</div>
  215. <div className="h4 card-subtitle text-muted">
  216. {product.p_cost}
  217. </div>
  218. </div>
  219. <div className="card-footer">
  220. <button
  221. onClick={() => {
  222. alert(product.p_desc);
  223. }}
  224. className="btn btn-outline-info btn-block btn-sm"
  225. data-toggle="tooltip"
  226. data-placement="bottom"
  227. title={product.p_desc}
  228. >
  229. Learn More
  230. </button>
  231. <button
  232. onClick={() => {
  233. handleAddToCart(product);
  234. }}
  235. className="btn btn-outline-success btn-block btn-sm"
  236. >
  237. Add to Cart
  238. </button>
  239. </div>
  240. </div>
  241. </div>
  242. ))}
  243. </div>
  244. </div>
  245. <div className="col-2">
  246. <h2>Cart</h2>
  247. {cartItems.length === 0 ? (
  248. <p>Your cart is empty.</p>
  249. ) : (
  250. <>
  251. <ul>
  252. {cartItems.map((item, i) => (
  253. <li className="mb-3" key={i}>
  254. <div className="card">
  255. <div className="card-header">
  256. <img
  257. src={item.p_image}
  258. className="card-img-top"
  259. alt={item.p_name}
  260. />
  261. </div>
  262. <div className="card-body">
  263. <div className="h5 card-title">{item.p_name}</div>
  264. <div className="h6 card-subtitle text-muted">
  265. {item.p_cost}
  266. </div>
  267. </div>
  268. <div className="card-footer">
  269. <button
  270. onClick={() => {
  271. handleRemoveFromCart(item);
  272. }}
  273. className="btn btn-outline-danger btn-block btn-sm"
  274. >
  275. Remove
  276. </button>
  277. </div>
  278. </div>
  279. </li>
  280. ))}
  281. </ul>
  282. <hr />
  283. <div>
  284. <h3>Total Cost: {calculateTotal()}</h3>
  285. </div>
  286. </>
  287. )}
  288. </div>
  289. </div>
  290. </div>
  291. );
  292. }
  293.  
  294. export default Dashboard;
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement