import { Component, Input, OnInit } from '@angular/core'; import { FormDueEmailModalResponse, FormDueEmails } from '@features/configure-forms/form.typing'; import { EmailService } from '@features/system-emails/email.service'; import { EmailNotificationType } from '@features/system-emails/email.typing'; import { DuplicateCheckValidator, EmailValidator, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; interface SendFormDueFormGroup { clientEmailTemplateId: number; cc: string[]; bcc: string[]; attachments: any[]; // TODO: figure out this typing } @Component({ selector: 'gc-send-form-due-email-modal', templateUrl: './send-form-due-email-modal.component.html', styleUrls: ['./send-form-due-email-modal.component.scss'] }) export class SendFormDueEmailModalComponent extends YCModalComponent implements OnInit { @Input() isOverdue = false; @Input() isManager = false; @Input() formName: string; @Input() programId: number; @Input() userId: number; @Input() applicationId: number; @Input() formId: number; @Input() workflowLevelId: number; @Input() isNomination = false; @Input() applicationFormId: number; formGroup: TypeSafeFormGroup; emailType: EmailNotificationType; confirmText: string; currentEmailActive = false; saving = false; constructor ( private formBuilder: TypeSafeFormBuilder, private i18n: I18nService, private emailService: EmailService, private analyticsService: AnalyticsService ) { super(); } async ngOnInit () { await this.setConfirmTextAndEmailType(); this.formGroup = this.formBuilder.group({ clientEmailTemplateId: 0, cc: [[], EmailValidator()], bcc: [[], EmailValidator()], attachments: [[]] }, { validator: [ DuplicateCheckValidator( 'cc', 'bcc' ) ] }); } async setConfirmTextAndEmailType () { const typeAttr = this.isManager ? 'manager' : 'applicant'; const nomAttr = this.isNomination ? 'nomination' : 'application'; if (this.isOverdue) { this.emailType = FormDueEmails[typeAttr][nomAttr].overdue; if (this.isNomination) { this.confirmText = this.i18n.translate( 'FORMS:textFormOverdueEmailToNominatorConfirm', {}, 'You are about to send an email to the nominator that will remind them to submit their overdue form.' ); } else { this.confirmText = this.i18n.translate( 'FORMS:textFormOverdueEmailToApplicantConfirm', {}, 'You are about to send an email to the applicant that will remind them to submit their overdue form.' ); } } else { this.emailType = FormDueEmails[typeAttr][nomAttr].due; if (this.isNomination) { this.confirmText = this.i18n.translate( 'FORMS:textFormOverdueEmailToNominatorConfirm', {}, 'You are about to send an email to the nominator that will remind them to submit their form.' ); } else { this.confirmText = this.i18n.translate( 'FORMS:textFormDueEmailToApplicantConfirm', {}, 'You are about to send an email to the applicant that will remind them to submit their form.' ); } } this.currentEmailActive = await this.emailService.isProgramEmailActive( this.emailType, this.programId ); } async onSend () { this.saving = true; const formValue = this.formGroup.value; const attachments = await this.emailService.returnIdsFromMixedAttachments( formValue.attachments, this.applicationId ); this.saving = false; this.closeModal.emit({ clientEmailTemplateId: formValue.clientEmailTemplateId, applicationFormId: this.applicationFormId, applicationId: this.applicationId, formId: this.formId, workflowLevelId: this.workflowLevelId, userId: this.userId, emailOptionsModel: { ccEmails: formValue.cc, bccEmails: formValue.bcc, attachments } }); this.analyticsService.emitEvent({ eventName: 'Send form due modal submit', eventType: EventType.Click, extras: null }); } }