import { Injectable } from '@angular/core' ; import { WindowRefService } from './window-ref.service' ; @ Injectable ( ) export class LocalCartService { private _window : Window ; constructor ( windowRef ) { this . _window = windowRef . nativeWindow ; } /**
     * clearAll()
     * Clears:
     * - localstorage.cart
     * - localstorage.order
     */ public clearAll ( ) { this . clearCart ( ) ; this . clearOrder ( ) ; } /**
     * clearCart()
     *
     * clears localstorage cart
     */ public clearCart ( ) { this . _window . localStorage . setItem ( 'cart' , null ) ; } /**
     * cartHasItems()
     * Returns boolean, if localstorage has items stored in cart.
     */ public cartHasItems ( ) { return ( this . _window . localStorage . getItem ( 'cart' ) !== null ) ; } /**
     * cartGetItems()
     * returns json object of all items in localstorage cart
     */ public cartGetItems ( ) { if ( this . cartHasItems ( ) ) { let cart = this . _window . localStorage . getItem ( 'cart' ) ; cart = JSON . parse ( cart ) ; return cart ; } return null ; } /**
     * clearOrder()
     *
     * Clears localstorage order
     */ public clearOrder ( ) { this . _window . localStorage . setItem ( 'order' , null ) ; } param items public cartUpdateItems ( items ) { const itemStr = JSON . stringify ( items ) ; this . _window . localStorage . setItem ( 'cart' , itemStr ) ; } /**
     * orderHasItems()
     * * Returns boolean, if localstorage has items stored in order.
     */ public orderHasItems ( ) { return ( this . _window . localStorage . getItem ( 'order' ) !== null && this . _window . localStorage . getItem ( 'order' ) !== 'null' ) ; } param key public orderHas ( key ) { if ( this . orderHasItems ( ) && key ) { let order = this . _window . localStorage . getItem ( 'order' ) ; order = JSON . parse ( order ) ; return ( order [ key ] !== null ) ; } return false ; } /**
     * orderGetItems()
     * returns json object of all items in localstorage order
     */ public orderGetItems ( ) { if ( this . orderHasItems ( ) ) { let order = this . _window . localStorage . getItem ( 'order' ) ; order = JSON . parse ( order ) ; return order ; } return null ; } param items public orderUpdateItems ( items ) { const itemStr = JSON . stringify ( items ) ; this . _window . localStorage . setItem ( 'order' , itemStr ) ; } }