Files
barker/bookie/src/app/devices/device-detail/device-detail.component.ts
Amritanshu 010e9a84db Chore: Upgrade to Angular v18
Chore: Upgrade to Python 3.12
Chore: Upgrade to psycopg3
2024-06-03 13:22:56 +05:30

146 lines
4.0 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { MatButton } from '@angular/material/button';
import {
MatCard,
MatCardHeader,
MatCardTitle,
MatCardContent,
MatCardActions,
MatCardSubtitle,
} from '@angular/material/card';
import { MatCheckbox } from '@angular/material/checkbox';
import { MatOption } from '@angular/material/core';
import { MatDialog } from '@angular/material/dialog';
import { MatFormField, MatLabel } from '@angular/material/form-field';
import { MatInput } from '@angular/material/input';
import { MatSelect } from '@angular/material/select';
import { ActivatedRoute, Router } from '@angular/router';
import { Device } from '../../core/device';
import { Section } from '../../core/section';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { LocalTimePipe } from '../../shared/local-time.pipe';
import { DeviceService } from '../device.service';
@Component({
selector: 'app-device-detail',
templateUrl: './device-detail.component.html',
styleUrls: ['./device-detail.component.css'],
standalone: true,
imports: [
MatCard,
MatCardHeader,
MatCardTitle,
MatCardContent,
ReactiveFormsModule,
MatFormField,
MatLabel,
MatInput,
MatSelect,
MatOption,
MatCheckbox,
MatCardActions,
MatButton,
MatCardSubtitle,
LocalTimePipe,
],
})
export class DeviceDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup<{
name: FormControl<string>;
section: FormControl<string | null>;
enabled: FormControl<boolean>;
}>;
sections: Section[] = [];
item: Device = new Device();
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private toaster: ToasterService,
private ser: DeviceService,
) {
// Create form
this.form = new FormGroup({
name: new FormControl<string>('', { nonNullable: true }),
section: new FormControl<string | null>(null),
enabled: new FormControl<boolean>(false, { nonNullable: true }),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: Device; sections: Section[] };
this.showItem(data.item);
this.sections = data.sections;
});
}
showItem(item: Device) {
this.item = item;
this.form.setValue({
name: this.item.name ?? '',
section: this.item.section.id ? this.item.section.id : '',
enabled: this.item.enabled,
});
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement !== undefined) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe({
next: () => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/devices');
},
error: (error) => {
this.toaster.show('Error', error);
},
});
}
delete() {
this.ser.delete(this.item.id as string).subscribe({
next: () => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/devices');
},
error: (error) => {
this.toaster.show('Error', error);
},
});
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Device?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Device {
const formModel = this.form.value;
this.item.name = formModel.name ?? '';
this.item.section.id = formModel.section ?? '';
this.item.enabled = formModel.enabled ?? true;
return this.item;
}
}