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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 8x 7x 7x 3x 3x 3x 4x 4x 4x 7x 7x 2x 2x | import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map, startWith, switchMap, tap, withLatestFrom } from 'rxjs/operators';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { WishlistSharing } from '../models/wishlist-sharing/wishlist-sharing.model';
import { Wishlist, WishlistHeader } from '../models/wishlist/wishlist.model';
import {
addProductToNewWishlist,
addProductToWishlist,
createWishlist,
deleteWishlist,
getAllWishlists,
getAllWishlistsItemsSkus,
getPreferredWishlist,
getSelectedWishlistDetails,
getSharedWishlist,
getWishlistDetails,
getWishlistsError,
getWishlistsLoading,
moveItemToWishlist,
removeItemFromWishlist,
updateWishlist,
wishlistActions,
} from '../store/wishlist';
@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));
wishlistLoading$: Observable<boolean> = this.store.pipe(select(getWishlistsLoading));
wishlistError$: Observable<HttpError> = this.store.pipe(select(getWishlistsError));
sharedWishlist$: Observable<Wishlist> = this.store.pipe(select(getSharedWishlist));
/**
* Emits the unique product SKUs of wishlist items and triggers loading the item details
* of wishlists whose details have not been loaded yet.
* If a `wishlistId` is given, only that wishlist is considered, otherwise all wishlists are used.
*/
wishlistItemsSkus$(wishlistId?: string): Observable<string[]> {
const requestedDetails = new Set<string>();
if (wishlistId) {
return this.store.pipe(
select(getWishlistDetails(wishlistId)),
tap(wishlist => this.loadMissingWishlistDetails(wishlist ? [wishlist] : [], requestedDetails)),
map(wishlist => wishlist?.items?.map(item => item.sku) ?? [])
);
}
return this.wishlists$.pipe(
tap(wishlists => this.loadMissingWishlistDetails(wishlists, requestedDetails)),
switchMap(() => this.store.pipe(select(getAllWishlistsItemsSkus)))
);
}
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]) => {
if (filterCurrent && currentWishlist) {
return wishlistOptions.filter(option => option.value !== currentWishlist.id);
}
return wishlistOptions;
})
);
}
addWishlist(wishlist: WishlistHeader): HttpError | void {
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 }));
}
shareWishlist(wishlistId: string, wishlistSharing: WishlistSharing): void {
this.store.dispatch(wishlistActions.shareWishlist({ wishlistId, wishlistSharing }));
}
unshareWishlist(wishlistId: string): void {
this.store.dispatch(wishlistActions.unshareWishlist({ wishlistId }));
}
private loadMissingWishlistDetails(wishlists: Wishlist[], requestedDetails: Set<string>): void {
// only wishlists without a loaded items attribute need their details fetched
wishlists
.filter(wishlist => !wishlist.items && !requestedDetails.has(wishlist.id))
.forEach(wishlist => {
requestedDetails.add(wishlist.id);
this.store.dispatch(wishlistActions.loadWishlistDetails({ wishlistId: wishlist.id }));
});
}
}
|