Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { EmailModel } from '../rfai-admin.model';
import { Subscription } from 'rxjs/Subscription';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { RfaiAdminService } from '../rfai-admin.service';
import { AuthenticationService } from '../../../auth/auth.service';
import { initialValueChanged } from '../initial-value-changed.directive';
import {
CanDeactivateGuard,
CanComponentDeactivate
} from '../../../shared/guards/can-deactivate-gaurd';
@Component({
selector: 'app-email-notifications',
templateUrl: './email-notifications.component.html',
styleUrls: ['./email-notifications.component.scss']
})
export class EmailNotificationsComponent
implements OnInit, OnDestroy, CanComponentDeactivate {
constructor(
private rfaiAdminService: RfaiAdminService,
private fb: FormBuilder,
private canDeactivateGuard: CanDeactivateGuard,
private authenticationService: AuthenticationService
) {}
subscription: Subscription = new Subscription();
submittedEmail: EmailModel = new EmailModel();
errorEmail: EmailModel = new EmailModel();
abandonedEmail: EmailModel = new EmailModel();
closedEmail: EmailModel = new EmailModel();
manuallyClosedEmail: EmailModel = new EmailModel();
submittedEmailForm: FormGroup;
errorEmailForm: FormGroup;
abandonedEmailForm: FormGroup;
closedEmailForm: FormGroup;
manuallyClosedForm: FormGroup;
initalObject: EmailModel = new EmailModel();
successMsg = '';
errorMsg = '';
errorStatus = false;
successStatus = false;
discardChangesModalClosed = true;
discardChangesSubject: Subject<boolean> = new Subject<boolean>();
changesSaved = false;
tabIdx = '0';
ngOnInit() {
this.formInit();
this.onChanges();
}
formInit() {
this.submittedEmailForm = this.fb.group({
submittedEmailHeading: [this.submittedEmail.heading, Validators.required],
submittedEmailMessage: [this.submittedEmail.message, Validators.required]
});
this.errorEmailForm = this.fb.group({
errorEmailHeading: [this.errorEmail.heading, Validators.required],
errorEmailMessage: [this.errorEmail.message, Validators.required]
});

this.abandonedEmailForm = this.fb.group({
abandonedEmailHeading: [this.abandonedEmail.heading, Validators.required],
abandonedEmailMessage: [this.abandonedEmail.message, Validators.required]
});

this.closedEmailForm = this.fb.group({
closedEmailHeading: [this.closedEmail.heading, Validators.required],
closedEmailMessage: [this.closedEmail.message, Validators.required]
});

this.manuallyClosedForm = this.fb.group({
manuallyClosedEmailHeading: [
this.manuallyClosedEmail.heading,
Validators.required
],
manuallyClosedEmailMessage: [
this.manuallyClosedEmail.message,
Validators.required
]
});
this.getEmailData();
}

onChanges() {
this.manuallyClosedForm.statusChanges.subscribe(data => {
if (data === 'VALID') {
this.canDeactivateGuard.setCanLogout(false);
this.changesSaved = false;
} else {
this.canDeactivateGuard.setCanLogout(true);
}
});
this.closedEmailForm.statusChanges.subscribe(data => {
if (data === 'VALID') {
this.canDeactivateGuard.setCanLogout(false);
this.changesSaved = false;
} else {
this.canDeactivateGuard.setCanLogout(true);
}
});
this.abandonedEmailForm.statusChanges.subscribe(data => {
if (data === 'VALID') {
this.canDeactivateGuard.setCanLogout(false);
this.changesSaved = false;
} else {
this.canDeactivateGuard.setCanLogout(true);
}
});
this.errorEmailForm.statusChanges.subscribe(data => {
if (data === 'VALID') {
this.canDeactivateGuard.setCanLogout(false);
this.changesSaved = false;
} else {
this.canDeactivateGuard.setCanLogout(true);
}
});
this.submittedEmailForm.statusChanges.subscribe(data => {
if (data === 'VALID') {
this.canDeactivateGuard.setCanLogout(false);
this.changesSaved = false;
} else {
this.canDeactivateGuard.setCanLogout(true);
}
});
}
getEmailData() {
this.subscription.add(
this.rfaiAdminService.getEmailTemplates().subscribe(
data => {
data.emailTemplates.forEach(template => {
switch (template.templateName) {
case 'manuallyClosedTemplate':
this.manuallyClosedEmail = template;
this.setCustomValidator(
this.manuallyClosedForm,
'manuallyClosedEmailHeading',
'manuallyClosedEmailMessage',
{ ...template }
);
break;
case 'abandonedTemplate':
this.abandonedEmail = template;
this.setCustomValidator(
this.abandonedEmailForm,
'abandonedEmailHeading',
'abandonedEmailMessage',
{ ...template }
);
break;
case 'completeTemplate':
this.closedEmail = template;
this.setCustomValidator(
this.closedEmailForm,
'closedEmailHeading',
'closedEmailMessage',
{ ...template }
);
break;
case 'errorTemplate':
this.errorEmail = template;
this.setCustomValidator(
this.errorEmailForm,
'errorEmailHeading',
'errorEmailMessage',
{ ...template }
);
break;
case 'submittedTemplate':
this.submittedEmail = template;
this.setCustomValidator(
this.submittedEmailForm,
'submittedEmailHeading',
'submittedEmailMessage',
{ ...template }
);
break;
default:
return;
}
this.manuallyClosedForm.markAsPristine();
this.closedEmailForm.markAsPristine();
this.abandonedEmailForm.markAsPristine();
this.errorEmailForm.markAsPristine();
this.submittedEmailForm.markAsPristine();
});
},
error => {
console.log('error from getEmailData() ', error.statusText);
}
)
);
}
setCustomValidator(
form: FormGroup,
headingName: string,
messageName: string,
initialObject: EmailModel
) {
const formFields = [headingName, messageName];
formFields.forEach(formField => {
form.get(formField).clearValidators();
});
form.setValidators([
initialValueChanged(initialObject, headingName, messageName)
]);
form.updateValueAndValidity();
}

onSubmitEmail(email: EmailModel) {
this.manuallyClosedForm.markAsPristine();
this.closedEmailForm.markAsPristine();
this.abandonedEmailForm.markAsPristine();
this.errorEmailForm.markAsPristine();
this.submittedEmailForm.markAsPristine();
this.rfaiAdminService.setStatus(true, false);
this.rfaiAdminService.setStatus(false, false);
this.changesSaved = true;
email.modifiedBy = this.authenticationService.getDecodedUserInfo().userName;
this.subscription.add(
this.rfaiAdminService.submitEmailTemplate(email).subscribe(
data => {
this.successMsg = 'Updated Email Template successfully.';
this.successStatus = true;
this.rfaiAdminService.setStatus(true, this.successStatus);
this.rfaiAdminService.setMsg(true, this.successMsg);
this.errorStatus = false;
this.rfaiAdminService.setStatus(false, this.errorStatus);
this.getEmailData();
setTimeout(() => {
this.successStatus = false;
}, 8000);
},
error => {
this.errorStatus = true;
this.errorMsg = 'An error occurred updating Email Templates.';

this.rfaiAdminService.setStatus(false, this.errorStatus);
this.rfaiAdminService.setMsg(false, this.errorMsg);

this.successStatus = false;
this.rfaiAdminService.setStatus(true, this.successStatus);

setTimeout(() => {
this.errorStatus = false;
}, 8000);
}
)
);
}

cancelEmailTemplates(emailType: string) {
// this.canDeactivateGuard.setCanLogout(true);
this.rfaiAdminService.setStatus(true, false);
this.rfaiAdminService.setStatus(false, false);
this.subscription.add(
this.rfaiAdminService.getEmailTemplates().subscribe(data => {
if (data.emailTemplates) {
data.emailTemplates.forEach(template => {
switch (template.templateName) {
case 'manuallyClosedTemplate':
this.manuallyClosedForm.reset({
manuallyClosedEmailHeading: template.heading,
manuallyClosedEmailMessage: template.message
});
if (emailType === 'manuallyClosedTemplate') {
this.manuallyClosedEmail = template;
}
break;

case 'abandonedTemplate':
this.abandonedEmailForm.reset({
abandonedEmailHeading: template.heading,
abandonedEmailMessage: template.message
});
if (emailType === 'abandonedTemplate') {
this.abandonedEmail = template;
}
break;

case 'completeTemplate':
this.closedEmailForm.reset({
closedEmailHeading: template.heading,
closedEmailMessage: template.message
});
if (emailType === 'completeTemplate') {
this.closedEmail = template;
}
break;
case 'errorTemplate':
this.errorEmailForm.reset({
errorEmailHeading: template.heading,
errorEmailMessage: template.message
});
if (emailType === 'errorTemplate') {
this.errorEmail = template;
}
break;
case 'submittedTemplate':
this.submittedEmailForm.reset({
submittedEmailHeading: template.heading,
submittedEmailMessage: template.message
});
if (emailType === 'submittedTemplate') {
this.submittedEmail = template;
}
break;
default:
return;
}
});
}
})
);
}
canDeactivate(): Observable<boolean> | boolean {
if (
this.submittedEmailForm.valid ||
this.errorEmailForm.valid ||
this.abandonedEmailForm.valid ||
this.closedEmailForm.valid ||
this.manuallyClosedForm.valid
) {
if (!this.changesSaved) {
this.canDeactivateGuard.setCanLogout(false);
return this.openDiscardChangesModal();
} else {
return true;
}
} else {
return true;
}
}

action(value: boolean) {
this.discardChangesSubject.next(value);
this.canDeactivateGuard.setCanLogout(value);
this.closeDiscardChangesModal();
}

closeDiscardChangesModal() {
this.discardChangesModalClosed = true;
this.tabIdx = '0';
this.rfaiAdminService.setTabIdx(this.tabIdx);
}

openDiscardChangesModal(): Observable<boolean> {
setTimeout(() => {
if (!this.discardChangesModalClosed) {
// stop showing the message after 30 seconds, passive cancel
this.discardChangesModalClosed = true;
this.tabIdx = '0';
this.rfaiAdminService.setTabIdx(this.tabIdx);
this.discardChangesSubject.next(false);
}
}, 30000);
this.discardChangesModalClosed = false;
this.tabIdx = '-1';
this.rfaiAdminService.setTabIdx(this.tabIdx);
return this.discardChangesSubject.asObservable();
}

ngOnDestroy() {
this.subscription.unsubscribe();
}
}