128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, inject, OnInit, ViewChild } from '@angular/core';
|
|
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatCardModule } from '@angular/material/card';
|
|
import { MatCheckboxModule } from '@angular/material/checkbox';
|
|
import { MatDatepickerModule } from '@angular/material/datepicker';
|
|
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 moment from 'moment';
|
|
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { RecipeTemplate } from '../recipe-template';
|
|
import { RecipeTemplateService } from '../recipe-template.service';
|
|
|
|
@Component({
|
|
selector: 'app-recipe-template-detail',
|
|
templateUrl: './recipe-template-detail.component.html',
|
|
styleUrls: ['./recipe-template-detail.component.css'],
|
|
imports: [
|
|
MatCardModule,
|
|
ReactiveFormsModule,
|
|
MatFormFieldModule,
|
|
MatInputModule,
|
|
MatDatepickerModule,
|
|
MatCheckboxModule,
|
|
MatButtonModule,
|
|
],
|
|
})
|
|
export class RecipeTemplateDetailComponent implements OnInit, AfterViewInit {
|
|
private route = inject(ActivatedRoute);
|
|
private router = inject(Router);
|
|
private dialog = inject(MatDialog);
|
|
private snackBar = inject(MatSnackBar);
|
|
private ser = inject(RecipeTemplateService);
|
|
|
|
@ViewChild('nameElement', { static: true }) nameElement!: ElementRef<HTMLInputElement>;
|
|
form: FormGroup<{
|
|
name: FormControl<string>;
|
|
date: FormControl<moment.Moment>;
|
|
text: FormControl<string>;
|
|
selected: FormControl<boolean>;
|
|
}>;
|
|
|
|
item: RecipeTemplate = new RecipeTemplate();
|
|
|
|
constructor() {
|
|
this.form = new FormGroup({
|
|
name: new FormControl<string>('', { nonNullable: true }),
|
|
date: new FormControl(moment(new Date()), { nonNullable: true }),
|
|
text: new FormControl<string>('', { nonNullable: true }),
|
|
selected: new FormControl<boolean>(false, { nonNullable: true }),
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: RecipeTemplate };
|
|
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: RecipeTemplate) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: this.item.name,
|
|
date: moment(this.item.date, 'DD-MMM-YYYY'),
|
|
text: this.item.text,
|
|
selected: this.item.selected,
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
this.nameElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('', 'Success');
|
|
this.router.navigateByUrl('/recipe-templates');
|
|
},
|
|
error: (error) => {
|
|
this.snackBar.open(error, 'Danger');
|
|
},
|
|
});
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('', 'Success');
|
|
this.router.navigateByUrl('/recipe-templates');
|
|
},
|
|
error: (error) => {
|
|
this.snackBar.open(error, 'Danger');
|
|
},
|
|
});
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Recipe Template?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): RecipeTemplate {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name ?? '';
|
|
this.item.date = moment(formModel.date).format('DD-MMM-YYYY');
|
|
this.item.text = formModel.text ?? '';
|
|
this.item.selected = formModel.selected ?? false;
|
|
return this.item;
|
|
}
|
|
}
|