import { Component, OnInit } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { EmailTemplate, ProgramEmailPreference } from '@core/typings/program.typing'; import { EmailResources } from '@features/system-emails/email.resources'; import { EmailNotificationType } from '@features/system-emails/email.typing'; import { AutoTableRepositoryFactory } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { ProgramService } from '../program.service'; @Component({ selector: 'gc-program-communication', templateUrl: './program-communication.component.html', styleUrls: ['./program-communication.component.scss'] }) export class ProgramCommunicationComponent implements OnInit { activeMap: { [x: string]: number; } = {}; disabledMap: { [x: string]: boolean; } = {}; constructor ( private autoTableFactory: AutoTableRepositoryFactory, private spinnerService: SpinnerService, private programService: ProgramService, private emailResources: EmailResources, private analyticsService: AnalyticsService ) { } get isNomination () { return location.pathname.includes('nomination'); } get programMap () { return this.programService.get('configureProgramMap'); } get activeProgramId () { return this.programService.get('activeProgramId'); } get program () { return this.programMap[this.activeProgramId]; } get repo () { return this.autoTableFactory.getRepository( this.isNomination ? 'NOMINATION_EMAILS' : 'PROGRAM_EMAILS' ); } async ngOnInit () { this.setDisabledMap(); await this.setActiveMap(); } addNewProgramEmail (email: EmailTemplate) { const clientTemplates = this.getUpdatedClientTemplates(email); this.updateProgram(clientTemplates); } async setActiveMap () { this.spinnerService.startSpinner(); const activeCounts = await this.emailResources.getProgramActiveCount( +this.activeProgramId ); activeCounts.forEach((item) => { this.activeMap[item.type] = this.disabledMap[item.type] ? 0 : item.count; }); this.spinnerService.stopSpinner(); } setDisabledMap () { (this.program.emailNotificationTypesPreferences || []).forEach((item) => { this.disabledMap[item.id] = item.isExcluded; }); } updateActiveMap () { this.program.clientTemplates.forEach((temp) => { const active = temp.templates.filter((copy) => { return copy.active; }); const isDisabled = this.disabledMap[temp.emailNotificationType]; this.activeMap[temp.emailNotificationType] = isDisabled ? 0 : active.length; }); } setAsDefault (newTemplate: EmailTemplate) { const clientTemplates = this.getUpdatedClientTemplates(newTemplate); this.updateProgram(clientTemplates); this.analyticsService.emitEvent({ eventName: 'Set email as default', eventType: EventType.Click, extras: null }); } setActive (newTemplate: EmailTemplate) { const clientTemplates = this.getUpdatedClientTemplates(newTemplate); this.updateProgram(clientTemplates); this.analyticsService.emitEvent({ eventName: 'Set email active', eventType: EventType.Click, extras: null }); } getUpdatedClientTemplates ( newTemplate: EmailTemplate ) { let clientTemplates: EmailTemplate[]; const existingIndex = this.program.clientTemplates.findIndex((temp) => { return newTemplate.emailNotificationType === temp.emailNotificationType; }); if (existingIndex > -1) { clientTemplates = [ ...this.program.clientTemplates.slice(0, existingIndex), newTemplate, ...this.program.clientTemplates.slice(existingIndex + 1) ]; } else { clientTemplates = [ ...this.program.clientTemplates, newTemplate ]; } return clientTemplates; } async toggleEmailActive (response: { type: EmailNotificationType; isExcluded: boolean; }) { const index = (this.program.emailNotificationTypesPreferences || []) .findIndex((item) => item.id === response.type); let preferences: ProgramEmailPreference[]; const newPref = { id: response.type, isExcluded: response.isExcluded }; if (index > -1) { preferences = [ ...this.program.emailNotificationTypesPreferences.slice(0, index), newPref, ...this.program.emailNotificationTypesPreferences.slice(index + 1) ]; } else { preferences = [ ...this.program.emailNotificationTypesPreferences || [], newPref ]; } this.programService.setMapProperty( this.activeProgramId, 'emailNotificationTypesPreferences', preferences ); this.disabledMap[response.type] = response.isExcluded; if (response.isExcluded) { this.activeMap[response.type] = 0; } else { this.spinnerService.startSpinner(); const activeCounts = await this.emailResources.getProgramActiveCount( +this.activeProgramId ); const found = activeCounts.find((item) => item.type === response.type); if (found) { this.activeMap[response.type] = found.count; } this.spinnerService.stopSpinner(); } this.analyticsService.emitEvent({ eventName: 'Toggle email active/inactive', eventType: EventType.Click, extras: null }); } updateProgram ( clientTemplates: EmailTemplate[] ) { this.programService.setMapProperty( this.activeProgramId, 'clientTemplates', clientTemplates ); this.updateActiveMap(); } }