This commit is contained in:
Constantin Simonis 2025-02-04 09:14:52 +01:00
parent 44fa740ca3
commit 59e90dc534
Signed by: csimonis
GPG Key ID: 758DD9C506603183
3 changed files with 52 additions and 29 deletions

View File

@ -1,11 +1,12 @@
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import { import {
AbstractControl, AbstractControl,
FormArray, FormArray,
FormControl, FormControl,
FormGroup, FormGroup,
FormsModule, FormsModule,
ReactiveFormsModule, ValidationErrors, ReactiveFormsModule,
ValidationErrors,
Validators Validators
} from "@angular/forms"; } from "@angular/forms";
import {Hotel} from "../model/hotel"; import {Hotel} from "../model/hotel";
@ -132,6 +133,10 @@ export class EditHotelComponent implements OnInit {
activeContact!: string; activeContact!: string;
phoneNumberGroup!: FormGroup;
emailGroup!: FormGroup;
ngOnInit(): void { ngOnInit(): void {
if (this.hotel?.email) { if (this.hotel?.email) {
this.activeContact = 'email'; this.activeContact = 'email';
@ -152,16 +157,16 @@ export class EditHotelComponent implements OnInit {
description: new FormControl(this.hotel?.description, [Validators.required]), description: new FormControl(this.hotel?.description, [Validators.required]),
price: new FormControl(this.hotel?.price, [Validators.required]), price: new FormControl(this.hotel?.price, [Validators.required]),
rating: new FormControl(this.hotel?.rating, [Validators.required, Validators.min(0), Validators.max(5)]), rating: new FormControl(this.hotel?.rating, [Validators.required, Validators.min(0), Validators.max(5)]),
emailGroup: new FormGroup({
email: new FormControl(this.hotel?.email, [Validators.required, Validators.email]),
emailConfirm: new FormControl(this.hotel?.email, [Validators.required, Validators.email]),
}, {validators: this.emailMatch}),
phoneNumberGroup: new FormGroup({
phoneNumber: new FormControl(this.hotel?.phoneNumber, [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
phoneNumberConfirm: new FormControl(this.hotel?.phoneNumber, [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
}, {validators: this.phoneNumberMatch}),
tags: new FormArray(tags), tags: new FormArray(tags),
}) });
this.initGroups()
if (this.hotel?.email) {
this.form.addControl('emailGroup', this.emailGroup);
} else if (this.hotel?.phoneNumber) {
this.form.addControl('phoneNumberGroup', this.phoneNumberGroup);
}
Object.keys(this.form.controls).forEach(controlName => { Object.keys(this.form.controls).forEach(controlName => {
let debounce = 1000; let debounce = 1000;
@ -217,7 +222,6 @@ export class EditHotelComponent implements OnInit {
setErrorMessage(controlName: string, control: AbstractControl) { setErrorMessage(controlName: string, control: AbstractControl) {
this.errorMsgs[controlName] = Object.keys(control.errors ?? {}).map((key) => this.validationErrors[key]).join(' '); this.errorMsgs[controlName] = Object.keys(control.errors ?? {}).map((key) => this.validationErrors[key]).join(' ');
console.log(this.errorMsgs[controlName])
} }
showErrors() { showErrors() {
@ -232,6 +236,12 @@ export class EditHotelComponent implements OnInit {
} }
changeContactType(type: string) { changeContactType(type: string) {
if (type == 'email') {
this.form.addControl('emailGroup', this.emailGroup);
} else if (type == 'phoneNumber') {
this.form.addControl('phoneNumberGroup', this.phoneNumberGroup);
}
this.activeContact = type; this.activeContact = type;
} }
@ -264,4 +274,16 @@ export class EditHotelComponent implements OnInit {
return null; return null;
} }
private initGroups() {
this.emailGroup = new FormGroup({
email: new FormControl(this.hotel?.email, [Validators.required, Validators.email]),
emailConfirm: new FormControl(this.hotel?.email, [Validators.required, Validators.email]),
}, {validators: this.emailMatch});
this.phoneNumberGroup = new FormGroup({
phoneNumber: new FormControl(this.hotel?.phoneNumber, [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
phoneNumberConfirm: new FormControl(this.hotel?.phoneNumber, [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
}, {validators: this.phoneNumberMatch});
}
} }

View File

@ -1,4 +1,4 @@
import {Component, inject} from "@angular/core"; import {ChangeDetectionStrategy, Component, inject} from "@angular/core";
import {HotelComponent} from "./hotel.component"; import {HotelComponent} from "./hotel.component";
import {Hotel} from "../model/hotel"; import {Hotel} from "../model/hotel";
import {FormsModule} from "@angular/forms"; import {FormsModule} from "@angular/forms";
@ -32,7 +32,8 @@ import {StarComponent} from "./star.component";
`, `,
imports: [FormsModule, HotelComponent, AsyncPipe, CurrencyComponent, RouterLink, StarComponent, NgIf], imports: [FormsModule, HotelComponent, AsyncPipe, CurrencyComponent, RouterLink, StarComponent, NgIf],
providers: [HotelService], providers: [HotelService],
selector: 'app-hotels' selector: 'app-hotels',
changeDetection: ChangeDetectionStrategy.OnPush
}) })
export class HotelsComponent { export class HotelsComponent {

View File

@ -1,6 +1,6 @@
import {HttpClient} from "@angular/common/http"; import {HttpClient} from "@angular/common/http";
import {inject, Injectable} from "@angular/core"; import {inject, Injectable} from "@angular/core";
import {Observable} from "rxjs"; import {Observable, shareReplay} from "rxjs";
import {Hotel} from "../model/hotel"; import {Hotel} from "../model/hotel";
@Injectable({ @Injectable({
@ -10,11 +10,11 @@ export class HotelService {
private httpClient: HttpClient = inject(HttpClient); private httpClient: HttpClient = inject(HttpClient);
public getHotels(): Observable<Hotel[]> { public getHotels(): Observable<Hotel[]> {
return this.httpClient.get<Hotel[]>('/api/hotels'); return this.httpClient.get<Hotel[]>('/api/hotels').pipe(shareReplay());
} }
public getHotelById(id: number): Observable<Hotel> { public getHotelById(id: number): Observable<Hotel> {
return this.httpClient.get<Hotel>(`/api/hotels/${id}`); return this.httpClient.get<Hotel>(`/api/hotels/${id}`).pipe(shareReplay());
} }
public updateHotelById(hotel: Hotel): Observable<Object> { public updateHotelById(hotel: Hotel): Observable<Object> {