import { Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { FileService } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import moment from 'moment'; import { map, mergeMap, share, tap } from 'rxjs'; import { SharedDownloadsService } from '../shared-downloads.service'; @Component({ selector: 'gc-shared-downloads-wrapper', templateUrl: 'shared-downloads-wrapper.component.html', styleUrls: ['./shared-downloads-wrapper.component.scss'] }) export class SharedDownloadsWrapperComponent { private url: string; private fileName: string; private urlParams$ = this.activatedRoute.queryParamMap.pipe( map(queryMap => ({ downloadLink: queryMap.get('downloadLink'), userGeneratedReportID: queryMap.get('userGeneratedReportId'), expirationParam: queryMap.get('expiration'), fileName: queryMap.get('fileName') })), mergeMap(params => this.sharedDownloadService.evaluateDownloadUrl(params)), tap((params) => { this.url = params.downloadLink; this.fileName = params.fileName; }), share()); private expired$ = this.urlParams$.pipe( map(params => ({ params, isValid: (params.expiration.isAfter(moment()) && params.fileExists) }))); buttonVisible$ = this.expired$.pipe( map(({ isValid }) => isValid)); icon$ = this.expired$.pipe( map(({ isValid }) => isValid ? 'download' : 'hourglass-end')); buttonText = this.i18n.translate( 'common:textDownloadReport', {}, 'Download report' ); downloadBodyText$ = this.expired$.pipe( map(({ isValid, params }) => { if (isValid) { return this.i18n.translate( 'common:textSharedDownloadBodyText', { timeRemaining: params.expiration.diff(moment(), 'h') }, 'Click below to download your report. To ensure that valuable report information is kept secure, reports expire after a set amount of time. This report\'s download link will expire in __timeRemaining__ hour(s).' ); } return this.i18n.translate( 'common:textSharedDownloadBodyTextExpired', {}, 'To ensure that valuable report information is kept secure, reports expire after a set amount of time. If you would like to receive this report please contact an administrator and ask to have the report manually sent to you.' ); })); downloadHeaderText$ = this.expired$.pipe( map(({ isValid }) => { if (isValid) { return this.i18n.translate( 'common:textSharedDownloadHeader', {}, 'Download Report' ); } else { return this.i18n.translate( 'common:textExpiredSharedDownloadHeader', {}, 'Download Expired' ); } })); constructor ( private activatedRoute: ActivatedRoute, private sharedDownloadService: SharedDownloadsService, private i18n: I18nService, private analyticsService: AnalyticsService, private fileService: FileService ) { } async handleClick () { await this.fileService.downloadUrlAs(this.url, this.fileName); this.analyticsService.emitEvent({ eventName: 'Open shared file', eventType: EventType.Click, extras: null }); } }