From 3a35074ffaa656a186113a174e604e24399ad5a0 Mon Sep 17 00:00:00 2001 From: kj1352 Date: Mon, 17 Feb 2020 12:13:59 +0530 Subject: [PATCH] Cart page calculation complete --- src/app/cart/cart.page.html | 38 ++++++++------- src/app/cart/cart.page.scss | 6 ++- src/app/cart/cart.page.ts | 94 ++++++++++++++++++++++++++++++++++++- src/app/mocks/url.ts | 2 +- 4 files changed, 118 insertions(+), 22 deletions(-) diff --git a/src/app/cart/cart.page.html b/src/app/cart/cart.page.html index f8b4920..1bff000 100644 --- a/src/app/cart/cart.page.html +++ b/src/app/cart/cart.page.html @@ -41,9 +41,9 @@
  • - +
    {{ item.quantity }}
    - +
  • @@ -58,12 +58,12 @@
  • - - - + - + Pay Now - +
    @@ -139,9 +134,16 @@
    ₹ {{ item.item_price - item.item_discount }} - +
    + + + {{ isPresentInCart(temp_outlet_details.outlet_id, item.menuitem_id, selectedTempMallId) }} + + +
    diff --git a/src/app/cart/cart.page.scss b/src/app/cart/cart.page.scss index c50d417..5446e77 100644 --- a/src/app/cart/cart.page.scss +++ b/src/app/cart/cart.page.scss @@ -108,13 +108,14 @@ ul { font-weight: bold; color: dimgrey; width: 95%; - margin: 0 auto; + margin: 10px auto 0; &.outlet { background-color: #efefef; color: var(--brand-grey); padding: 5px 10px; width: 100%; + margin: 0 auto; } ion-button { @@ -362,6 +363,8 @@ ul { margin: 10px 0; white-space: nowrap; text-overflow: ellipsis; + width: 90%; + overflow: hidden; } } @@ -409,6 +412,7 @@ ul { .count { color: var(--brand-dark-grey); font-size: 12px; + text-align: center; } } } diff --git a/src/app/cart/cart.page.ts b/src/app/cart/cart.page.ts index 1aba7a2..cf7c73d 100644 --- a/src/app/cart/cart.page.ts +++ b/src/app/cart/cart.page.ts @@ -35,6 +35,7 @@ export class CartPage implements OnInit { tempPickupTime: any; showInstantMenu: boolean = false; temp_outlet_details: any; + selectedTempMallId: string; constructor( private location: Location, @@ -128,7 +129,7 @@ export class CartPage implements OnInit { } } - ionViewDidLeave() { + storeData() { localStorage.userCart = JSON.stringify(this.userCart); } @@ -137,13 +138,102 @@ export class CartPage implements OnInit { } back() { + this.storeData(); this.location.back(); } openInstantMenu(outlet) { + this.selectedTempMallId = outlet.mall_id; this.showInstantMenu = true; this.temp_outlet_details = this.getOutletDetails(outlet); - console.log(this.temp_outlet_details); } + isPresentInCart(outletId: number, itemId: number, tempMallId: number) { + let item: any; + + item = this.userCart.orderedlist.find((order) => { + return order.item_id === itemId && order.outlet_id === outletId && order.mall_id === tempMallId; + }); + + return item? item.quantity: 0; + } + + incrementCartCount(outletId: number, itemId: number, tempMallId: number) { + let outletitem = this.getItemDetailsByIds(outletId, itemId, tempMallId); + + if (outletitem) { + let i = this.userCart.orderedlist.findIndex((order) => { + return order.item_id === itemId && order.outlet_id === outletId && order.mall_id === tempMallId; + }); + + this.userCart.orderedlist[i].quantity += 1; + this.userCart.orderedlist[i].total_price = (outletitem.item_price - outletitem.item_discount) * this.userCart.orderedlist[i].quantity; + } + } + + decrementCartCount(outletId: number, itemId: number, tempMallId: number) { + let outletitem = this.getItemDetailsByIds(outletId, itemId, tempMallId); + + if (outletitem) { + let i = this.userCart.orderedlist.findIndex((order) => { + return order.item_id === itemId && order.outlet_id === outletId && order.mall_id === tempMallId; + }); + + if (this.userCart.orderedlist[i].quantity > 1) { + this.userCart.orderedlist[i].quantity -= 1; + this.userCart.orderedlist[i].total_price = (outletitem.item_price - outletitem.item_discount) * this.userCart.orderedlist[i].quantity; + } else if (this.userCart.orderedlist[i].quantity === 1) { + this.userCart.orderedlist.splice(i, 1); + this.storeData(); + this.fetchCartItems(); + } + } + } + + getItemDetailsByIds(outletId: number, itemId: number, tempMallId: number) { + let mall, outlet, outletitem; + mall = this.allMalls.find((mall) => { + return mall.mall.mall_id === tempMallId; + }); + + if (mall) { + outlet = mall.mall.outlet.find((outlet) => { + return outlet.outlet_id === outletId; + }); + } + + if (outlet) { + return outletitem = outlet.menuitems.find((menu) => { + return menu.menuitem_id === itemId; + }); + } + } + + addToCart(outletId: number, itemId: number, tempMallId: number) { + let outletitem = this.getItemDetailsByIds(outletId, itemId, tempMallId); + if (outletitem) { + this.userCart.orderedlist.push({ + mall_id: tempMallId, + outlet_id: outletId, + item_id: itemId, + quantity: 1, + pickup_time: moment().add(moment.duration(outletitem.wait_duration).asMinutes(), 'minutes').format(), + take_away: true, + order_status: false, + total_price: outletitem.item_price - outletitem.item_discount, + soft_delete: false + }); + } + } + + getTotalCartAmount() { + let total: number = 0; + + this.userCart.orderedlist.forEach((order) => { + total = order.total_price + total; + }); + + return total; + } + } diff --git a/src/app/mocks/url.ts b/src/app/mocks/url.ts index 87d5968..334ebe7 100644 --- a/src/app/mocks/url.ts +++ b/src/app/mocks/url.ts @@ -1,3 +1,3 @@ -export const URL: string = 'http://13.126.110.70:8080/mall-aggregator'; +export const URL: string = 'http://13.235.48.49:8989/mall-aggregator'; export const TOKEN: string = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';