import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { TranslationService } from '@core/services/translation.service'; import { ProgramTypes } from '@core/typings/program.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { FormAudience } from '@features/configure-forms/form.typing'; import { ProgramService } from '@features/programs/program.service'; import { EmailResources } from '@features/system-emails/email.resources'; import { EmailService } from '@features/system-emails/email.service'; import { SendTestEmailPayload, SimpleEmail } from '@features/system-emails/email.typing'; import { UserService } from '@features/users/user.service'; import { ArrayHelpersService, TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { YCModalComponent } from '@yourcause/common/modals'; import { NotifierService } from '@yourcause/common/notifier'; import { ExportEmailService } from '../export-emails/export-emails.service'; interface EmailGroup { translationLanguage: string; } @Component({ selector: 'gc-view-email-modal', templateUrl: './view-email-modal.component.html', styleUrls: ['./view-email-modal.component.scss'], encapsulation: ViewEncapsulation.None }) export class ViewEmailModalComponent extends YCModalComponent implements OnInit { @Input() email: SimpleEmail; @Input() isNomination = false; @Input() isCopy = false; @Input() programId: number; @Input() showEmailHeader = true; @Input() removeEmailContainerClass = false; body: string; title = ''; AudienceType = FormAudience; hasInternational = true; editEmailsArea = false; languageOptions: TypeaheadSelectOption[]; defaultLanguage = 'en-US'; formGroup: TypeSafeFormGroup; FormAudience = FormAudience; constructor ( private logger: LogService, private spinnerService: SpinnerService, private notifier: NotifierService, private i18n: I18nService, private clientSettingsService: ClientSettingsService, private emailService: EmailService, private exportEmailService: ExportEmailService, private formBuilder: TypeSafeFormBuilder, private arrayHelper: ArrayHelpersService, private emailResources: EmailResources, private userService: UserService, private programService: ProgramService, private translationService: TranslationService ) { super(); } get type () { return this.email ? this.email.emailNotificationType : null; } get clientLanguages () { return this.clientSettingsService.get('selectedLanguages'); } get branding () { return this.clientSettingsService.get('clientBranding'); } async ngOnInit () { if (!this.isCopy) { this.spinnerService.startSpinner(); try { await this.emailService.setTemplateMap(this.type); this.spinnerService.stopSpinner(); this.body = this.email.body; } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'GLOBAL:textErrorLoadingEmail', {}, 'There was an error loading the email' )); this.spinnerService.stopSpinner(); this.onCancel(); } } else { this.body = this.email.body; } if (this.hasInternational) { this.languageOptions = this.arrayHelper.sort( this.clientSettingsService.langKeys.map((lang) => { return { label: this.translationService.translateLanguageName(lang), value: lang }; }), 'label' ); this.formGroup = this.formBuilder.group({ translationLanguage: this.clientSettingsService.defaultLanguage }); } this.removeButton(); } async languageOptionChange (langId: string) { const email = await this.emailResources.getClientTranslatedTemplate( langId, this.email.id ); this.email = { ...this.email, ...email }; this.body = this.exportEmailService.getBodyWithoutButton(this.email.body).body; } removeButton () { const response = this.exportEmailService.getBodyWithoutButton(this.body); this.body = response.body; } getSecondaryButtonText () { return this.editEmailsArea ? this.i18n.translate( 'GLOBAL:textSendTestEmail', {}, 'Send test email' ) : null; } async sendTest (isCopy = false) { const payload: SendTestEmailPayload = { clientEmailTemplateId: (this.email.id && this.email.id !== 0) ? this.email.id : null, toEmail: this.userService.userEmail, languageId: this.formGroup.value.translationLanguage }; if (isCopy && !!this.email.id) { payload.clientEmailTemplateId = this.email.id; } else { payload.emailNotificationType = this.email.emailNotificationType; } if (this.programId) { payload.grantProgramId = this.programId; } else { payload.grantProgramId = await this.getRandomProgramId(this.email); } try { await this.emailResources.sendTestEmail(payload); this.notifier.success(this.i18n.translate( 'MANAGE:textSuccessSendingTestEmail', {}, 'Successfully sent test email' )); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'MANAGE:textErrorSendingTestEmail', {}, 'There was an error sending the test email' )); } } async getRandomProgramId (email: SimpleEmail) { await this.programService.getAllActiveManagerPrograms(); let isNomination = false; if (this.programId) { isNomination = this.isNomination; } else { isNomination = email.subject.includes('nominat'); } const found = this.programService.allActiveManagerPrograms.find((item) => { return isNomination ? item.programType === ProgramTypes.NOMINATION : item.programType === ProgramTypes.GRANT; }); return found ? +found.grantProgramId : null; } onCancel () { this.closeModal.emit(); } }