77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { Component, inject, afterNextRender, linkedSignal, input, computed } from '@angular/core';
|
|
import { form as createForm, FormField, FormRoot } from '@angular/forms/signals';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { Router } from '@angular/router';
|
|
|
|
import { CostCentre } from '../../core/cost-centre';
|
|
import { ErrorStateComponent } from '../../shared/error-state/error-state.component';
|
|
import { SkeletonLoaderComponent } from '../../shared/skeleton-loader/skeleton-loader.component';
|
|
import { CostCentreService } from '../cost-centre.service';
|
|
|
|
export interface CostCentreDetailFormData {
|
|
name: string;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-cost-centre-detail',
|
|
templateUrl: './cost-centre-detail.component.html',
|
|
styleUrls: ['./cost-centre-detail.component.css'],
|
|
imports: [
|
|
FormField,
|
|
FormRoot,
|
|
MatFormFieldModule,
|
|
MatInputModule,
|
|
MatButtonModule,
|
|
SkeletonLoaderComponent,
|
|
ErrorStateComponent,
|
|
],
|
|
})
|
|
export class CostCentreDetailComponent {
|
|
id = input(null, { transform: (v: string | null | undefined) => v ?? null });
|
|
|
|
private router = inject(Router);
|
|
private snackBar = inject(MatSnackBar);
|
|
private ser = inject(CostCentreService);
|
|
|
|
itemResource = this.ser.get(this.id);
|
|
|
|
item = computed(() => this.itemResource.value() ?? new CostCentre());
|
|
|
|
model = linkedSignal({
|
|
source: this.item,
|
|
computation: (itemVal): CostCentreDetailFormData => ({
|
|
name: itemVal.name ?? '',
|
|
}),
|
|
});
|
|
|
|
form = createForm(this.model);
|
|
|
|
constructor() {
|
|
afterNextRender(() => {
|
|
this.form().focusBoundControl();
|
|
});
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('', 'Success');
|
|
this.router.navigateByUrl('/cost-centres');
|
|
},
|
|
error: (error) => {
|
|
this.snackBar.open(error as string, 'Danger');
|
|
},
|
|
});
|
|
}
|
|
|
|
getItem(): CostCentre {
|
|
const formModel = this.model();
|
|
const item = this.item();
|
|
item.name = formModel.name ?? '';
|
|
return item;
|
|
}
|
|
}
|