import { Component, OnDestroy, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { SpinnerService } from '@core/services/spinner.service'; import { CommunicationsService } from '@features/communications/communications.service'; import { EmailService } from '@features/system-emails/email.service'; import { I18nService } from '@yourcause/common/i18n'; import { ConfirmationModalComponent, ModalFactory } from '@yourcause/common/modals'; import { Subscription } from 'rxjs'; import { ApplicationAttachmentService } from '../application-attachments.service'; import { ApplicationAttachmentForUI, AttachmentType, CommFileInfo } from '../application-attachments.typing'; import { ExternalFileModalComponent } from '../external-file-modal/external-file-modal.component'; import { MergeDocumentModalComponent } from '../merge-document-modal/merge-document-modal.component'; @Component({ selector: 'gc-application-attachments-tab', templateUrl: './application-attachments-tab.component.html', styleUrls: ['./application-attachments-tab.component.scss'] }) export class ApplicationAttachmentsTabComponent implements OnInit, OnDestroy { id = this.activatedRoute.snapshot.parent.params.id; attachments: ApplicationAttachmentForUI[] = []; AttachmentType = AttachmentType; attachmentTypeMap = this.applicationAttachmentService.getAttachmentTypeMap(); emailSubjectMap = this.emailService.emailSubjectMap; sub = new Subscription(); constructor ( private activatedRoute: ActivatedRoute, private applicationAttachmentService: ApplicationAttachmentService, private emailService: EmailService, private modalFactory: ModalFactory, private i18n: I18nService, private communicationsService: CommunicationsService, private spinnerService: SpinnerService, private appAttachmentService: ApplicationAttachmentService, private router: Router ) { } get isNomination () { return location.pathname.includes('nomination'); } get allComms () { return this.communicationsService.applicationCommunicationsMap[this.id]; } async ngOnInit () { this.spinnerService.startSpinner(); await Promise.all([ this.setAttachments(), this.communicationsService.getCommunicationsForApplication(this.id, false) ]); this.spinnerService.stopSpinner(); } async setAttachments () { this.attachments = await this.applicationAttachmentService.getApplicationAttachments( this.id ); } async externalFileModal () { const response = await this.modalFactory.open( ExternalFileModalComponent, { isNomination: this.isNomination } ); if (response) { this.spinnerService.startSpinner(); const passed = await this.applicationAttachmentService.handleAttachExternalFile( response, this.id ); if (passed) { await this.setAttachments(); if (response.addAnother) { setTimeout(() => { this.externalFileModal(); }); } } this.spinnerService.stopSpinner(); } } async mergeDocumentModal () { const response = await this.modalFactory.open( MergeDocumentModalComponent, { isNomination: this.isNomination, applicationId: this.id } ); if (response) { this.spinnerService.startSpinner(); const passed = await this.applicationAttachmentService.handleAttachMergeDocument( response, this.id ); if (passed) { await this.setAttachments(); if (response.addAnother) { setTimeout(() => { this.mergeDocumentModal(); }); } } this.spinnerService.stopSpinner(); } } async removeAttachment (row: ApplicationAttachmentForUI) { const modalSubHeader = row.fileInfo.length === 1 ? row.fileInfo[0].fileName : null; const response = await this.modalFactory.open( ConfirmationModalComponent, { confirmButtonText: this.i18n.translate( 'common:textRemove', {}, 'Remove' ), confirmText: this.i18n.translate( 'GLOBAL:textRemoveAttachmentConfirm', {}, 'Are you sure you want to remove the attachment?' ), modalHeader: this.i18n.translate( 'GLOBAL:hdrRemoveAttachment', {}, 'Remove Attachment' ), modalSubHeader } ); if (response) { this.spinnerService.startSpinner(); const passed = await this.applicationAttachmentService.handleRemoveAttachment( row.fileInfo.map((file) => file.fileUploadId), this.id, row.attachmentType ); if (passed) { await this.setAttachments(); } this.spinnerService.stopSpinner(); } } async openFile (file: CommFileInfo, attachmentType: AttachmentType) { this.spinnerService.startSpinner(); await this.applicationAttachmentService.downloadFileForManager( file.fileUploadId, this.id, attachmentType, file.fileName ); this.spinnerService.stopSpinner(); } async navigateToCommRecord (fileUploadId: number) { const matchingCommRecord = await this.appAttachmentService.getMatchingCommRecordForAttachment( fileUploadId, this.id ); const base = `/management/${this.isNomination ? 'nomination-' : 'application-'}view/${this.id}`; this.router.navigateByUrl(`${base}/communications/${+matchingCommRecord.communicationId}`); } ngOnDestroy () { this.sub.unsubscribe(); } }