All files / src/app/extensions/wishlists/pages/account-wishlist-detail account-wishlist-detail-page.component.ts

50% Statements 12/24
0% Branches 0/4
22.22% Functions 2/9
50% Lines 11/22

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 741x 1x       1x   1x                     1x                   1x   1x     1x 1x 1x   1x                                                                        
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, ViewChild, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Observable } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { WishlistsFacade } from '../../facades/wishlists.facade';
import { WishlistSharing } from '../../models/wishlist-sharing/wishlist-sharing.model';
import { Wishlist, WishlistItem } from '../../models/wishlist/wishlist.model';
import { SelectWishlistModalComponent } from '../../shared/select-wishlist-modal/select-wishlist-modal.component';
 
@Component({
  selector: 'ish-account-wishlist-detail-page',
  standalone: false,
  templateUrl: './account-wishlist-detail-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AccountWishlistDetailPageComponent implements OnInit {
  wishlist$: Observable<Wishlist>;
  wishlistError$: Observable<HttpError>;
  wishlistLoading$: Observable<boolean>;
 
  @ViewChild('moveDialog') moveDialog: SelectWishlistModalComponent;
 
  private currentWishlist: Wishlist;
  moveItem: WishlistItem;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(private wishlistsFacade: WishlistsFacade) {}
 
  ngOnInit() {
    this.wishlist$ = this.wishlistsFacade.currentWishlist$;
    this.wishlistLoading$ = this.wishlistsFacade.wishlistLoading$;
    this.wishlistError$ = this.wishlistsFacade.wishlistError$;
 
    this.wishlist$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(wishlist => {
      this.currentWishlist = wishlist;
      this.moveItem = undefined;
    });
  }
 
  editPreferences(wishlist: Wishlist, wishlistName: string) {
    this.wishlistsFacade.updateWishlist({
      ...wishlist,
      id: wishlistName,
    });
  }
 
  unshareWishlist(wishlistId: string) {
    this.wishlistsFacade.unshareWishlist(wishlistId);
  }
 
  shareWishlist(wishlistSharing: WishlistSharing, wishlistId: string) {
    this.wishlistsFacade.shareWishlist(wishlistId, wishlistSharing);
  }
 
  showMoveDialog(wishlistItemId: string) {
    this.moveItem = this.currentWishlist.items.find(item => item.id === wishlistItemId);
    if (this.moveItem) {
      this.moveDialog.show();
    }
  }
 
  moveItemToOtherWishlist(wishlistMoveData: { id: string; title: string }) {
    if (wishlistMoveData.id) {
      this.wishlistsFacade.moveItemToWishlist(this.currentWishlist.id, wishlistMoveData.id, this.moveItem.sku);
    } else {
      this.wishlistsFacade.moveItemToNewWishlist(this.currentWishlist.id, wishlistMoveData.title, this.moveItem.sku);
    }
  }
}