|
| 1 | +import {HttpErrorResponse} from '@angular/common/http'; |
| 2 | +import {AfterViewInit, Component, OnDestroy, OnInit} from '@angular/core'; |
| 3 | +import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap'; |
| 4 | +import {NgxSpinnerService} from 'ngx-spinner'; |
| 5 | +import {LocalStorageService} from 'ngx-webstorage'; |
| 6 | +import {EMPTY, Observable, Subject} from 'rxjs'; |
| 7 | +import {catchError, concatMap, filter, map, takeUntil, tap} from 'rxjs/operators'; |
| 8 | +import {UtmToastService} from '../../shared/alert/utm-toast.service'; |
| 9 | +import {ADMIN_ROLE} from '../../shared/constants/global.constant'; |
| 10 | +import {ExportPdfService} from '../../shared/services/util/export-pdf.service'; |
| 11 | +import {UtmCpStandardComponent} from '../shared/components/utm-cp-standard/utm-cp-standard.component'; |
| 12 | +import {CpReportsService} from '../shared/services/cp-reports.service'; |
| 13 | +import {CpStandardSectionService} from '../shared/services/cp-standard-section.service'; |
| 14 | +import {ComplianceReportType} from '../shared/type/compliance-report.type'; |
| 15 | +import {ComplianceStandardSectionType} from '../shared/type/compliance-standard-section.type'; |
| 16 | +import {ComplianceStandardType} from '../shared/type/compliance-standard.type'; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + selector: 'app-compliance-report-viewer', |
| 20 | + templateUrl: './compliance-report-viewer.component.html', |
| 21 | + styleUrls: ['./compliance-report-viewer.component.css'] |
| 22 | +}) |
| 23 | +export class ComplianceReportViewerComponent implements OnInit, AfterViewInit, OnDestroy { |
| 24 | + admin = ADMIN_ROLE; |
| 25 | + sections$: Observable<ComplianceStandardSectionType[]>; |
| 26 | + standard: ComplianceStandardType; |
| 27 | + activeIndexSection = 0; |
| 28 | + destroy$: Subject<void> = new Subject(); |
| 29 | + report: ComplianceReportType; |
| 30 | + pdfExport = false; |
| 31 | + action: 'reports' | 'compliance' = 'compliance'; |
| 32 | + activeSection: ComplianceStandardSectionType = null; |
| 33 | + |
| 34 | + constructor(private standardSectionService: CpStandardSectionService, |
| 35 | + private toastService: UtmToastService, |
| 36 | + private modalService: NgbModal, |
| 37 | + private $localStorage: LocalStorageService, |
| 38 | + private spinner: NgxSpinnerService, |
| 39 | + private reportsService: CpReportsService, |
| 40 | + private exportPdfService: ExportPdfService) { |
| 41 | + this.standard = this.$localStorage.retrieve('selectedStandard'); |
| 42 | + } |
| 43 | + |
| 44 | + ngOnInit() { |
| 45 | + this.sections$ = this.standardSectionService.onRefresh$ |
| 46 | + .pipe(takeUntil(this.destroy$), |
| 47 | + filter(refresh => !!refresh && refresh.loading), |
| 48 | + concatMap(() => this.standardSectionService.fetchData({ |
| 49 | + page: 0, |
| 50 | + size: 1000, |
| 51 | + 'standardId.equals': this.standard.id |
| 52 | + })), |
| 53 | + map((res) => { |
| 54 | + return res.body.map((s, index) => { |
| 55 | + return { |
| 56 | + ...s, |
| 57 | + isCollapsed: this.activeIndexSection === index, |
| 58 | + isActive: this.activeIndexSection === index |
| 59 | + }; |
| 60 | + }); |
| 61 | + }), |
| 62 | + tap((sections) => { |
| 63 | + this.activeSection = { |
| 64 | + ...sections[this.activeIndexSection] |
| 65 | + }; |
| 66 | + }), |
| 67 | + catchError((err: HttpErrorResponse) => { |
| 68 | + this.toastService.showError('Error', |
| 69 | + 'Unable to retrieve the list of compliance standard sections. Please try again or contact support.'); |
| 70 | + return EMPTY; |
| 71 | + })); |
| 72 | + |
| 73 | + this.reportsService.onLoadReport$ |
| 74 | + .pipe(takeUntil(this.destroy$), |
| 75 | + filter((params) => !!params)) |
| 76 | + .subscribe((params) => this.report = params.template); |
| 77 | + } |
| 78 | + |
| 79 | + ngAfterViewInit(): void { |
| 80 | + if (!this.standard) { |
| 81 | + this.manageStandards(); |
| 82 | + } else { |
| 83 | + this.standardSectionService.notifyRefresh({ |
| 84 | + loading: true, |
| 85 | + activeSection: 0 |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + manageStandards() { |
| 91 | + const options: NgbModalOptions = { |
| 92 | + backdrop: 'static', |
| 93 | + keyboard: false, |
| 94 | + centered: true |
| 95 | + }; |
| 96 | + const modalRef = this.modalService.open(UtmCpStandardComponent, options); |
| 97 | + |
| 98 | + modalRef.result.then((standard) => { |
| 99 | + this.standard = standard; |
| 100 | + this.standardSectionService.notifyRefresh({ |
| 101 | + loading: true, |
| 102 | + activeSection: 0 |
| 103 | + }); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + onChangeSectionActive(index: number) { |
| 108 | + this.activeIndexSection = index; |
| 109 | + this.standardSectionService.notifyRefresh({ |
| 110 | + loading: true, |
| 111 | + activeSection: index |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + exportToPdf() { |
| 116 | + this.spinner.show('buildPrintPDF'); |
| 117 | + const url = this.getUrl(); |
| 118 | + const fileName = this.report ? this.report.associatedDashboard.name.replace(/ /g, '_') : 'Reports'; |
| 119 | + this.exportPdfService.getPdf(url, fileName, 'PDF_TYPE_TOKEN').subscribe(response => { |
| 120 | + this.spinner.hide('buildPrintPDF').then(() => |
| 121 | + this.exportPdfService.handlePdfResponse(response)); |
| 122 | + }, error => { |
| 123 | + this.spinner.hide('buildPrintPDF').then(() => |
| 124 | + this.toastService.showError('Error', 'An error occurred while creating a PDF.')); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + selectAction(action: 'reports' | 'compliance' ) { |
| 129 | + this.action = action; |
| 130 | + } |
| 131 | + |
| 132 | + trackFn(index: number, section: ComplianceStandardSectionType) { |
| 133 | + return section.id; |
| 134 | + } |
| 135 | + |
| 136 | + getUrl(){ |
| 137 | + return this.action === 'reports' ? '/dashboard/export-compliance/' + this.report.id : |
| 138 | + `/compliance/print-view/?section=${this.getActiveSectionParams()}`; |
| 139 | + } |
| 140 | + |
| 141 | + getActiveSectionParams(){ |
| 142 | + return encodeURIComponent(JSON.stringify({ |
| 143 | + standardId: this.activeSection.standardId, |
| 144 | + id: this.activeSection.id |
| 145 | + })); |
| 146 | + } |
| 147 | + |
| 148 | + ngOnDestroy(): void { |
| 149 | + this.reportsService.loadReport(null); |
| 150 | + this.destroy$.next(); |
| 151 | + this.destroy$.complete(); |
| 152 | + this.standardSectionService.notifyRefresh(null); |
| 153 | + } |
| 154 | +} |
0 commit comments