src/app/pages/shopping-cart/shopping-cart.component.ts
Shopping cart page component
selector | app-shopping-cart |
styleUrls | shopping-cart.component.scss |
templateUrl | ./shopping-cart.component.html |
Properties |
Methods |
constructor(service: AlertService)
|
||||||||
Constructor
Parameters :
|
calculateSum |
calculateSum()
|
Calculate sum of all ShoppingCartItems total prices
Returns :
void
|
calculateTotalPrice | ||||||||
calculateTotalPrice(cartItem: ShoppingCartItem)
|
||||||||
calculate the total price of a shoppingcart position
Parameters :
Returns :
void
|
decreaseQuantity | ||||||||
decreaseQuantity(cartItem: ShoppingCartItem)
|
||||||||
Decrease shopping cart item amount
Parameters :
Returns :
void
|
deleteCartItem | ||||||||
deleteCartItem(index: number)
|
||||||||
Delete shopping cart item
Parameters :
Returns :
void
|
increaseQuantity | ||||||||
increaseQuantity(cartItem: ShoppingCartItem)
|
||||||||
Increase shopping cart item amount
Parameters :
Returns :
void
|
ngOnInit |
ngOnInit()
|
Initalize component
Returns :
void
|
Public cartItems |
cartItems:
|
Type : ShoppingCartItem[]
|
Default value : []
|
Shopping cart items |
Public checkType |
checkType:
|
Type : any
|
Default value : SHOPPINGCARTITEMTYPE
|
checkType variable |
Private log |
log:
|
Default value : Log.create('ShoppingCartComponent')
|
Logger |
Public sum |
sum:
|
Type : number
|
Sum |
import { Component, OnInit } from '@angular/core';
import * as localforage from 'localforage';
import { Log } from 'ng2-logger';
import { SHOPPINGCARTITEMTYPE } from '../../enums/shopping-cart-item-type';
import { Alert } from '../../interfaces/alert';
import { ShoppingCartItem } from '../../interfaces/shopping-cart-item';
import { AlertService } from '../../services/alert/alert.service';
/**
* Shopping cart page component
* @author Daniel Sogl, Tim Kriessler
*/
@Component({
selector: 'app-shopping-cart',
templateUrl: './shopping-cart.component.html',
styleUrls: ['./shopping-cart.component.scss']
})
export class ShoppingCartComponent implements OnInit {
/** Logger */
private log = Log.create('ShoppingCartComponent');
/** Shopping cart items */
public cartItems: ShoppingCartItem[] = [];
/** Sum */
public sum: number;
/** checkType variable */
public checkType: any = SHOPPINGCARTITEMTYPE;
/**
* Constructor
*/
constructor(private service: AlertService) {}
/**
* Initalize component
*/
ngOnInit() {
this.log.color = 'orange';
this.log.d('Component initialized');
// Load items from local storage
localforage
.getItem<ShoppingCartItem[]>('cart-items')
.then(items => {
if (items) {
this.log.d('Cart items', items);
this.cartItems = items;
this.cartItems.sort((a, b) => {
if (a.eventname < b.eventname) {
return -1;
}
if (a.eventname > b.eventname) {
return 1;
}
return 0;
});
}
this.calculateSum();
})
.catch(err => {
this.log.er('Error getting local storage items');
});
}
/**
* Calculate sum of all ShoppingCartItems total prices
*/
calculateSum() {
this.sum = 0;
for (let i = 0; i < this.cartItems.length; i++) {
this.calculateTotalPrice(this.cartItems[i]);
this.sum += this.cartItems[i].price * this.cartItems[i].amount;
}
}
/**
* Decrease shopping cart item amount
* @param {ShoppingCartItem} cartItem Item
*/
decreaseQuantity(cartItem: ShoppingCartItem) {
if (cartItem.amount > 1) {
cartItem.amount--;
}
this.calculateTotalPrice(cartItem); // update total price
this.calculateSum(); // update shopping cart sum
this.log.info(
'Amount decremented. Event: ' + cartItem.name + ' ' + cartItem.eventname
);
localforage.setItem('cart-items', this.cartItems);
}
/**
* Increase shopping cart item amount
* @param {ShoppingCartItem} cartItem Item
*/
increaseQuantity(cartItem: ShoppingCartItem) {
cartItem.amount++;
this.calculateTotalPrice(cartItem); // update total price
this.calculateSum(); // update shopping cart sum
this.log.info(
'Amount incremented. Event: ' + cartItem.name + ' ' + cartItem.eventname
);
localforage.setItem('cart-items', this.cartItems);
}
/**
* calculate the total price of a shoppingcart position
* @param {ShoppingCartItem} cartItem Item
*/
calculateTotalPrice(cartItem: ShoppingCartItem) {
cartItem.totalPrice = cartItem.amount * cartItem.price;
}
/**
* Delete shopping cart item
* @param {ShoppingCartItem} cartItem Item
*/
deleteCartItem(index: number) {
const alert: Alert = {
title: 'Produkt aus Warenkorb entfernt'
};
this.cartItems.splice(index, 1);
localforage.setItem('cart-items', this.cartItems);
this.calculateSum();
this.service.showSuccess(alert);
}
}
<div class="row m-5 p-5">
<div class="col text-center">
<h1>{{ 'LABELS.SHOPPING_CART' | translate }}</h1>
<!--
Symbole:
Papierkorb: <i class="fa fa-trash" aria-hidden="true"></i>
Drucker: <i class="fa fa-print" aria-hidden="true"></i>
Download: <i class="fa fa-download" aria-hidden="true"></i>
Plus: <i class="fa fa-plus-circle" aria-hidden="true"></i>
-->
<br/>
<table class="table table-striped table-responsive-sm">
<!-- Table headings -->
<thead id="sum_row" class="indigo">
<tr>
<th scope="col">{{ 'NAV.CART_PICTURE' | translate }}</th>
<th scope="col">{{ 'NAV.CART_TYPE' | translate }}</th>
<th scope="col">{{ 'NAV.CART_SIZE' | translate }}</th>
<th scope="col">{{ 'NAV.CART_QUANTITY' | translate }}</th>
<th scope="col">{{ 'NAV.CART_PRICE' | translate }}</th>
<th scope="col"></th>
</tr>
</thead>
<!-- END table headings-->
<tbody>
<!-- dynamic table row that iterates through shoppingCartItems array -->
<tr *ngFor="let cartItem of cartItems; let i = index">
<td class="vertical-mid">
<img [src]="cartItem.thumbnail" class="img-thumbnail mr-2" />
<span data-toggle="tooltip" data-placement="right" title="{{cartItem.eventname}}">{{cartItem.name}}</span>
</td>
<td class="vertical-mid">
<i *ngIf="cartItem.itemType === checkType.PRINT" class="fa fa-print fa-2x" aria-hidden="true"></i>
<i *ngIf="cartItem.itemType === checkType.DOWNLOAD" class="fa fa-download fa-2x" aria-hidden="true"></i>
</td>
<td class="vertical-mid">{{cartItem.format}}</td>
<td class="vertical-mid">
<i *ngIf="cartItem.itemType === checkType.PRINT" class="fa fa-chevron-left fa-lg clickable pl-2 pr-2" style="color: #e91e63"
aria-hidden="true" (click)="decreaseQuantity(cartItem)"></i>
<span>{{cartItem.amount}}</span>
<i *ngIf="cartItem.itemType === checkType.PRINT" class="fa fa-chevron-right fa-lg clickable pl-2 pr-2" style="color: #e91e63"
aria-hidden="true" (click)="increaseQuantity(cartItem)"></i>
</td>
<td class="vertical-mid">{{cartItem.totalPrice | number:'0.2-2' | replace:'.':','}}€</td>
<td class="vertical-mid">
<i class="fa fa-2x fa-trash clickable clickeffect" style="color: #e91e63" aria-hidden="true" (click)="deleteCartItem(i)"></i>
</td>
</tr>
<!-- END dynamic table row -->
<!-- Sum row -->
<tr class="table-primary" style="background-color: indigo; color: white">
<td></td>
<td></td>
<td></td>
<td>{{ 'LABELS.TOTAL' | translate }}</td>
<td>{{sum | number:'0.2-2' | replace:'.':','}}€</td>
<td></td>
</tr>
<!-- END sum row -->
</tbody>
</table>
<!-- Buttons to previous or next step -->
<div class="row justify-content-end">
<button type="button" class="btn pink waves-light col-sm-3" routerLink="/search-events" mdbRippleRadius>{{ 'BUTTONS.CONTINUE_SHOPPING' | translate }}</button>
<button type="button" class="btn pink waves-light col-sm-3" routerLink="/checkout" mdbRippleRadius>{{ 'BUTTONS.PROCEED_TO_CHECKOUT' | translate }}</button>
</div>
<!-- END buttons -->
</div>