//Importing the libraries import { Component, OnInit } from '@angular/core'; import { DialogService, DynamicDialogRef, DynamicDialogConfig } from 'primeng/api'; //Importing the service classes import { EmailsService } from '../../../shared/service/emails.service'; //Importing the core classes import { IEmailTemplateCategories } from '../../../shared/core/IEmailTemplateCategories'; import { IEmailTemplate } from '../../../shared/core/IEmailTemplate'; @Component({ selector: 'app-contact-save-template', templateUrl: './contact-save-template.component.html', styleUrls: ['./contact-save-template.component.scss'] }) export class ContactSaveTemplateComponent implements OnInit { //to store email categories emailTemplateCategories: IEmailTemplateCategories[] //to store email templates emailTemplates: IEmailTemplate[] showCatecory:boolean = true; saveTemplateField:boolean = true; //to get new email template name newTemplateName: string = "" //to save new category name newCategoryName: string = "" newTemplateCategory: IEmailTemplateCategories //to category name validation categoryNameVal: boolean = true //to store selected category id selectedCategoryId: number //to save new email template newEmailTemplate: IEmailTemplate //this is for passing data tempCategory: IEmailTemplateCategories //to template name validation templateNameVal: boolean = true //to category id validation categoryIdVal: boolean = true emailSubject: string emailContent: string constructor( public dialogService: DialogService, private emailService: EmailsService, public ref: DynamicDialogRef, public config: DynamicDialogConfig ) { } /** ngOnInit() starts */ ngOnInit() { this.loadEmailTemplateCategories(); this.emailSubject = this.config.data.emailSubject; this.emailContent = this.config.data.emailContent; //console.log(this.emailSubject); //console.log(this.emailContent); } /** Ends - ngOnInit() */ loadTemplatesByCategoryId(templateCategoryId: number){ this.emailService.getEmailTemplateByCategoryId(templateCategoryId).subscribe( (data: IEmailTemplate[]) =>{ this.emailTemplates = data; this.selectedCategoryId = templateCategoryId; //console.log(this.emailTemplates); }); } loadEmailTemplateCategories(){ this.emailService.getEmailTemplateCategories().subscribe( (data: IEmailTemplateCategories[]) =>{ this.emailTemplateCategories = data; }); } // add category section addCategory(){ this.showCatecory = false; } //saveTemplate showSaveTemplateText(){ this.saveTemplateField = false; } //closeTemplate closeTemplate(){ this.saveTemplateField = true; this.templateNameVal = true; this.categoryIdVal = true; } closeCategory(){ this.showCatecory = true; this.categoryNameVal = true; } //save new category SaveCategory(){ if(this.newCategoryName != ""){ this.categoryNameVal = true; this.newTemplateCategory = { Id: 0, Name: this.newCategoryName, Description: "", Rank: 0, IsDisable: true, CreatedDate: new Date() } this.emailService.SaveEmailTemplateCategory(this.newTemplateCategory).subscribe( (data:any)=>{ this.emailTemplateCategories = [] this.newCategoryName = ""; this.showCatecory = true; this.loadEmailTemplateCategories(); }); } else{ this.categoryNameVal = false return; } } //to save new email template saveTemplate(){ if(this.newTemplateName != ""){ if(this.selectedCategoryId != 0){ this.tempCategory = { Id : 0, Name : "", Description : "", Rank : 0, IsDisable : false, CreatedDate : new Date() } this.newEmailTemplate = { Id: 0, Title: this.newTemplateName, TemplateCategoryId: this.selectedCategoryId, Subject: this.emailSubject, TemplateContent: this.emailContent, Rank: 0, IsDisable: true, CreatedDate: new Date(), TemplateCategory: this.tempCategory } this.emailService.SaveEmailTemplate(this.newEmailTemplate).subscribe( data => { //Refresh the list this.emailService.getEmailTemplateByCategoryId(this.selectedCategoryId).subscribe( (data: IEmailTemplate[]) => { this.emailTemplates = data; //console.log(this.emailTemplates); this.closeTemplate(); }); }); } else{ this.categoryIdVal = false this.templateNameVal = true; return; } } else{ this.templateNameVal = false; this.categoryIdVal = true return; } } //close popup close(){ this.dialogService.dialogComponentRef.instance.close(); } }