luthor/otis/src/app/cause-list/cause-list.component.ts

145 lines
3.8 KiB
TypeScript

import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MatPaginator } from '@angular/material/paginator';
import { ActivatedRoute, Router } from '@angular/router';
import { jsPDF } from 'jspdf';
import autoTable from 'jspdf-autotable';
import * as moment from 'moment';
import { Observable, of } from 'rxjs';
import { Case } from '../core/case';
import { CauseList } from './cause-list';
import { CauseListDatasource } from './cause-list-datasource';
@Component({
selector: 'app-cause-list',
templateUrl: './cause-list.component.html',
styleUrls: ['./cause-list.component.css'],
})
export class CauseListComponent implements OnInit {
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
form: FormGroup;
info: CauseList = new CauseList();
list: Observable<Case[]> = new Observable<Case[]>();
dataSource: CauseListDatasource = new CauseListDatasource(this.list);
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = [
'officeFileNumber',
'title',
'courtCaseNumber',
'forum',
'courtAndItemNumber',
'bench',
'nextHearingDate',
'appearOnBehalfOf',
'otherHearingDates',
'remarks',
];
constructor(private route: ActivatedRoute, private router: Router, private fb: FormBuilder) {
// Create form
this.form = this.fb.group({
startDate: '',
finishDate: '',
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { info: CauseList };
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.list = of(this.info.cases);
this.dataSource = new CauseListDatasource(this.list, this.paginator);
});
}
courtNumber(row: Case): string {
const [hearing] = row.hearings;
return hearing.courtNumber;
}
itemNumber(row: Case): string {
const [hearing] = row.hearings;
return hearing.itemNumber;
}
bench(row: Case): string {
const [hearing] = row.hearings;
return hearing.bench;
}
nextHearingDate(row: Case): string {
const [hearing] = row.hearings;
return hearing.nextHearingDate || '';
}
otherHearingDates(row: Case): string {
return row.hearings
.map((x) => x.nextHearingDate)
.filter((x) => !!x)
.slice(0, 5)
.join(', ');
}
show() {
const info = this.getInfo();
this.router.navigate(['cause-list'], {
queryParams: {
startDate: info.startDate,
finishDate: info.finishDate,
},
});
}
getInfo(): CauseList {
const formModel = this.form.value;
return new CauseList({
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
});
}
exportCsv() {
const doc = new jsPDF({
orientation: 'landscape',
});
// doc.autoTable({ html: '#table' });
autoTable(doc, {
head: [
[
'File No.',
'Title',
'Court Case Number',
'Forum',
'Court / Item Number',
'Bench',
'Next Hearing',
'On Behalf of',
'Other Hearings',
'Remarks',
],
],
body: this.dataSource.dataValues.map((row) => [
`${row.caseSource.prefix}-${row.officeFileNumber}`,
row.title,
row.courtCaseNumber,
row.court?.name || '',
`${this.courtNumber(row)} / ${this.itemNumber(row)}`,
this.bench(row),
this.nextHearingDate(row),
row.appearOnBehalfOf,
this.otherHearingDates(row),
row.remarks,
]),
});
doc.save('cause-list.pdf');
}
}