All files / src/app/extensions/wishlists/facades wishlists.facade.ts

18.51% Statements 5/27
37.5% Branches 3/8
0% Functions 0/14
20% Lines 5/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 908x 8x   8x         8x                                   8x                                                                                                                              
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map, startWith, withLatestFrom } from 'rxjs/operators';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
 
import { Wishlist, WishlistHeader } from '../models/wishlist/wishlist.model';
import {
  addProductToNewWishlist,
  addProductToWishlist,
  createWishlist,
  deleteWishlist,
  getAllWishlists,
  getAllWishlistsItemsSkus,
  getPreferredWishlist,
  getSelectedWishlistDetails,
  getWishlistsError,
  getWishlistsLoading,
  moveItemToWishlist,
  removeItemFromWishlist,
  updateWishlist,
} from '../store/wishlist';
 
/* eslint-disable @typescript-eslint/member-ordering */
@Injectable({ providedIn: 'root' })
export class WishlistsFacade {
  constructor(private store: Store) {}
 
  wishlists$: Observable<Wishlist[]> = this.store.pipe(select(getAllWishlists));
  currentWishlist$: Observable<Wishlist> = this.store.pipe(select(getSelectedWishlistDetails));
  preferredWishlist$: Observable<Wishlist> = this.store.pipe(select(getPreferredWishlist));
  allWishlistsItemsSkus$: Observable<string[]> = this.store.pipe(select(getAllWishlistsItemsSkus));
  wishlistLoading$: Observable<boolean> = this.store.pipe(select(getWishlistsLoading));
  wishlistError$: Observable<HttpError> = this.store.pipe(select(getWishlistsError));
 
  wishlistSelectOptions$(filterCurrent = true) {
    return this.wishlists$.pipe(
      startWith([] as Wishlist[]),
      map(wishlists =>
        wishlists.map(wishlist => ({
          value: wishlist.id,
          label: wishlist.title,
        }))
      ),
      withLatestFrom(this.currentWishlist$),
      map(([wishlistOptions, currentWishlist]) => {
        Iif (filterCurrent && currentWishlist) {
          return wishlistOptions.filter(option => option.value !== currentWishlist.id);
        }
        return wishlistOptions;
      })
    );
  }
 
  addWishlist(wishlist: WishlistHeader): void | HttpError {
    this.store.dispatch(createWishlist({ wishlist }));
  }
 
  deleteWishlist(id: string): void {
    this.store.dispatch(deleteWishlist({ wishlistId: id }));
  }
 
  updateWishlist(wishlist: Wishlist): void {
    this.store.dispatch(updateWishlist({ wishlist }));
  }
 
  addProductToNewWishlist(title: string, sku: string): void {
    this.store.dispatch(addProductToNewWishlist({ title, sku }));
  }
 
  addProductToWishlist(wishlistId: string, sku: string, quantity?: number): void {
    this.store.dispatch(addProductToWishlist({ wishlistId, sku, quantity }));
  }
 
  moveItemToWishlist(sourceWishlistId: string, targetWishlistId: string, sku: string): void {
    this.store.dispatch(
      moveItemToWishlist({ source: { id: sourceWishlistId }, target: { id: targetWishlistId, sku } })
    );
  }
 
  moveItemToNewWishlist(sourceWishlistId: string, title: string, sku: string): void {
    this.store.dispatch(moveItemToWishlist({ source: { id: sourceWishlistId }, target: { title, sku } }));
  }
 
  removeProductFromWishlist(wishlistId: string, sku: string): void {
    this.store.dispatch(removeItemFromWishlist({ wishlistId, sku }));
  }
}