From 1f252a3d4b30d0cb145b961ed846b8681d480647 Mon Sep 17 00:00:00 2001 From: Natalia Venditto Date: Mon, 31 Jul 2023 16:41:36 +0200 Subject: [PATCH] feat(portal): use Angular Signals (#230) * feat(portal): migrate to signals (#223) * chore: merging to test in code spaces * fix: call discount signals in templates --------- Co-authored-by: Sumit Parakh Co-authored-by: Wassim Chegham --- .../portal/src/app/about/about.component.ts | 8 +--- .../checkoutpage/checkoutpage.component.html | 2 +- .../checkoutpage/checkoutpage.component.ts | 8 ++-- .../src/app/profile/profile.component.html | 18 ++++---- .../src/app/profile/profile.component.ts | 30 ++++++------ .../app/rentalpage/rentalpage.component.html | 46 +++++++++---------- .../app/rentalpage/rentalpage.component.ts | 21 +++++---- .../app/searchpage/searchpage.component.html | 4 +- .../app/searchpage/searchpage.component.ts | 20 ++++---- .../booking-form/booking-form.component.html | 36 +++++++-------- .../booking-form/booking-form.component.ts | 42 ++++++++--------- .../src/app/shared/card/card.component.html | 4 +- .../src/app/shared/card/card.component.ts | 12 +++-- .../errors/not-found/not-found.component.ts | 11 +---- .../favorite-button.component.html | 4 +- .../favorite-button.component.ts | 8 ++-- .../shared/main-nav/main-nav.component.html | 4 +- .../app/shared/main-nav/main-nav.component.ts | 8 ++-- .../shared/text-block/text-block.component.ts | 8 +--- packages/portal/src/types/index.d.ts | 2 + 20 files changed, 141 insertions(+), 155 deletions(-) diff --git a/packages/portal/src/app/about/about.component.ts b/packages/portal/src/app/about/about.component.ts index 4da30890..552100c7 100644 --- a/packages/portal/src/app/about/about.component.ts +++ b/packages/portal/src/app/about/about.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from "@angular/core"; +import { Component } from "@angular/core"; import { TextBlockComponent } from "../shared/text-block/text-block.component"; @Component({ @@ -8,10 +8,6 @@ import { TextBlockComponent } from "../shared/text-block/text-block.component"; standalone: true, imports: [TextBlockComponent], }) -export class AboutComponent implements OnInit { +export class AboutComponent { title = "About"; - - constructor() {} - - ngOnInit(): void {} } diff --git a/packages/portal/src/app/checkoutpage/checkoutpage.component.html b/packages/portal/src/app/checkoutpage/checkoutpage.component.html index 93270de0..d7141021 100644 --- a/packages/portal/src/app/checkoutpage/checkoutpage.component.html +++ b/packages/portal/src/app/checkoutpage/checkoutpage.component.html @@ -1,4 +1,4 @@ -
+

Thank you for your booking!

diff --git a/packages/portal/src/app/checkoutpage/checkoutpage.component.ts b/packages/portal/src/app/checkoutpage/checkoutpage.component.ts index 1c9fe477..e92455bd 100644 --- a/packages/portal/src/app/checkoutpage/checkoutpage.component.ts +++ b/packages/portal/src/app/checkoutpage/checkoutpage.component.ts @@ -1,5 +1,5 @@ import { CommonModule } from '@angular/common'; -import { Component, OnInit, inject } from '@angular/core'; +import { Component, OnInit, inject, signal } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { ActivatedRoute, RouterModule } from '@angular/router'; import { ReservationService } from '../shared/reservation.service'; @@ -12,16 +12,16 @@ import { ReservationService } from '../shared/reservation.service'; imports: [RouterModule, CommonModule, MatButtonModule], }) export class CheckoutpageComponent implements OnInit { - result = 'error'; + result = signal('error'); private route = inject(ActivatedRoute); private reservationService = inject(ReservationService); ngOnInit(): void { - this.result = this.route.snapshot.queryParams['result']; + this.result.set(this.route.snapshot.queryParams['result']); const reservationId = this.route.snapshot.queryParams['reservationId']; - if (reservationId && this.result === 'cancel') { + if (reservationId && this.result() === 'cancel') { this.reservationService.cancelReservation(reservationId); } } diff --git a/packages/portal/src/app/profile/profile.component.html b/packages/portal/src/app/profile/profile.component.html index a84b70c1..d40a4e84 100644 --- a/packages/portal/src/app/profile/profile.component.html +++ b/packages/portal/src/app/profile/profile.component.html @@ -5,28 +5,28 @@

My Profile

- +
-

+

You have no saved listings. Browse listings

@@ -60,8 +60,8 @@

My Profile

- No payments found. - + No payments found. +
{{ showAmount(payment.amount) }} {{ payment.currency }} (via {{ payment.provider }}) {{ payment.status }} @@ -79,8 +79,8 @@

My Profile

- No reservations found. - + No reservations found. +
{{ reservation.title }} ({{ reservation.guests }} diff --git a/packages/portal/src/app/profile/profile.component.ts b/packages/portal/src/app/profile/profile.component.ts index ebed8776..ad4c9491 100644 --- a/packages/portal/src/app/profile/profile.component.ts +++ b/packages/portal/src/app/profile/profile.component.ts @@ -1,5 +1,5 @@ import { CommonModule } from "@angular/common"; -import { Component, OnInit, inject } from "@angular/core"; +import { Component, OnInit, inject, signal } from "@angular/core"; import { MatButtonModule } from "@angular/material/button"; import { MatCardModule } from "@angular/material/card"; import { MatIconModule } from "@angular/material/icon"; @@ -20,11 +20,11 @@ import { UserService } from "../shared/user/user.service"; imports: [CommonModule, RouterModule, MatCardModule, MatListModule, MatIconModule, MatButtonModule, MatTabsModule], }) export class ProfileComponent implements OnInit { - user: User = {} as User; - listings: Listing[] = []; - reservations: Reservation[] = []; - payments: Payment[] = []; - selectedTabIndex = 0; + user = signal({} as User); + listings = signal([]); + reservations = signal([]); + payments = signal([]); + selectedTabIndex = signal(0); private route = inject(ActivatedRoute); private userService = inject(UserService); @@ -36,13 +36,13 @@ export class ProfileComponent implements OnInit { constructor() { this.userService.user$.subscribe(user => { - this.user = user; + this.user.set(user); }); } async ngOnInit() { this.route.data.subscribe(async data => { - this.user = data["user"]; + this.user.set(data["user"]); await this.listFavorites(); await this.listReservations(); await this.listPayments(); @@ -52,32 +52,32 @@ export class ProfileComponent implements OnInit { this.route.paramMap.subscribe(async params => { const tab = params.get("tab"); - this.selectedTabIndex = tabs.indexOf(tab || "favorites"); + this.selectedTabIndex.set(tabs.indexOf(tab || "favorites")); }); } async listFavorites() { // this call allows us to get the count of favorites - this.listings = await this.favoriteService.countFavoritesByUser(this.user); + this.listings.set(await this.favoriteService.countFavoritesByUser(this.user())); // this call allows us to get the actual listings - this.listings = await this.favoriteService.getFavoritesByUser(this.user); + this.listings.set(await this.favoriteService.getFavoritesByUser(this.user())); } async removeFavorite(listing: Listing) { // remove the listing from the UI - this.listings = this.listings.filter(l => l.id !== listing.id); + this.listings.update(listings => listings.filter(l => l.id !== listing.id)); // remove the favorite from the database - await this.favoriteService.removeFavorite(listing, this.user); + await this.favoriteService.removeFavorite(listing, this.user()); } async listReservations() { - this.reservations = await this.reservationService.getReservationsByUser(this.user); + this.reservations.set(await this.reservationService.getReservationsByUser(this.user())); } async listPayments() { - this.payments = await this.paymentService.getPaymentsByUser(this.user); + this.payments.set(await this.paymentService.getPaymentsByUser(this.user())); } async viewListing(listingId: string) { diff --git a/packages/portal/src/app/rentalpage/rentalpage.component.html b/packages/portal/src/app/rentalpage/rentalpage.component.html index 8b4492f7..f96b5bd2 100644 --- a/packages/portal/src/app/rentalpage/rentalpage.component.html +++ b/packages/portal/src/app/rentalpage/rentalpage.component.html @@ -2,38 +2,38 @@
-
+
-
{{ listing.reviews_number | i18nPlural : reviewsMapping }}
+
{{ listing().reviews_number | i18nPlural : reviewsMapping }}
@@ -42,7 +42,7 @@
-
-

{{ listing.title }}

+
+

{{ listing().title }}

- {{ listing.address[0] }}, {{ listing.address[1] }}, {{ listing.address[2] }}, - {{ listing.address[3] }} + {{ listing().address[0] }}, {{ listing().address[1] }}, {{ listing().address[2] }}, + {{ listing().address[3] }}

-
+

- {{ listing.description }} + {{ listing().description }}

-
+

Features

    -
  • +
  • {{ feature.split("|")[0] }} @@ -77,6 +77,6 @@

    Features

-
- +
+
diff --git a/packages/portal/src/app/rentalpage/rentalpage.component.ts b/packages/portal/src/app/rentalpage/rentalpage.component.ts index 5c424782..32e6628e 100644 --- a/packages/portal/src/app/rentalpage/rentalpage.component.ts +++ b/packages/portal/src/app/rentalpage/rentalpage.component.ts @@ -1,5 +1,5 @@ import { CommonModule } from "@angular/common"; -import { Component, OnInit, inject } from "@angular/core"; +import { Component, OnInit, inject, signal } from "@angular/core"; import { ActivatedRoute, Navigation, Router } from "@angular/router"; import { BookingFormComponent } from "../shared/booking-form/booking-form.component"; import { FavoriteButtonComponent } from "../shared/favorite-button/favorite-button/favorite-button.component"; @@ -17,13 +17,14 @@ import { UserRole, UserService } from "../shared/user/user.service"; }) export class RentalpageComponent implements OnInit { userRole: typeof UserRole = UserRole; - listing: Listing; user: User; navigation: Navigation | null; - reviewStars: number[] = []; - isLoading = true; reviewsMapping: { [k: string]: string } = { "=0": "No reviews", "=1": "1 message", other: "# reviews" }; + listing = signal({} as Listing); + reviewStars = signal([]); + isLoading = signal(true); + private router = inject(Router); private route = inject(ActivatedRoute); private listingService = inject(ListingService); @@ -31,7 +32,7 @@ export class RentalpageComponent implements OnInit { constructor() { this.navigation = this.router.getCurrentNavigation(); - this.listing = this.navigation?.extras.state?.["listing"] || null; + this.listing.set(this.navigation?.extras.state?.["listing"] || {}); this.user = this.navigation?.extras.state?.["user"] || null; } @@ -45,19 +46,19 @@ export class RentalpageComponent implements OnInit { const listing = await this.listingService.getListingById(this.route.snapshot.params["id"]); if (listing !== undefined) { - this.listing = listing; - this.isLoading = false; + this.listing.set(listing); + this.isLoading.set(false); } else { this.router.navigate(["/404"]); } - this.reviewStars = Array(5) + this.reviewStars.set(Array(5) .fill(0) - .map((x, i) => (i < this.listing?.reviews_stars ? 1 : 0)); + .map((x, i) => (i < this.listing().reviews_stars ? 1 : 0))); } async share() { - await this.listingService.share(this.listing); + await this.listingService.share(this.listing()); } async onRent(reservationDetails: ReservationRequest) { diff --git a/packages/portal/src/app/searchpage/searchpage.component.html b/packages/portal/src/app/searchpage/searchpage.component.html index 41e32b4a..9d5a7adb 100644 --- a/packages/portal/src/app/searchpage/searchpage.component.html +++ b/packages/portal/src/app/searchpage/searchpage.component.html @@ -12,9 +12,9 @@