import { Component, OnInit, Input, Output, EventEmitter, OnDestroy, } from '@angular/core'; import { SettingsService } from './settings.service'; import { BomMessage, MessageBusService, SBOSettings, Setting, BomService, BomModel, SettingOption, DialogService, AnalyticsService, AnalyticsEvent, PaginationParamsModel, } from '@arrow/bom/core'; import { ConfigService } from '@arrow/bom/config'; import { TranslateService } from '@ngx-translate/core'; import { Subscription } from 'rxjs'; import { FeatureFlagService } from '@arrow/bom/feature-flag'; @Component({ selector: 'bom-settings', templateUrl: './settings.component.html', styleUrls: ['./settings.component.scss'] }) export class SettingsComponent implements OnInit, OnDestroy { @Input() disabled: boolean; @Input() bomData: BomModel; @Input() bom: any; @Input() bomStub: any; @Input() totalParts: number; @Input() bomLoading: any; @Input() isAnonymous: any; @Input() showSettings: any; @Input() isSelfOwned: any; @Input() paginationParams: PaginationParamsModel; @Output() saved: EventEmitter = new EventEmitter(); @Output() deleteBom: EventEmitter = new EventEmitter(); settings: SBOSettings; private _subscriptions: Subscription[] = []; public target; public changeCount: number = 0; public showRegionsInMyarrow: boolean; constructor( private _settingsService: SettingsService, private _messageBus: MessageBusService, private _config: ConfigService, private _dialogService: DialogService, private _analyticsService: AnalyticsService, private _bomService: BomService, private _translate: TranslateService, private _featureFlagService: FeatureFlagService ) {} ngOnInit() { this.showRegionsInMyarrow = this._featureFlagService.showregionsinmyarrow; this.target = this._config.getTarget(); this.settings = this._settingsService.getSettings(); this._subscriptions.push( this._messageBus .from('ch:sbo') .subscribe( (message: BomMessage) => (this.settings = this._settingsService.getSettings()) ) ); } ngOnDestroy() { this._subscriptions.forEach(s => s.unsubscribe()); } /** * apply selected options * @param setting * @param option * @param disabled */ updateOption(setting: Setting, option: SettingOption, disabled: boolean) { if (disabled) return; if (setting.mutuallyExclusiveOptions) { setting.options.forEach((_option: SettingOption) => { if (_option === option) { _option.isActive = true; } else { _option.isActive = false; } }); } else { option.isActive = !option.isActive; } } /** * @param settings */ save(settings: SBOSettings) { this.saved.emit(this._settingsService.saveSettings(settings, this.paginationParams)); } /** * close settings */ closeSettings() { this.saved.emit(false); } /** * open modal to delete a bom * @param bom * @param bomData */ openDeleteBomModal(bom: any, bomData: BomModel): void { this._dialogService .confirm( this._translate.instant('Delete-this-BOM?'), this._translate.instant('Are-you-sure?-This-cannot-be-undone.'), {} ) .subscribe((response: boolean) => { if (response && bom.id) { this._bomService.deleteBom(bomData.id).subscribe( () => { this._analyticsService.googleTrackEvent(AnalyticsEvent.CLICK, { category: 'BOM', action: 'View Bom Delete', label: bomData.id }); this.deleteBom.emit(true); }, (error: string) => { this._messageBus.to('ch:loader', { payload: false }); this._dialogService.alert( this._translate.instant('Error'), error ); } ); } }); } }