Files
brewman/overlord/src/app/recipe-template/recipe-template-detail/recipe-template-detail.component.ts
T
tanshu 4571b31fc0 Chore: Check for deprecations using eslint plugin
Chore: environment file replacement stopped.
Chore: All components are now standalone and removed all modules
Chore: App routing is now in app.routes and similarly for all submodules
Chore: Http interceptors are now functional and split refresh interceptor into a separate one.
2024-05-31 22:54:41 +05:30

139 lines
4.1 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 } 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<string>;
date: FormControl<Date>;
text: FormControl<string>;
selected: FormControl<boolean>;
}>;
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<string>('', { nonNullable: true }),
date: new FormControl(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').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;
}
}