File

modules/basket-component/src/basket.component.ts

Description

Represent store basket. Inject basket service that get methods and data to work with database

Metadata

selector app-basket
styleUrls basket.component.scss
templateUrl basket.component.html

Constructor

constructor(basketService: any, zone: any, router: any)

Methods

removeProduct
removeProduct(item: any)
Parameters :
  • item

    basketProduct

Returns: void
buy
buy()

Redirect to payment page

Returns: void
incItems
incItems(productIndex: any)

Increment number of specific product

Parameters :
  • productIndex

    index of product in array

Returns: void
decItems
decItems(productIndex: any)

Decrement number of specific product

Parameters :
  • productIndex

    index of product in array

Returns: void

Properties

Public products
products: any[]

Basket products

Public totalPrice
totalPrice: number
Default value: 0

Total products price

import { BasketService } from '@nodeart/basketservice/index';
import { DbAbstractionLayer } from '@nodeart/dal/index';
import {Component, OnInit, OnChanges, SimpleChanges, NgZone} from '@angular/core';
import { Router } from '@angular/router';

/**
 * Represent store basket. Inject basket service that get methods and data to work with database 
 */
@Component({
  selector: 'app-basket',
  templateUrl: './basket.component.html',
  styleUrls: ['./basket.component.scss']
})
export class BasketComponent implements OnInit {

  /**
   * Basket products
   */
  public products = [];

  /**
   * Total products price
   */
  public totalPrice = 0;
  constructor(protected basketService: BasketService, protected zone: NgZone, protected router: Router) {
    this.basketService.products.subscribe(data => {
      let products = JSON.parse(JSON.stringify(data));
      this.totalPrice = this.basketService.totalPrice;
      this.zone.run(() => {
        this.products = products;
      });
    });
    this.basketService.updateProducts();
   }

  ngOnInit() {
    
  }

  /**
   * 
   * @param item basketProduct
   */
  removeProduct(item){
    this.basketService.removeAllProductItems(item);
  }

  /**
   * Redirect to payment page
   */
  buy(){
    this.router.navigate(['payments']);
  }

  /**
   * Increment number of specific product
   * @param productIndex index of product in array
   */
  incItems(productIndex) {
    let newProduct = this.products[productIndex];
    let newProductObj = {
      id: newProduct.id,
      fullId: newProduct.fullId,
      attributes: []
    };
    if(newProduct.attributes){
      newProductObj.attributes = newProduct.attributes;
    }
    this.basketService.addProduct(newProductObj);
  }

  /**
   * Decrement number of specific product
   * @param productIndex index of product in array
   */
  decItems(productIndex) {
    this.basketService.removeProduct(this.products[productIndex]);
  }


}

results matching ""

    No results matching ""