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 } from '@angular/material/card'; import { MatCheckbox } from '@angular/material/checkbox'; import { MatDatepickerInput, MatDatepickerToggle, MatDatepicker } from '@angular/material/datepicker'; import { MatDialog } from '@angular/material/dialog'; import { MatFormField, MatLabel, MatSuffix } from '@angular/material/form-field'; import { MatInput } from '@angular/material/input'; import { ActivatedRoute, Router } from '@angular/router'; import moment from 'moment'; import { ToasterService } from '../../core/toaster.service'; 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'], standalone: true, imports: [ MatCard, MatCardHeader, MatCardTitle, MatCardContent, ReactiveFormsModule, MatFormField, MatLabel, MatInput, MatDatepickerInput, MatDatepickerToggle, MatSuffix, MatDatepicker, MatCheckbox, MatCardActions, MatButton, ], }) export class RecipeTemplateDetailComponent implements OnInit, AfterViewInit { @ViewChild('nameElement', { static: true }) nameElement?: ElementRef; form: FormGroup<{ name: FormControl; date: FormControl; text: FormControl; selected: FormControl; }>; item: RecipeTemplate = new RecipeTemplate(); constructor( private route: ActivatedRoute, private router: Router, private dialog: MatDialog, private toaster: ToasterService, private ser: RecipeTemplateService, ) { this.form = new FormGroup({ name: new FormControl('', { nonNullable: true }), date: new FormControl(new Date(), { nonNullable: true }), text: new FormControl('', { nonNullable: true }), selected: new FormControl(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').toDate(), text: this.item.text, selected: this.item.selected, }); } ngAfterViewInit() { setTimeout(() => { if (this.nameElement) { this.nameElement.nativeElement.focus(); } }, 0); } save() { this.ser.saveOrUpdate(this.getItem()).subscribe({ next: () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/recipe-templates'); }, error: (error) => { this.toaster.show('Danger', error); }, }); } delete() { this.ser.delete(this.item.id).subscribe({ next: () => { this.toaster.show('Success', ''); this.router.navigateByUrl('/recipe-templates'); }, error: (error) => { this.toaster.show('Danger', error); }, }); } 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; } }