76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import {Component, inject, OnInit, signal} from '@angular/core';
|
|
import {FormBuilder, ReactiveFormsModule, Validators} from '@angular/forms';
|
|
import {MatFormFieldModule} from '@angular/material/form-field';
|
|
import {MatInputModule} from '@angular/material/input';
|
|
import {MatButtonModule} from '@angular/material/button';
|
|
import {MatSelectModule} from '@angular/material/select';
|
|
import {AuthService} from '../../services/auth';
|
|
import {CreateMeteringPointRequest, MeteringPoint, MeteringPointService} from '../../services/metering-point';
|
|
|
|
@Component({
|
|
selector: 'app-metering-points',
|
|
standalone: true,
|
|
imports: [ReactiveFormsModule, MatFormFieldModule, MatInputModule, MatButtonModule, MatSelectModule],
|
|
templateUrl: './metering-points.html'
|
|
})
|
|
export class MeteringPointsComponent implements OnInit {
|
|
private fb = inject(FormBuilder);
|
|
private authService = inject(AuthService);
|
|
private meteringPointService = inject(MeteringPointService);
|
|
|
|
isAdmin = this.authService.currentUserRole() === 'ADMIN';
|
|
|
|
points = signal<MeteringPoint[]>([]);
|
|
isLoading = signal(true);
|
|
errorMessage = signal('');
|
|
|
|
addForm = this.fb.group({
|
|
atNumber: ['', [Validators.required, Validators.pattern(/^AT[0-9]{31}$/)]],
|
|
type: this.fb.control<CreateMeteringPointRequest['type']>('CONSUMER', {nonNullable: true, validators: Validators.required})
|
|
});
|
|
|
|
ngOnInit() {
|
|
this.loadPoints();
|
|
}
|
|
|
|
private loadPoints() {
|
|
this.isLoading.set(true);
|
|
const request$ = this.isAdmin
|
|
? this.meteringPointService.getAllMeteringPoints()
|
|
: this.meteringPointService.getMyMeteringPoints();
|
|
|
|
request$.subscribe({
|
|
next: points => {
|
|
this.points.set(points);
|
|
this.isLoading.set(false);
|
|
},
|
|
error: () => {
|
|
this.errorMessage.set('Zählpunkte konnten nicht geladen werden.');
|
|
this.isLoading.set(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
onSubmit() {
|
|
if (this.addForm.invalid) {
|
|
this.addForm.markAllAsTouched();
|
|
return;
|
|
}
|
|
|
|
const payload: CreateMeteringPointRequest = {
|
|
atNumber: this.addForm.value.atNumber!,
|
|
type: this.addForm.value.type!
|
|
};
|
|
|
|
this.meteringPointService.addMeteringPoint(payload).subscribe({
|
|
next: created => {
|
|
this.points.update(list => [...list, created]);
|
|
this.addForm.reset({atNumber: '', type: 'CONSUMER'});
|
|
},
|
|
error: err => {
|
|
this.errorMessage.set(err.error?.message || 'Zählpunkt konnte nicht hinzugefügt werden.');
|
|
}
|
|
});
|
|
}
|
|
}
|