From 1ed9186912c8461e06a97d9586e61b3bc075d12f Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Tue, 14 Jan 2025 08:40:03 +0100 Subject: [PATCH] add contact --- .../hotel/component/edit-hotel.component.ts | 80 ++++++++++++++++++- src/app/hotel/model/hotel.ts | 2 + src/app/hotel/service/HotelData.service.ts | 16 +++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/src/app/hotel/component/edit-hotel.component.ts b/src/app/hotel/component/edit-hotel.component.ts index 7ee3180..b278c2a 100644 --- a/src/app/hotel/component/edit-hotel.component.ts +++ b/src/app/hotel/component/edit-hotel.component.ts @@ -10,7 +10,7 @@ import { } from "@angular/forms"; import {Hotel} from "../model/hotel"; import {RouterLink} from "@angular/router"; -import {NgForOf} from "@angular/common"; +import {NgForOf, NgIf} from "@angular/common"; import {debounceTime} from "rxjs"; import {ErrorMsgComponent} from "./error-msg.component"; @@ -23,6 +23,7 @@ import {ErrorMsgComponent} from "./error-msg.component"; RouterLink, NgForOf, ErrorMsgComponent, + NgIf, ], template: ` @@ -45,6 +46,46 @@ import {ErrorMsgComponent} from "./error-msg.component";
+
+
+ + +
+ + +
+ + + +
+
+ + @if (activeContact === 'email') { + +
+ +
+ +
+ +
+ +
+ + } @else if (activeContact === 'phoneNumber') { + +
+ +
+ +
+ +
+ +
+ + } +

@@ -80,7 +121,17 @@ export class EditHotelComponent implements OnInit { max: 'Value must be less than or equal to 5' }; + activeContact!: string; + ngOnInit(): void { + if (this.hotel?.email) { + this.activeContact = 'email'; + } else if (this.hotel?.phoneNumber) { + this.activeContact = 'phoneNumber'; + } else { + this.activeContact = 'none'; + } + const tags = []; for (const tag of this.hotel?.tags ?? []) { @@ -92,6 +143,10 @@ export class EditHotelComponent implements OnInit { description: new FormControl(this.hotel?.description, [Validators.required]), price: new FormControl(this.hotel?.price, [Validators.required]), rating: new FormControl(this.hotel?.rating, [Validators.required, Validators.min(0), Validators.max(5)]), + email: new FormControl(this.hotel?.email, [Validators.required, Validators.email]), + emailConfirm: new FormControl(this.hotel?.email, [Validators.required, Validators.email]), + 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)]), tags: new FormArray(tags), }) @@ -118,6 +173,21 @@ export class EditHotelComponent implements OnInit { return; } + switch (this.activeContact) { + case 'email': + if (this.form.value.email !== this.form.value.emailConfirm) { + this.errorMsgs['email'] = 'Emails do not match'; + return; + } + break; + case 'phoneNumber': + if (this.form.value.phoneNumber !== this.form.value.phoneNumberConfirm) { + this.errorMsgs['phoneNumber'] = 'Phone numbers do not match'; + return; + } + break; + } + const hotel: Hotel = { imageUrl: this.hotel?.imageUrl ?? "", hotelName: this.form.value.name, @@ -125,7 +195,9 @@ export class EditHotelComponent implements OnInit { price: this.form.value.price, rating: this.form.value.rating, id: this.hotel?.id ?? 0, - tags: this.form.value.tags ?? [] + tags: this.form.value.tags ?? [], + email: this.form.value.email, + phoneNumber: this.form.value.phoneNumber }; this.updateHotel.emit(hotel) @@ -159,4 +231,8 @@ export class EditHotelComponent implements OnInit { this.setErrorMessage(controlName, control); }); } + + changeContactType(type: string) { + this.activeContact = type; + } } diff --git a/src/app/hotel/model/hotel.ts b/src/app/hotel/model/hotel.ts index dc0f387..5a8c215 100644 --- a/src/app/hotel/model/hotel.ts +++ b/src/app/hotel/model/hotel.ts @@ -2,6 +2,8 @@ export interface Hotel { id: number; hotelName: string; description: string; + email: string|null, + phoneNumber: string|null, price: number; imageUrl: string; rating: number; diff --git a/src/app/hotel/service/HotelData.service.ts b/src/app/hotel/service/HotelData.service.ts index 10cb315..ba59218 100644 --- a/src/app/hotel/service/HotelData.service.ts +++ b/src/app/hotel/service/HotelData.service.ts @@ -12,7 +12,9 @@ export class HotelDataService implements InMemoryDbService{ "price": 230.5, "imageUrl": "assets/img/heisenberg.jpg", "rating": 3.5, - "tags": ["Meer", "Berge"] + "tags": ["Meer", "Berge"], + "email": "buea@mail.com", + "phoneNumber": "1234567890" }, { "id": 2, @@ -21,7 +23,9 @@ export class HotelDataService implements InMemoryDbService{ "price": 145.5, "imageUrl": "assets/img/kjan.png", "rating": 5, - "tags": ["Meer", "Berge"] + "tags": ["Meer", "Berge"], + "email": "marrakesch@mail.com", + "phoneNumber": "1234567890" }, { "id": 3, @@ -30,7 +34,9 @@ export class HotelDataService implements InMemoryDbService{ "price": 120.12, "imageUrl": "assets/img/huy.png", "rating": 4, - "tags": ["Meer", "Berge"] + "tags": ["Meer", "Berge"], + "email": "abuja@mail.com", + "phoneNumber": "1234567890" }, { "id": 4, @@ -39,7 +45,9 @@ export class HotelDataService implements InMemoryDbService{ "price": 135.12, "imageUrl": "assets/img/rat.png", "rating": 2.5, - "tags": ["Meer", "Berge"] + "tags": ["Meer", "Berge"], + "email": "our@mail.com", + "phoneNumber": "1234567890" } ];