informaticage

Logic bux fix on massari cart

Dec 13th, 2020 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const JSON_CATEGORIES = "./data/categories.json";
  2. const JSON_MENU = "./data/menu.json";
  3.  
  4. class Cart {
  5.   constructor() {
  6.     this.items = [];
  7.     this.subtotal = 0;
  8.   }
  9.   getAdditionsCost(additions) {
  10.     return additions
  11.       .map((addition) => {
  12.         var _a;
  13.         return (
  14.           addition.price *
  15.           ((_a = addition.amount) !== null && _a !== void 0 ? _a : 1)
  16.         );
  17.       })
  18.       .reduce((accumulator, current) => accumulator + current);
  19.   }
  20.   addToCart(item) {
  21.     /// Adds item and it's price
  22.     const currentItemsAmount = this.items.length;
  23.     if (this.items.push(item) === currentItemsAmount + 1) {
  24.       this.subtotal += item.price;
  25.       if (item.additions) {
  26.         this.subtotal += this.getAdditionsCost(item.additions);
  27.       }
  28.       /// Returns true if item has been successfully inserted
  29.       return true;
  30.     }
  31.     return false;
  32.   }
  33.   removeFromCart(index) {
  34.     /// Retrieves item
  35.     const item = this.items[index];
  36.     /// If item did NOT exist
  37.     if (item === undefined) {
  38.       return false;
  39.     }
  40.     /// Attempts deletion
  41.     if (this.items.splice(index, 1) !== []) {
  42.       /// Scales subtotal
  43.       this.subtotal -= item.price;
  44.       /// Scales additions price
  45.       if (item.additions) {
  46.         this.subtotal -= this.getAdditionsCost(item.additions);
  47.       }
  48.       return true;
  49.     }
  50.     return false;
  51.   }
  52.   checkout(cb) {
  53.     return cb([...this.items], this.subtotal);
  54.   }
  55. }
  56.  
  57. let njpCart = new Cart();
  58.  
  59. const DOM = {
  60.   $modal: $("#product-modal"),
  61.   $modalToggler: $('[data-action="open-cart-modal"]'),
  62.   $addToCart: $('[data-action="add-to-cart"]'),
  63.   $updateCart: $('[data-action="update-cart"]'),
  64.   $details: $("#product-modal .panel-details"),
  65.   $name: $("#product-modal .product-modal-name"),
  66.   $ingredients: $("#product-modal .product-modal-ingredients"),
  67.   $price: $("#product-modal .product-modal-price"),
  68.   $sizes: $("#product-modal .panel-details-size"),
  69.   $sizesList: $("#product-modal .product-modal-sizes"),
  70.   $additions: $("#product-modal .panel-details-additions"),
  71.   $additionsList: $("#product-modal .product-modal-additions"),
  72. };
  73.  
  74. let njpActiveItem = {};
  75. let njpCategories;
  76. let njpMenu;
  77.  
  78. $.getJSON(JSON_CATEGORIES, (data) => {
  79.   njpCategories = data;
  80. });
  81.  
  82. $.getJSON(JSON_MENU, (data) => {
  83.   njpMenu = data;
  84. });
  85.  
  86. DOM.$addToCart.on("click", function () {
  87.   const formattedItem = {
  88.     name: njpActiveItem.name,
  89.     price: njpActiveItem.price,
  90.     additions: njpActiveItem.additions,
  91.   };
  92.   njpCart.addToCart(formattedItem);
  93. });
  94.  
  95. DOM.$modalToggler.on("click", function () {
  96.   njpActiveItem = getMenuItem($(this).data("id"));
  97. });
  98.  
  99. const findAdditionsOnDom = () => {
  100.   $(".custom-checkbox")
  101.     .find("input")
  102.     .on("change", function () {
  103.       const additionId = $(this)[0].value;
  104.       if (!njpActiveItem.additions) {
  105.         njpActiveItem.additions = [];
  106.       }
  107.       const additionChosen = getCategoryAddition(
  108.         njpActiveItem.categoryId,
  109.         additionId
  110.       );
  111.       njpActiveItem.additions.push(additionChosen);
  112.     });
  113. };
  114.  
  115. $("#product-modal")
  116.   .find(".modal-content")
  117.   .mouseenter(function () {
  118.     findAdditionsOnDom();
  119.   });
  120.  
  121. const getMenuItem = (id) => {
  122.   return njpMenu[id - 1];
  123. };
  124.  
  125. const getCategory = (id) => {
  126.   return njpCategories[id - 1];
  127. };
  128.  
  129. getCategoryAddition = (categoryId, additionId) => {
  130.   return getCategory(categoryId)?.additions[additionId - 1];
  131. };
  132.  
  133.  
  134. const JSON_CATEGORIES = "./data/categories.json";
  135. const JSON_MENU = "./data/menu.json";
  136.  
  137. class Cart {
  138.   constructor() {
  139.     this.items = [];
  140.     this.subtotal = 0;
  141.   }
  142.   getAdditionsCost(additions) {
  143.     return additions
  144.       .map((addition) => {
  145.         var _a;
  146.         return (
  147.           addition.price *
  148.           ((_a = addition.amount) !== null && _a !== void 0 ? _a : 1)
  149.         );
  150.       })
  151.       .reduce((accumulator, current) => accumulator + current);
  152.   }
  153.   addToCart(item) {
  154.     /// Adds item and it's price
  155.     const currentItemsAmount = this.items.length;
  156.     if (this.items.push(item) === currentItemsAmount + 1) {
  157.       this.subtotal += item.price;
  158.       if (item.additions) {
  159.         this.subtotal += this.getAdditionsCost(item.additions);
  160.       }
  161.       /// Returns true if item has been successfully inserted
  162.       return true;
  163.     }
  164.     return false;
  165.   }
  166.   removeFromCart(index) {
  167.     /// Retrieves item
  168.     const item = this.items[index];
  169.     /// If item did NOT exist
  170.     if (item === undefined) {
  171.       return false;
  172.     }
  173.     /// Attempts deletion
  174.     if (this.items.splice(index, 1) !== []) {
  175.       /// Scales subtotal
  176.       this.subtotal -= item.price;
  177.       /// Scales additions price
  178.       if (item.additions) {
  179.         this.subtotal -= this.getAdditionsCost(item.additions);
  180.       }
  181.       return true;
  182.     }
  183.     return false;
  184.   }
  185.   checkout(cb) {
  186.     return cb([...this.items], this.subtotal);
  187.   }
  188. }
  189.  
  190. let njpCart = new Cart();
  191.  
  192. const DOM = {
  193.   $modal: $("#product-modal"),
  194.   $modalToggler: $('[data-action="open-cart-modal"]'),
  195.   $addToCart: $('[data-action="add-to-cart"]'),
  196.   $updateCart: $('[data-action="update-cart"]'),
  197.   $details: $("#product-modal .panel-details"),
  198.   $name: $("#product-modal .product-modal-name"),
  199.   $ingredients: $("#product-modal .product-modal-ingredients"),
  200.   $price: $("#product-modal .product-modal-price"),
  201.   $sizes: $("#product-modal .panel-details-size"),
  202.   $sizesList: $("#product-modal .product-modal-sizes"),
  203.   $additions: $("#product-modal .panel-details-additions"),
  204.   $additionsList: $("#product-modal .product-modal-additions"),
  205.   $deleteItemFromCart: $('[data-action="remove-from-cart"]'),
  206. };
  207.  
  208. let njpActiveItem = {};
  209. let njpCategories;
  210. let njpMenu;
  211.  
  212. $.getJSON(JSON_CATEGORIES, (data) => {
  213.   njpCategories = data;
  214. });
  215.  
  216. $.getJSON(JSON_MENU, (data) => {
  217.   njpMenu = data;
  218. });
  219.  
  220. DOM.$addToCart.on("click", function () {
  221.   const formattedItem = {
  222.     name: njpActiveItem.name,
  223.     price: njpActiveItem.price,
  224.     additions: njpActiveItem.additions,
  225.   };
  226.   njpCart.addToCart(formattedItem);
  227. });
  228.  
  229. DOM.$modalToggler.on("click", function () {
  230.   njpActiveItem = getMenuItem($(this).data("id"));
  231. });
  232.  
  233. $(DOM.$deleteItemFromCart).on('click').on("click", function () {
  234.   console.log("paparezza");
  235.   console.log($(this).data("id"));
  236.   njpActiveItem = getMenuItem($(this).data("id"));
  237. });
  238.  
  239. const findAdditionsOnDom = () => {
  240.   $(".custom-checkbox")
  241.     .find("input")
  242.     .on("change", function () {
  243.       const additionId = $(this)[0].value;
  244.       if (!njpActiveItem.additions) {
  245.         njpActiveItem.additions = [];
  246.       }
  247.       const additionChosen = getCategoryAddition(
  248.         njpActiveItem.categoryId,
  249.         additionId
  250.       );
  251.       njpActiveItem.additions.push(additionChosen);
  252.     });
  253. };
  254.  
  255. $("#product-modal")
  256.   .find(".modal-content")
  257.   .mouseenter(function () {
  258.     findAdditionsOnDom();
  259.   });
  260.  
  261.   //panel-cart
  262.  
  263. const getMenuItem = (id) => {
  264.   return njpMenu[id - 1];
  265. };
  266.  
  267. const getCategory = (id) => {
  268.   return njpCategories[id - 1];
  269. };
  270.  
  271. getCategoryAddition = (categoryId, additionId) => {
  272.   return getCategory(categoryId)?.additions[additionId - 1];
  273. };
  274.  
  275.  
  276.  
Add Comment
Please, Sign In to add comment