### 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[]` </bcode>`
Basket products

Public totalPrice
`totalPrice: `number` </bcode>`
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(private basketService: BasketService, private zone: NgZone, private router: Router) {
        this.basketService.products.subscribe(data => {
          let products = JSON.parse(JSON.stringify(data));
          let connectedProducts = [];
          this.totalPrice = this.basketService.totalPrice;
          for(let index = 0; index < products.length; index++){
            let product = products[index];
            if(connectedProducts.length == 0){
              product.items = 1;
              connectedProducts.push(product);
            } else {
              let connectedProductsLen = connectedProducts.length;
              let wasFound = false;
              for(let i = 0; i < connectedProductsLen; i++){
                let connectedProduct = connectedProducts[i];
                if(connectedProduct.fullId === product.fullId){
                  connectedProducts[i].items++;
                  wasFound = true;
                  break;
                }
              }
              if(!wasFound){
                product.items = 1;
                connectedProducts.push(product);
              }

            }
          }
          console.log(connectedProducts);
          this.zone.run(() => {
            this.products = connectedProducts;
          });
        });
        this.basketService.updateProducts();
       }

      ngOnInit() {

      }

      /**
       *
       * @param item basketProduct
       */
      removeProduct(item){
        this.basketService.removeProduct(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 ""