109 lines
2.6 KiB
TypeScript
109 lines
2.6 KiB
TypeScript
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {TableService} from '../table.service';
|
|
import {Table} from '../../core/table';
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
import {ToasterService} from '../../core/toaster.service';
|
|
import {FormBuilder, FormGroup} from '@angular/forms';
|
|
import {ConfirmDialogComponent} from "../../shared/confirm-dialog/confirm-dialog.component";
|
|
import {MatDialog} from "@angular/material";
|
|
|
|
@Component({
|
|
selector: 'app-table-detail',
|
|
templateUrl: './table-detail.component.html',
|
|
styleUrls: ['./table-detail.component.css']
|
|
})
|
|
export class TableDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement: ElementRef;
|
|
form: FormGroup;
|
|
item: Table;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private dialog: MatDialog,
|
|
private fb: FormBuilder,
|
|
private toaster: ToasterService,
|
|
private ser: TableService
|
|
) {
|
|
this.createForm();
|
|
}
|
|
|
|
createForm() {
|
|
this.form = this.fb.group({
|
|
name: '',
|
|
location: '',
|
|
isActive: ''
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data
|
|
.subscribe((data: { item: Table }) => {
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: Table) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: this.item.name,
|
|
location: this.item.location,
|
|
isActive: this.item.isActive
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
this.nameElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem())
|
|
.subscribe(
|
|
(result) => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/tables');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error.error);
|
|
}
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id)
|
|
.subscribe(
|
|
(result) => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/tables');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error.error);
|
|
}
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: {title: 'Delete Account?', content: 'Are you sure? This cannot be undone.'}
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): Table {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name;
|
|
this.item.location = formModel.location;
|
|
this.item.isActive = formModel.isActive;
|
|
return this.item;
|
|
}
|
|
}
|