Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const JSON_CATEGORIES = "./data/categories.json";
- const JSON_MENU = "./data/menu.json";
- class Cart {
- constructor() {
- this.items = [];
- this.subtotal = 0;
- }
- getAdditionsCost(additions) {
- return additions
- .map((addition) => {
- var _a;
- return (
- addition.price *
- ((_a = addition.amount) !== null && _a !== void 0 ? _a : 1)
- );
- })
- .reduce((accumulator, current) => accumulator + current);
- }
- addToCart(item) {
- /// Adds item and it's price
- const currentItemsAmount = this.items.length;
- if (this.items.push(item) === currentItemsAmount + 1) {
- this.subtotal += item.price;
- if (item.additions) {
- this.subtotal += this.getAdditionsCost(item.additions);
- }
- /// Returns true if item has been successfully inserted
- return true;
- }
- return false;
- }
- removeFromCart(index) {
- /// Retrieves item
- const item = this.items[index];
- /// If item did NOT exist
- if (item === undefined) {
- return false;
- }
- /// Attempts deletion
- if (this.items.splice(index, 1) !== []) {
- /// Scales subtotal
- this.subtotal -= item.price;
- /// Scales additions price
- if (item.additions) {
- this.subtotal -= this.getAdditionsCost(item.additions);
- }
- return true;
- }
- return false;
- }
- checkout(cb) {
- return cb([...this.items], this.subtotal);
- }
- }
- let njpCart = new Cart();
- const DOM = {
- $modal: $("#product-modal"),
- $modalToggler: $('[data-action="open-cart-modal"]'),
- $addToCart: $('[data-action="add-to-cart"]'),
- $updateCart: $('[data-action="update-cart"]'),
- $details: $("#product-modal .panel-details"),
- $name: $("#product-modal .product-modal-name"),
- $ingredients: $("#product-modal .product-modal-ingredients"),
- $price: $("#product-modal .product-modal-price"),
- $sizes: $("#product-modal .panel-details-size"),
- $sizesList: $("#product-modal .product-modal-sizes"),
- $additions: $("#product-modal .panel-details-additions"),
- $additionsList: $("#product-modal .product-modal-additions"),
- };
- let njpActiveItem = {};
- let njpCategories;
- let njpMenu;
- $.getJSON(JSON_CATEGORIES, (data) => {
- njpCategories = data;
- });
- $.getJSON(JSON_MENU, (data) => {
- njpMenu = data;
- });
- DOM.$addToCart.on("click", function () {
- const formattedItem = {
- name: njpActiveItem.name,
- price: njpActiveItem.price,
- additions: njpActiveItem.additions,
- };
- njpCart.addToCart(formattedItem);
- });
- DOM.$modalToggler.on("click", function () {
- njpActiveItem = getMenuItem($(this).data("id"));
- });
- const findAdditionsOnDom = () => {
- $(".custom-checkbox")
- .find("input")
- .on("change", function () {
- const additionId = $(this)[0].value;
- if (!njpActiveItem.additions) {
- njpActiveItem.additions = [];
- }
- const additionChosen = getCategoryAddition(
- njpActiveItem.categoryId,
- additionId
- );
- njpActiveItem.additions.push(additionChosen);
- });
- };
- $("#product-modal")
- .find(".modal-content")
- .mouseenter(function () {
- findAdditionsOnDom();
- });
- const getMenuItem = (id) => {
- return njpMenu[id - 1];
- };
- const getCategory = (id) => {
- return njpCategories[id - 1];
- };
- getCategoryAddition = (categoryId, additionId) => {
- return getCategory(categoryId)?.additions[additionId - 1];
- };
- const JSON_CATEGORIES = "./data/categories.json";
- const JSON_MENU = "./data/menu.json";
- class Cart {
- constructor() {
- this.items = [];
- this.subtotal = 0;
- }
- getAdditionsCost(additions) {
- return additions
- .map((addition) => {
- var _a;
- return (
- addition.price *
- ((_a = addition.amount) !== null && _a !== void 0 ? _a : 1)
- );
- })
- .reduce((accumulator, current) => accumulator + current);
- }
- addToCart(item) {
- /// Adds item and it's price
- const currentItemsAmount = this.items.length;
- if (this.items.push(item) === currentItemsAmount + 1) {
- this.subtotal += item.price;
- if (item.additions) {
- this.subtotal += this.getAdditionsCost(item.additions);
- }
- /// Returns true if item has been successfully inserted
- return true;
- }
- return false;
- }
- removeFromCart(index) {
- /// Retrieves item
- const item = this.items[index];
- /// If item did NOT exist
- if (item === undefined) {
- return false;
- }
- /// Attempts deletion
- if (this.items.splice(index, 1) !== []) {
- /// Scales subtotal
- this.subtotal -= item.price;
- /// Scales additions price
- if (item.additions) {
- this.subtotal -= this.getAdditionsCost(item.additions);
- }
- return true;
- }
- return false;
- }
- checkout(cb) {
- return cb([...this.items], this.subtotal);
- }
- }
- let njpCart = new Cart();
- const DOM = {
- $modal: $("#product-modal"),
- $modalToggler: $('[data-action="open-cart-modal"]'),
- $addToCart: $('[data-action="add-to-cart"]'),
- $updateCart: $('[data-action="update-cart"]'),
- $details: $("#product-modal .panel-details"),
- $name: $("#product-modal .product-modal-name"),
- $ingredients: $("#product-modal .product-modal-ingredients"),
- $price: $("#product-modal .product-modal-price"),
- $sizes: $("#product-modal .panel-details-size"),
- $sizesList: $("#product-modal .product-modal-sizes"),
- $additions: $("#product-modal .panel-details-additions"),
- $additionsList: $("#product-modal .product-modal-additions"),
- $deleteItemFromCart: $('[data-action="remove-from-cart"]'),
- };
- let njpActiveItem = {};
- let njpCategories;
- let njpMenu;
- $.getJSON(JSON_CATEGORIES, (data) => {
- njpCategories = data;
- });
- $.getJSON(JSON_MENU, (data) => {
- njpMenu = data;
- });
- DOM.$addToCart.on("click", function () {
- const formattedItem = {
- name: njpActiveItem.name,
- price: njpActiveItem.price,
- additions: njpActiveItem.additions,
- };
- njpCart.addToCart(formattedItem);
- });
- DOM.$modalToggler.on("click", function () {
- njpActiveItem = getMenuItem($(this).data("id"));
- });
- $(DOM.$deleteItemFromCart).on('click').on("click", function () {
- console.log("paparezza");
- console.log($(this).data("id"));
- njpActiveItem = getMenuItem($(this).data("id"));
- });
- const findAdditionsOnDom = () => {
- $(".custom-checkbox")
- .find("input")
- .on("change", function () {
- const additionId = $(this)[0].value;
- if (!njpActiveItem.additions) {
- njpActiveItem.additions = [];
- }
- const additionChosen = getCategoryAddition(
- njpActiveItem.categoryId,
- additionId
- );
- njpActiveItem.additions.push(additionChosen);
- });
- };
- $("#product-modal")
- .find(".modal-content")
- .mouseenter(function () {
- findAdditionsOnDom();
- });
- //panel-cart
- const getMenuItem = (id) => {
- return njpMenu[id - 1];
- };
- const getCategory = (id) => {
- return njpCategories[id - 1];
- };
- getCategoryAddition = (categoryId, additionId) => {
- return getCategory(categoryId)?.additions[additionId - 1];
- };
Add Comment
Please, Sign In to add comment