114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild, inject } from '@angular/core';
|
|
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { Regime } from '../../core/regime';
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { RegimeService } from '../regime.service';
|
|
|
|
@Component({
|
|
selector: 'app-regime-detail',
|
|
templateUrl: './regime-detail.component.html',
|
|
styleUrls: ['./regime-detail.component.css'],
|
|
imports: [MatButtonModule, MatFormFieldModule, MatInputModule, ReactiveFormsModule],
|
|
})
|
|
export class RegimeDetailComponent implements OnInit, AfterViewInit {
|
|
private route = inject(ActivatedRoute);
|
|
private router = inject(Router);
|
|
private dialog = inject(MatDialog);
|
|
private snackBar = inject(MatSnackBar);
|
|
private ser = inject(RegimeService);
|
|
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: FormGroup<{
|
|
name: FormControl<string>;
|
|
header: FormControl<string>;
|
|
prefix: FormControl<string>;
|
|
}>;
|
|
|
|
item: Regime = new Regime();
|
|
|
|
constructor() {
|
|
// Create form
|
|
this.form = new FormGroup({
|
|
name: new FormControl<string>('', { nonNullable: true }),
|
|
header: new FormControl<string>('', { nonNullable: true }),
|
|
prefix: new FormControl<string>('', { nonNullable: true }),
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: Regime };
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: Regime) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: this.item.name ?? '',
|
|
header: this.item.header ?? '',
|
|
prefix: this.item.prefix ?? '',
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement !== undefined) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('', 'Success');
|
|
this.router.navigateByUrl('/regimes');
|
|
},
|
|
error: (error) => {
|
|
this.snackBar.open(error, 'Error');
|
|
},
|
|
});
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id as number).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('', 'Success');
|
|
this.router.navigateByUrl('/regimes');
|
|
},
|
|
error: (error) => {
|
|
this.snackBar.open(error, 'Error');
|
|
},
|
|
});
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Regime?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): Regime {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name ?? '';
|
|
this.item.header = formModel.header ?? '';
|
|
this.item.prefix = formModel.prefix ?? '';
|
|
return this.item;
|
|
}
|
|
}
|