import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { ToCsvService } from '../shared/to-csv.service'; import { ProductUpdatesReport } from './product-updates-report'; import { ProductUpdatesReportDataSource } from './product-updates-report-datasource'; @Component({ selector: 'app-product-updates-report', templateUrl: './product-updates-report.component.html', styleUrls: ['./product-updates-report.component.css'], }) export class ProductUpdatesReportComponent implements OnInit { dataSource: ProductUpdatesReportDataSource; form: FormGroup; info: ProductUpdatesReport; /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = ['details']; constructor( private route: ActivatedRoute, private router: Router, private fb: FormBuilder, private toCsv: ToCsvService, ) { this.createForm(); } ngOnInit() { this.route.data.subscribe((data: { info: ProductUpdatesReport }) => { this.info = data.info; this.form.setValue({ startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); this.dataSource = new ProductUpdatesReportDataSource(this.info.report); }); } show() { const info = this.getInfo(); this.router.navigate(['product-updates-report'], { queryParams: { startDate: info.startDate, finishDate: info.finishDate, }, }); } createForm() { this.form = this.fb.group({ startDate: '', finishDate: '', }); } getInfo(): ProductUpdatesReport { const formModel = this.form.value; return { startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }; } exportCsv() { const headers = { Details: 'details', }; const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], { type: 'text/csv;charset=utf-8;', }); const link = document.createElement('a'); link.href = window.URL.createObjectURL(csvData); link.setAttribute('download', 'product-updates-report.csv'); document.body.appendChild(link); link.click(); document.body.removeChild(link); } }