import { Component, Input, OnInit, OnDestroy, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { DownloadBomComponent } from '../download-bom/download-bom.component'; import { ShareBomComponent } from '../share-bom/share-bom.component'; import { ConfigService } from '@arrow/bom/config'; import { Cart2Service } from '@arrow/bom/cart2'; // Core import { SocketsService, DialogService, BomService, BomItemService, BomModel, AnalyticsService, ItemSelection, MessageBusService, AnalyticsEvent, ApiSocketListenAction, PaginationParamsModel } from '@arrow/bom/core'; // Warpaint import { WptDialog } from '@arrow/warpaint/dialog'; import { take, takeUntil } from 'rxjs/operators'; import { AskEngineerModalComponent } from '../ask-engineer-modal/ask-engineer-modal.component'; import { Subject } from 'rxjs'; @Component({ selector: 'bom-actions', templateUrl: 'actions.component.html', styleUrls: ['actions.component.scss'] }) export class ActionsComponent implements OnInit, OnDestroy { private _killSig$$: Subject = new Subject(); @Input() bomData: BomModel; @Input() userSettings: any; @Input() isAnonymous: boolean; @Input() isOwner: boolean = false; @Input() bomLoading: boolean; @Input() quantityConflictCount: number; @Input() paginationParams: PaginationParamsModel; @Output() delete: EventEmitter = new EventEmitter(); @Output() deleteBom: EventEmitter = new EventEmitter(); @ViewChild('askEngineerBtn', { read: ElementRef }) askEngineerBtn: ElementRef; subscriptions: Array = []; downloadBomTooltipMsg: string = this._translate.instant('Download-BOM'); isAnyPartSelected: boolean = false; constructor( private _sockets: SocketsService, private _bomService: BomService, private _router: Router, private _route: ActivatedRoute, private _dialog: WptDialog, private _dialogService: DialogService, private _bomItemService: BomItemService, private _analyticsService: AnalyticsService, private _translate: TranslateService, private _messageBus: MessageBusService, private _configService: ConfigService, private _cart2: Cart2Service, ) {} ngOnInit() { this._bomService.focusAskEngineerBtn.subscribe( () => { this.askEngineerBtn?.nativeElement?.focus(); } ); this._sockets .observeAction(ApiSocketListenAction.CURRENT_PERMISSIONS) .pipe(takeUntil(this._killSig$$)) .subscribe((sharePermissions: any) => { sharePermissions.level = sharePermissions.level.toLowerCase(); this.isOwner = sharePermissions.level === 'own' ? true : false; }); this._bomItemService.selectionChanged$ .pipe(takeUntil(this._killSig$$)) .subscribe((selection: any) => { if (selection.hasPartialSelection !== undefined) { this.downloadBomTooltipMsg = selection.hasPartialSelection ? this._translate.instant('Download-Selected') : this._translate.instant('Download-BOM'); } }); this._bomItemService.selectionChanged$ .pipe(takeUntil(this._killSig$$)) .subscribe((selection: ItemSelection) => { this.isAnyPartSelected = (this._bomItemService.getSelection().selectionInverted && this._bomItemService.getSelection().rowIds.length >= 0) || (!this._bomItemService.getSelection().selectionInverted && this._bomItemService.getSelection().rowIds.length > 0); }); } ngOnDestroy() { this._killSig$$.next(); this._killSig$$.complete(); } /** * Shows the copy BOM modal on copy icon click. * @param bomLoading * @param bomData * @param isAnonymous */ openCopyBomModal( bomLoading: boolean, bomData: BomModel, isAnonymous: boolean ): void { if (isAnonymous || bomLoading) return; this._dialogService .prompt( this._translate.instant('Duplicate-this-BOM'), this._translate.instant('{BOM_NAME}-Copy', { BOM_NAME: bomData.name }), this._translate.instant('BOM-name'), this._translate.instant('Duplicate-BOM-placeholder'), this._translate.instant('Duplicate-BOM-aria-label') ) .subscribe((value: string | boolean) => { if (typeof value !== 'string') return; this._analyticsService.googleTrackEvent(AnalyticsEvent.CLICK, { category: 'BOM', action: 'Duplicate', label: bomData.id }); const bomCopy = Object.assign({}, bomData); bomCopy.name = value; this.copyBom(bomCopy); }); } /** * Opens the export column modal so the user can select which * columns they want exported in their downloaded xl spreadsheet * @param bomLoading * @param bomData */ openDownloadBomModal( bomLoading: boolean, bomData: BomModel, userSettings: any ): void { if (bomLoading) return; this._dialog.open(DownloadBomComponent, { // Ensure modal window content is scrollable on smaller screen resolutions (i.e. 320px) panelClass: 'bom-download-cdk-overlay-pane', // See _normalize.scss maxWidth: '795px', data: { bomData, userSettings } }); } /** *Opens modal to share bom so user can share a bom with another user * only owner users can share boms * @param bomLoading * @param isOwner * @param isAnonymous * */ openShareBomModal( bomLoading: boolean, isOwner: boolean, isAnonymous: boolean ): void { if (bomLoading || !isOwner || isAnonymous) return; this._dialog.open(ShareBomComponent, { minHeight: 275, data: { bom: this.bomData } }); } askEngineerModal() { this._dialog.open(AskEngineerModalComponent, { width: '420px' }); } private copyBom(item: BomModel): void { if (item) { this._bomService .copyBom(item.id, item.name) .pipe(take(1)) .subscribe( response => { const path = this._configService.getTarget() === 'arrowcom' && this._configService.getDomain() === 'arrow.com' ? '../../../view' : '../../../bom/view'; this._router.navigate([path, response.payload.bomId, 1, 25]); }, () => { this._dialogService.alert( 'An error has occurred', 'Unable to copy BOM' ); } ); } } deleteSelected() { const selection: ItemSelection = this._bomItemService.getSelection(); // delete BOM if user select all parts if ( (selection.selectionInverted && selection.rowIds.length === 0) || (!selection.selectionInverted && selection.rowIds.length === this.bomData.rows.length) ) { this._dialogService .confirm( this._translate.instant('Delete-this-BOM?'), this._translate.instant('Are-you-sure?-This-cannot-be-undone.'), {} ) .subscribe((response: boolean) => { if (response && this.bomData.id) { this._bomService.deleteBom(this.bomData.id).subscribe( () => { this.deleteBom.emit(true); }, (error: string) => { this._messageBus.to('ch:loader', { payload: false }); this._dialogService.alert( this._translate.instant('Error'), error ); } ); } }); } if ( !selection.selectionInverted && selection.rowIds.length > 0 && selection.rowIds.length < this.bomData.rows.length ) { //delete selected rows this._dialogService .confirm( this._translate.instant('Delete-Selected'), this._translate.instant('Are-you-sure-you-want-to-delete-parts', { COUNT: selection.rowIds.length }), { style: 'warning', buttons: { cancel: this._translate.instant('Cancel'), confirm: this._translate.instant('Delete-Selected') } } ) .subscribe(clickedYes => { if (clickedYes) { this._bomItemService.deleteMultipleConfirmed(this.bomData); this.delete.emit(null); } }); } if (selection.selectionInverted && selection.rowIds.length > 0) { this._dialogService .confirm( this._translate.instant('Delete-Selected'), this._translate.instant('Are-you-sure-you-want-to-delete-parts', { COUNT: this.bomData.rows.length - selection.rowIds.length }), { style: 'warning', buttons: { cancel: this._translate.instant('Cancel'), confirm: this._translate.instant('Delete-Selected') } } ) .subscribe(clickedYes => { if (clickedYes) { this._bomItemService.deleteMultipleConfirmed(this.bomData); this.delete.emit(null); } }); } } openAutoRoundModal() { const selection = this._bomItemService.getSelection(); this._cart2 .autoRound(this.bomData.id, selection, this.bomData.parts, this.paginationParams, 'Auto Round MOQ') .subscribe(() => this.bomLoading = false); } }