pizza.render.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {setPizzasListToLS, getPizzasListFromLS} from '../services/localStorage.service.js'
  2. import {createImgPath} from '../helpers/path.helper.js'
  3. import {pizzaList} from '../data/pizzaList.js'
  4. getPizzasListFromLS();
  5. export function renderCard(pizza) {
  6. const blockForCard = document.createElement('div');
  7. blockForCard.className = "col-xs-12 col-md-4 col-lg-3 mb-4"
  8. const card = document.createElement('div')
  9. card.className = "card h-100 pb-3 pt-2 px-1";
  10. card.innerHTML = `
  11. <p class = "card-caloricity">${pizza.caloricity || ''} Ккал</p>
  12. <div class = "mx-auto pt-3" style="width: 150px">
  13. <img class ="card-img-top" src="${createImgPath(pizza.img)}" alt="${pizza.name}"/>
  14. </div>
  15. <div class = "card-body">
  16. <h5 class = "card-title">${pizza.name || ''}</h5>
  17. <p class = "card-text">${pizza.composition.join(', ') || ''}</p>
  18. </div>
  19. <div class = "card-footer bg-transparent border-white">
  20. <p class = "card-price">${pizza.price || ''} грн</p>
  21. </div>
  22. `
  23. // favorit
  24. const heart = document.createElement('button');
  25. heart.classList.add('btn-favorite');
  26. if (pizza.isFavorite) heart.classList.add('btn-favorite--active');
  27. heart.addEventListener('click', function (event) {
  28. event.stopPropagation();
  29. this.classList.toggle('btn-favorite--active');
  30. pizza.isFavorite = !pizza.isFavorite;
  31. setPizzasListToLS();
  32. renderCards(pizzaList);
  33. })
  34. //add to cart
  35. const buttonCart = document.createElement('button')
  36. buttonCart.className = 'btn btn-add-to-basket d-block'
  37. buttonCart.innerText = 'Заказать'
  38. buttonCart.onclick = (e) => {
  39. e.preventDefault()
  40. e.stopPropagation();
  41. addPizzaToCart(pizza)
  42. }
  43. card.appendChild(heart);
  44. card.appendChild(buttonCart);
  45. card.onclick = (e) => {
  46. if(!e.target.className.includes('btn-add-to-basket')) {
  47. modalWindowContent(pizza);
  48. }
  49. }
  50. card.setAttribute("data-toggle", "modal");
  51. card.setAttribute("data-target", "#staticBackdrop");
  52. blockForCard.appendChild(card)
  53. document.querySelector('.card-list').appendChild(blockForCard)
  54. }
  55. export const modalWindowContent = (pizza) => {
  56. const modalBody = document.querySelector('.modal-body')
  57. modalBody.innerHTML = `
  58. <p class = "card-caloricity">${pizza.caloricity || ''} Ккал</p>
  59. <div class = "mx-auto pt-3" style="width: 150px">
  60. <img class ="card-img-top" src="${createImgPath(pizza.img)}" alt="${pizza.name}"/>
  61. </div>
  62. <div class = "card-body">
  63. <h5 class = "card-title">${pizza.name || ''}</h5>
  64. <p class = "card-text">${pizza.composition.join(', ') || ''}</p>
  65. </div>
  66. <div class = "card-footer bg-transparent border-white">
  67. <p class = "card-price">${pizza.price || ''} грн</p>
  68. </div>
  69. `;
  70. }
  71. export const renderCards = pizzas => {
  72. document.querySelector('.card-list').innerHTML = '';
  73. pizzas.forEach(pizza => renderCard(pizza))
  74. }
  75. const cartList = [];
  76. const getCartDataLS = () => {
  77. const pizzaFromCartLS = JSON.parse(localStorage.getItem('cartList'));
  78. if (pizzaFromCartLS) {
  79. pizzaFromCartLS.forEach((pizza, i) => cartList[i] = pizza);
  80. }
  81. }
  82. getCartDataLS();
  83. const setCartDataLS = (data) => {
  84. localStorage.setItem('cartList', JSON.stringify(data));
  85. }
  86. const addPizzaToCart = (pizza) => {
  87. let findPizzaById = cartList.find(item => item.id === pizza.id)
  88. if (findPizzaById) {
  89. findPizzaById.count += 1;
  90. setCartDataLS(cartList);
  91. renderCart();
  92. return;
  93. }
  94. cartList.push({
  95. id: pizza.id,
  96. count: 1
  97. });
  98. setCartDataLS(cartList);
  99. renderCart();
  100. }
  101. const totalOptions = () => {
  102. return cartList.reduce((a, b) => {
  103. let pizzaFromCart = pizzaList.find(item => item.id == b.id);
  104. a.totalPrice += pizzaFromCart.price * b.count;
  105. a.totalCount += b.count;
  106. return {
  107. totalPrice: a.totalPrice,
  108. totalCount: a.totalCount
  109. }
  110. }, {
  111. totalPrice: 0,
  112. totalCount: 0
  113. })
  114. }
  115. const renderCart = () => {
  116. document.querySelector('.btn-cart-info').innerHTML = `${totalOptions().totalCount} шт. <br> ${totalOptions().totalPrice} грн`;
  117. }
  118. renderCart();
  119. document.querySelector('.btn-cart').onclick = function (e) {
  120. e.preventDefault();
  121. if (totalOptions().totalCount) {
  122. window.open('./cart.html');
  123. }
  124. }