luthor/otis/src/app/cases/case-detail/case-detail.component.ts

262 lines
9.1 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs/internal/Observable';
import { distinctUntilChanged, startWith, switchMap, tap } from 'rxjs/operators';
import { Act } from '../../core/act';
import { Case } from '../../core/case';
import { CaseType } from '../../core/case-type';
import { Court } from '../../core/court';
import { CourtStatus } from '../../core/court-status';
import { Department } from '../../core/department';
import { Nature } from '../../core/nature';
import { Office } from '../../core/office';
import { OfficeStatus } from '../../core/office-status';
import { ToasterService } from '../../core/toaster.service';
import { OfficeService } from '../../offices/office.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { CaseService } from '../case.service';
@Component({
selector: 'app-case-detail',
templateUrl: './case-detail.component.html',
styleUrls: ['./case-detail.component.css'],
})
export class CaseDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup;
departments: Department[] = [];
offices: Observable<Office[]> = new Observable<Office[]>();
item: Case = new Case();
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: CaseService,
private officeService: OfficeService,
) {
// Create form
this.form = this.fb.group({
officeFileNumber: '',
courtCaseNumber: '',
year: '',
title: '',
docketNumber: '',
receiptDate: '',
limitationDate: '',
filingDate: '',
appearOnBehalfOf: '',
questionOfLaw: '',
aorName: '',
opposingCouncilAor: '',
opposingCouncilDetail: '',
lowerCourtCaseNumber: '',
dateOfImpugnedJudgement: '',
briefDescription: '',
remarks: '',
slpCounter: '',
contactDetail: '',
caseConnectedWith: '',
bunchCases: '',
court: '',
department: '',
office: '',
caseType: '',
act: '',
nature: '',
officeStatus: '',
courtStatus: '',
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: Case; departments: Department[] };
this.departments = data.departments;
this.showItem(data.item);
});
}
showItem(item: Case) {
this.item = item;
this.form.setValue({
officeFileNumber: this.item.officeFileNumber,
courtCaseNumber: this.item.courtCaseNumber,
year: this.item.year,
title: this.item.title,
docketNumber: this.item.docketNumber,
receiptDate: this.item.receiptDate,
limitationDate: this.item.limitationDate,
filingDate: this.item.filingDate,
appearOnBehalfOf: this.item.appearOnBehalfOf,
questionOfLaw: this.item.questionOfLaw,
aorName: this.item.aorName,
opposingCouncilAor: this.item.opposingCouncilAor,
opposingCouncilDetail: this.item.opposingCouncilDetail,
lowerCourtCaseNumber: this.item.lowerCourtCaseNumber,
dateOfImpugnedJudgement: this.item.dateOfImpugnedJudgement,
briefDescription: this.item.briefDescription,
remarks: this.item.remarks,
slpCounter: this.item.slpCounter,
contactDetail: this.item.contactDetail,
caseConnectedWith: this.item.caseConnectedWith,
bunchCases: this.item.bunchCases,
court: this.item.court ? this.item.court.id : '',
department: this.item.department ? this.item.department.id : '',
office: this.item.office ? this.item.office.id : '',
caseType: this.item.caseType ? this.item.caseType.id : '',
act: this.item.act ? this.item.act.id : '',
nature: this.item.nature ? this.item.nature.id : '',
officeStatus: this.item.officeStatus ? this.item.officeStatus.id : '',
courtStatus: this.item.courtStatus ? this.item.courtStatus.id : '',
});
this.offices = (this.form.get('department') as FormControl).valueChanges.pipe(
startWith(this.item.department ? this.item.department.id : ''),
distinctUntilChanged(),
switchMap((x) => this.officeService.filteredList(x)),
);
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement !== undefined) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/cases');
},
(error) => {
this.toaster.show('Error', error);
},
);
}
delete() {
this.ser.delete(this.item.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/cases');
},
(error) => {
this.toaster.show('Error', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Case?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Case {
const formModel = this.form.value;
this.item.officeFileNumber = formModel.officeFileNumber;
this.item.courtCaseNumber = formModel.courtCaseNumber;
this.item.year = formModel.year;
this.item.title = formModel.title;
this.item.docketNumber = formModel.docketNumber;
this.item.receiptDate = formModel.receiptDate;
this.item.limitationDate = formModel.limitationDate;
this.item.filingDate = formModel.filingDate;
this.item.appearOnBehalfOf = formModel.appearOnBehalfOf;
this.item.questionOfLaw = formModel.questionOfLaw;
this.item.aorName = formModel.aorName;
this.item.opposingCouncilAor = formModel.opposingCouncilAor;
this.item.opposingCouncilDetail = formModel.opposingCouncilDetail;
this.item.lowerCourtCaseNumber = formModel.lowerCourtCaseNumber;
this.item.dateOfImpugnedJudgement = formModel.dateOfImpugnedJudgement;
this.item.briefDescription = formModel.briefDescription;
this.item.remarks = formModel.remarks;
this.item.slpCounter = formModel.slpCounter;
this.item.contactDetail = formModel.contactDetail;
this.item.caseConnectedWith = formModel.caseConnectedWith;
this.item.bunchCases = formModel.bunchCases;
if (formModel.court === undefined || formModel.court === '') {
this.item.court = undefined;
} else {
if (this.item.court === null || this.item.court === undefined) {
this.item.court = new Court();
}
this.item.court.id = formModel.court;
}
if (formModel.department === undefined || formModel.department === '') {
this.item.department = undefined;
} else {
if (this.item.department === null || this.item.department === undefined) {
this.item.department = new Department();
}
this.item.department.id = formModel.department;
}
if (formModel.office === undefined || formModel.office === '') {
this.item.office = undefined;
} else {
if (this.item.office === null || this.item.office === undefined) {
this.item.office = new Office();
}
this.item.office.id = formModel.office;
}
if (formModel.caseType === undefined || formModel.caseType === '') {
this.item.caseType = undefined;
} else {
if (this.item.caseType === null || this.item.caseType === undefined) {
this.item.caseType = new CaseType();
}
this.item.caseType.id = formModel.caseType;
}
if (formModel.act === undefined || formModel.act === '') {
this.item.act = undefined;
} else {
if (this.item.act === null || this.item.act === undefined) {
this.item.act = new Act();
}
this.item.act.id = formModel.act;
}
if (formModel.nature === undefined || formModel.nature === '') {
this.item.nature = undefined;
} else {
if (this.item.nature === null || this.item.nature === undefined) {
this.item.nature = new Nature();
}
this.item.nature.id = formModel.nature;
}
if (formModel.officeStatus === undefined || formModel.officeStatus === '') {
this.item.officeStatus = undefined;
} else {
if (this.item.officeStatus === null || this.item.officeStatus === undefined) {
this.item.officeStatus = new OfficeStatus();
}
this.item.officeStatus.id = formModel.officeStatus;
}
if (formModel.courtStatus === undefined || formModel.courtStatus === '') {
this.item.courtStatus = undefined;
} else {
if (this.item.courtStatus === null || this.item.courtStatus === undefined) {
this.item.courtStatus = new CourtStatus();
}
this.item.courtStatus.id = formModel.courtStatus;
}
return this.item;
}
}