cart.render.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {createImgPath} from '../helpers/path.helper.js'
  2. import {pizzaList} from '../data/pizzaList.js'
  3. let cartList = [];
  4. const getCartDataLS = () => {
  5. const pizzaFromCartLS = JSON.parse(localStorage.getItem('cartList'));
  6. if (pizzaFromCartLS) {
  7. pizzaFromCartLS.forEach((pizza, i) => cartList[i] = pizza);
  8. }
  9. }
  10. const setCartDataLS = (data) => {
  11. localStorage.setItem('cartList', JSON.stringify(data));
  12. }
  13. getCartDataLS();
  14. const cartElemRender = (pizza) => {
  15. let pizzaFromCart = pizzaList.find(item => item.id == pizza.id);
  16. const productElem = document.createElement('div');
  17. productElem.innerHTML = `
  18. <div><img class="product__img" src="${createImgPath(pizzaFromCart.img)}"></div>
  19. <h5 class="product__title">${pizzaFromCart.name}</h5>
  20. <input class="product__count" type="number" value="${pizza.count}">
  21. <p>${pizzaFromCart.price}</p>
  22. <p>${pizzaFromCart.price * pizza.count}</p>
  23. <span class='product__remove'>&times;</span>
  24. `;
  25. return productElem;
  26. }
  27. const totalOptions = () => {
  28. return cartList.reduce((a, b) => {
  29. let pizzaFromCart = pizzaList.find(item => item.id == b.id);
  30. a.totalPrice += pizzaFromCart.price * b.count;
  31. a.totalCount += b.count;
  32. return {
  33. totalPrice: a.totalPrice,
  34. totalCount: a.totalCount
  35. }
  36. }, {
  37. totalPrice: 0,
  38. totalCount: 0
  39. })
  40. }
  41. const fullCartRender = () => {
  42. const cartContent = document.querySelector('#cart__content');
  43. cartContent.innerHTML = '';
  44. const productElements = document.createElement('div');
  45. productElements.classList.add('product__list');
  46. if (cartList.length) {
  47. document.querySelector('#cart__checkout').classList.add('btn-checkout--active');
  48. document.querySelector('#cart__reset').classList.add('btn-clear--active');
  49. cartList.forEach((pizza, id) => {
  50. const productElem = cartElemRender(pizza);
  51. productElements.appendChild(productElem);
  52. productElem.querySelector(".product__count").onchange = function () {
  53. pizza.count = +this.value;
  54. if (!pizza.count) {
  55. cartList.splice(id, 1);
  56. }
  57. setCartDataLS(cartList);
  58. fullCartRender();
  59. };
  60. productElem.querySelector(".product__remove").addEventListener('click', function () {
  61. cartList.splice(id, 1);
  62. setCartDataLS(cartList);
  63. fullCartRender();
  64. });
  65. });
  66. const totalPriceBlock = document.createElement('div');
  67. totalPriceBlock.innerHTML = `
  68. <div>
  69. <p class="">Итого: ${totalOptions().totalCount} шт. - ${totalOptions().totalPrice} грн</p>
  70. <div>`;
  71. productElements.appendChild(totalPriceBlock);
  72. cartContent.appendChild(productElements);
  73. } else {
  74. cartContent.textContent = 'В корзине нет товаров';
  75. document.querySelector('#cart__checkout').classList.remove('btn-checkout--active');
  76. document.querySelector('#cart__reset').classList.remove('btn-clear--active');
  77. }
  78. }
  79. fullCartRender();
  80. // clear cart
  81. export const removeCartProduct = () => {
  82. localStorage.removeItem('cartList');
  83. localStorage.removeItem('userInfo');
  84. cartList = [];
  85. fullCartRender();
  86. }
  87. document.querySelector('#cart__checkout').onclick = (e) => {
  88. e.preventDefault();
  89. window.open('./registration.html')
  90. }