All files / src/app/extensions/wishlists/store/wishlist wishlist.selectors.ts

100% Statements 29/29
75% Branches 3/4
100% Functions 13/13
100% Lines 18/18

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 4210x 10x   10x     10x   10x   33x   10x   10x   13x   10x   10x   10x     2x     10x   10x           10x 10x   4x    
import { createSelector, createSelectorFactory, resultMemoize } from '@ngrx/store';
import { uniq } from 'lodash-es';
 
import { isArrayEqual } from 'ish-core/utils/functions';
 
import { Wishlist } from '../../models/wishlist/wishlist.model';
import { getWishlistsState } from '../wishlists-store';
 
import { initialState, wishlistsAdapter } from './wishlist.reducer';
 
const getWishlistState = createSelector(getWishlistsState, state => (state ? state.wishlists : initialState));
 
const { selectEntities, selectAll } = wishlistsAdapter.getSelectors(getWishlistState);
 
export const getAllWishlists = selectAll;
 
export const getWishlistsLoading = createSelector(getWishlistState, state => state.loading);
 
export const getWishlistsError = createSelector(getWishlistState, state => state.error);
 
export const getSelectedWishlistId = createSelector(getWishlistState, state => state.selected);
 
export const getSelectedWishlistDetails = createSelector(
  selectEntities,
  getSelectedWishlistId,
  (entities, id): Wishlist => id && entities[id]
);
 
export const getWishlistDetails = (id: string) => createSelector(selectEntities, entities => entities[id]);
 
export const getPreferredWishlist = createSelector(getAllWishlists, entities => entities.find(e => e.preferred));
 
/**
 * Gets all unique items from all wishlists
 * Returns an array of the wishlist item product SKUs
 */
export const getAllWishlistsItemsSkus = createSelectorFactory<object, string[]>(projector =>
  resultMemoize(projector, isArrayEqual)
)(getAllWishlists, (wishlists: Wishlist[]): string[] =>
  uniq(wishlists.map(wishlist => wishlist.items.map(items => items.sku)).flat())
);