import { Component, OnInit } from "@angular/core"; import Utils from "../../shared/utils"; import { User } from "../../shared/modal/user"; import { SharedService } from "../../shared/shared.service"; import { UserService } from "../../services/user.service"; import { alertErrorKeys } from "../../shared/form.constants"; declare var $: any; @Component({ moduleId: module.id, selector: "error-cmp", templateUrl: "error.component.html", styleUrls: ["error.component.css"], }) export class ErrorComponent implements OnInit { private COMPONENT_NAME: string = "ErrorComponent"; user: User; errors: any; errorMessage: string; displayAlertMessage: boolean = false; constructor( public sharedService: SharedService, public userService: UserService ) {} /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated * @returns void */ ngOnInit(): void { const METHOD_NAME: string = "ngOnInit()"; this.user = this.userService.getUser(); this.sharedService.errorMessage.subscribe((val: any) => { this.errors = null; this.errorMessage = null; try { if (val && typeof val === "string") { this.errors = val; if (alertErrorKeys.indexOf(this.errors) > -1) { this.displayAlertMessage = true; } else { this.displayAlertMessage = false; } this.errorMessage = Utils.getErrorValue(val); if (this.errorMessage === undefined) { this.errorMessage = Utils.getErrorKey(val); } if (val.indexOf(" {") !== -1) { // console.log(val.substring(val.indexOf(' {'), val.lastIndexOf('}')+1)); let error: any; const parseErrors: any = JSON.parse( val.substring(val.indexOf(" {"), val.lastIndexOf("}") + 1) ); if (parseErrors) { // error handing for commerce services if (parseErrors.errors && parseErrors.errors.length > 0) { error = parseErrors.errors[0]; //Added this for checking of alert message from API response if (alertErrorKeys.indexOf(error.errorKey) > -1) { this.displayAlertMessage = true; } this.errorMessage = Utils.getErrorValue(error.errorKey); if ( error.errorKey === "_ERR_ORDER_CREDIT_CHECK_FAILED" && error.errorParameters ) { const errorParam = JSON.parse(error.errorParameters[0]); this.errorMessage += " " + errorParam.errorMessage; } if ( error.errorKey === "_ERR_INCOMPATIBLE_ITEM_IN_PACKAGE" && error.errorParameters ) { const errorParam = JSON.parse(error.errorParameters[0]); if (errorParam && errorParam.incompatibleCatalogEntryId) { if (errorParam.itemType.toLowerCase() === "feature") { this.errorMessage = Utils.getConflictsMsg( "_CONFLICT_ITEM", errorParam.name ); } else if (errorParam.itemType.toLowerCase() === "device") { this.errorMessage = Utils.getConflictsMsg( "_CONFLICT_ITEM_DEVICE", errorParam.name ); } else if ( errorParam.itemType.toLowerCase() === "accessory" ) { this.errorMessage = Utils.getConflictsMsg( "_CONFLICT_ITEM_ACC", errorParam.name ); } } } } else { // error handing for non commerce services // console.log('parseErrors'+ parseErrors) ; if (parseErrors.errorCode && parseErrors.errorMessage) { try { const toggleErrors: any = JSON.parse( parseErrors.errorMessage ); if (toggleErrors.errors && toggleErrors.errors.length > 0) { error = toggleErrors.errors[0]; if (error.systemMessage && error.userMessage) { this.errorMessage = error.systemMessage + ":" + error.userMessage; } } else { this.errorMessage = JSON.stringify(toggleErrors); } } catch (err) { if (parseErrors.errorCode) { this.errorMessage = Utils.getErrorContains( parseErrors.errorCode ); } } } } } } if (!this.errorMessage) { this.errorMessage = Utils.getErrorValue("_ERR_GENERIC"); } } } catch (err) { if (!this.errorMessage) { this.errorMessage = Utils.getErrorValue("_ERR_GENERIC"); } } }); $(".main-panel").scrollTop(0); } /** * This function is used to close error message dialog box. * @returns void */ closeError(): void { const METHOD_NAME: string = "closeError()"; this.sharedService.updateErrorMessage(null); } }