import { Component, Input, OnInit } from '@angular/core'; import { ApplicantService } from '@core/services/auth-user/applicant.service'; import { SpinnerService } from '@core/services/spinner.service'; import { ApplicantFromSearch } from '@core/typings/applicant.typing'; import { ApplicationForUi } from '@core/typings/application.typing'; import { OfflineProgramDetail, ProgramApplicantType } from '@core/typings/program.typing'; import { ApplicationViewService } from '@features/application-view/application-view.service'; import { NonprofitService } from '@features/nonprofit/nonprofit.service'; import { VettingInfo } from '@features/offline-grants/offline-grants-create-edit-application/organization/organization.component'; import { CreateEditApplicationModalResponse, OfflineCreateModalAction } from '@features/offline-grants/offline-grants.typing'; import { ProgramService } from '@features/programs/program.service'; import { SearchResult } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; @Component({ selector: 'gc-create-edit-application-modal', templateUrl: './create-edit-application-modal.component.html', styleUrls: ['./create-edit-application-modal.component.scss'] }) export class CreateEditApplicationModalComponent extends YCModalComponent< CreateEditApplicationModalResponse > implements OnInit { @Input() applicationId: number; @Input() programId: number; @Input() cycleId: number; @Input() isNomination: boolean; @Input() applicantCanReceiveEmails = true; application: ApplicationForUi; selectedApplicant: ApplicantFromSearch; selectedOrg: SearchResult; vettingInfo: VettingInfo; vettingIsValid: boolean; program: OfflineProgramDetail; notifyApplicant: boolean; alertMessage: string; supportsOrganization: boolean; modalHeader: string; primaryButtonText: string; secondaryButtonText: string; canEditProgram: boolean; orgModalOpen = false; applicantModalOpen = false; ready = false; constructor ( private applicationViewService: ApplicationViewService, private i18n: I18nService, private programService: ProgramService, private spinnerService: SpinnerService, private applicantService: ApplicantService, private analyticsService: AnalyticsService, private nonprofitService: NonprofitService ) { super(); } get isModalValid () { return this.programId && this.cycleId && this.selectedApplicant && (this.supportsOrganization ? !!this.selectedOrg : true) && (this.vettingInfo?.requiresVetting ? this.vettingIsValid : true); } async ngOnInit () { this.spinnerService.startSpinner(); await this.setApplicationInfo(); this.setAlertMessage(); this.setButtonText(); this.ready = true; this.spinnerService.stopSpinner(); } setButtonText () { if (this.applicationId) { this.modalHeader = this.i18n.translate( this.isNomination ? 'APPLY:textEditNomination' : 'APPLY:textEditApplication', {}, this.isNomination ? 'Edit Nomination' : 'Edit Application' ); this.primaryButtonText = this.i18n.translate( 'APPLY:textSaveChanges', {}, 'Save changes' ); this.secondaryButtonText = ''; } else { this.modalHeader = this.i18n.translate( this.isNomination ? 'APPLY:textCreateNomination' : 'APPLY:textCreateApplication', {}, this.isNomination ? 'Create Nomination' : 'Create Application' ); this.primaryButtonText = this.i18n.translate( 'common:textSaveAndEdit', {}, 'Save and edit' ); this.secondaryButtonText = !this.notifyApplicant ? this.i18n.translate( 'common:textSaveAsDraft', {}, 'Save as draft' ) : this.i18n.translate( this.isNomination ? 'GLOBAL:textSendToNominator' : 'GLOBAL:textSendToApplicant', {}, this.isNomination ? 'Send to nominator' : 'Send to applicant' ); } } async setApplicationInfo () { if (this.applicationId) { this.application = await this.applicationViewService.getApplicationDetail( this.applicationId ); this.canEditProgram = this.application.isDraft; await Promise.all([ this.setOrganization(), this.setApplicant(), this.programIdChanged(this.application.programId) ]); this.cycleIdChanged(this.application.grantProgramCycle.id); } else { this.canEditProgram = true; this.notifyApplicant = true; } } async setOrganization () { this.selectedOrg = await this.nonprofitService.constructSearchResult(this.application); } async setApplicant () { let applicant: ApplicantFromSearch; this.notifyApplicant = this.application.applicantCanReceiveEmails; if (this.application.isMasked && !this.application.canViewMaskedApplicantInfo) { applicant = { id: this.application.applicantId, firstName: '', lastName: '', email: '', address: null, phoneNumber: null, notifyApplicant: this.application.applicantCanReceiveEmails }; } else { applicant = await this.applicantService.exactMatchSearchApplicants( this.application.applicantEmail ); } this.selectedApplicant = applicant; } setSupportsOrganization () { this.supportsOrganization = this.program?.programApplicantType !== ProgramApplicantType.INDIVIDUAL; } setAlertMessage () { this.alertMessage = this.i18n.translate( this.isNomination ? 'common:textCreateEditNomAlert2' : 'common:textCreateEditAppAlert3', {}, `Start the ${this.isNomination ? 'nomination' : 'application'} by completing the required information below. Once completed, choose how you want to proceed with the ${this.isNomination ? 'nomination' : 'application'}:` + `