Skip to content

Commit

Permalink
chore: Upgrade to Angular 18 - Handle new return type of guards
Browse files Browse the repository at this point in the history
- replaced `Observable<boolean | UrlTree> with combined `Observable<GuardResult>` from @angular/router
  for more, see: angular/angular#45023
  • Loading branch information
pawelfras committed Oct 10, 2024
1 parent 6518611 commit c6b88bc
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 99 deletions.
4 changes: 2 additions & 2 deletions feature-libs/cart/base/core/guards/cart-validation.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import {
ActiveCartFacade,
CartValidationFacade,
Expand Down Expand Up @@ -37,7 +37,7 @@ export class CartValidationGuard {

protected GLOBAL_MESSAGE_TIMEOUT = 10000;

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return !this.cartConfigService.isCartValidationEnabled()
? of(true)
: this.cartValidationService.validateCart().pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router, UrlTree } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
CheckoutAuthGuard,
Expand Down Expand Up @@ -48,7 +48,7 @@ export class CheckoutB2BAuthGuard extends CheckoutAuthGuard {
);
}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return combineLatest([
this.authService.isUserLoggedIn(),
this.activeCartFacade.isGuestCart(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable, inject, isDevMode } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { inject, Injectable, isDevMode } from '@angular/core';
import { ActivatedRouteSnapshot, GuardResult, Router } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
CheckoutCostCenterFacade,
Expand All @@ -23,7 +23,7 @@ import {
CheckoutStepType,
} from '@spartacus/checkout/base/root';
import { LoggerService, RoutingConfigService } from '@spartacus/core';
import { Observable, combineLatest, of } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { filter, map, switchMap, take, tap } from 'rxjs/operators';

@Injectable({
Expand Down Expand Up @@ -54,7 +54,7 @@ export class CheckoutB2BStepsSetGuard extends CheckoutStepsSetGuard {
);
}

canActivate(route: ActivatedRouteSnapshot): Observable<boolean | UrlTree> {
canActivate(route: ActivatedRouteSnapshot): Observable<GuardResult> {
let currentIndex = -1;
const currentRouteUrl = '/' + route.url.join('/');

Expand Down Expand Up @@ -99,7 +99,7 @@ export class CheckoutB2BStepsSetGuard extends CheckoutStepsSetGuard {
protected isB2BStepSet(
step: CheckoutStep,
isAccountPayment: boolean
): Observable<boolean | UrlTree> {
): Observable<GuardResult> {
if (step && !step.disabled) {
switch (step.type[0]) {
case CheckoutStepType.PAYMENT_TYPE: {
Expand All @@ -122,9 +122,7 @@ export class CheckoutB2BStepsSetGuard extends CheckoutStepsSetGuard {
return of(true);
}

protected isPaymentTypeSet(
step: CheckoutStep
): Observable<boolean | UrlTree> {
protected isPaymentTypeSet(step: CheckoutStep): Observable<GuardResult> {
return this.checkoutPaymentTypeFacade.getSelectedPaymentTypeState().pipe(
filter((state) => !state.loading),
map((state) => state.data),
Expand All @@ -141,7 +139,7 @@ export class CheckoutB2BStepsSetGuard extends CheckoutStepsSetGuard {
protected isDeliveryAddressAndCostCenterSet(
step: CheckoutStep,
isAccountPayment: boolean
): Observable<boolean | UrlTree> {
): Observable<GuardResult> {
return combineLatest([
this.checkoutDeliveryAddressFacade.getDeliveryAddressState().pipe(
filter((state) => !state.loading),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import { ActiveCartFacade, Cart } from '@spartacus/cart/base/root';
import { SemanticPathService } from '@spartacus/core';
import { Observable } from 'rxjs';
Expand All @@ -21,7 +21,7 @@ export class CartNotEmptyGuard {
protected router: Router
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.activeCartFacade.takeActive().pipe(
map((cart) => {
if (this.isEmpty(cart)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router, UrlTree } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
AuthRedirectService,
Expand All @@ -29,7 +29,7 @@ export class CheckoutAuthGuard {
protected router: Router
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return combineLatest([
this.authService.isUserLoggedIn(),
this.activeCartFacade.isGuestCart(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

import { inject, Injectable, isDevMode, OnDestroy } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import {
ActivatedRouteSnapshot,
GuardResult,
Router,
UrlTree,
} from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
CheckoutDeliveryAddressFacade,
Expand Down Expand Up @@ -65,7 +70,7 @@ export class CheckoutStepsSetGuard implements OnDestroy {
});
}

canActivate(route: ActivatedRouteSnapshot): Observable<boolean | UrlTree> {
canActivate(route: ActivatedRouteSnapshot): Observable<GuardResult> {
let currentIndex = -1;
const currentRouteUrl = '/' + route.url.join('/');

Expand Down Expand Up @@ -98,7 +103,7 @@ export class CheckoutStepsSetGuard implements OnDestroy {
);
}

protected isStepSet(step: CheckoutStep): Observable<boolean | UrlTree> {
protected isStepSet(step: CheckoutStep): Observable<GuardResult> {
if (step && !step.disabled) {
switch (step.type[0]) {
case CheckoutStepType.DELIVERY_ADDRESS: {
Expand Down Expand Up @@ -126,9 +131,7 @@ export class CheckoutStepsSetGuard implements OnDestroy {
return of(true);
}

protected isDeliveryAddress(
step: CheckoutStep
): Observable<boolean | UrlTree> {
protected isDeliveryAddress(step: CheckoutStep): Observable<GuardResult> {
return this.checkoutDeliveryAddressFacade.getDeliveryAddressState().pipe(
filter((state) => !state.loading),
map((state) => state.data),
Expand All @@ -142,19 +145,15 @@ export class CheckoutStepsSetGuard implements OnDestroy {
);
}

protected isDeliveryModeSet(
step: CheckoutStep
): Observable<boolean | UrlTree> {
protected isDeliveryModeSet(step: CheckoutStep): Observable<GuardResult> {
return this.checkoutDeliveryModesFacade.getSelectedDeliveryModeState().pipe(
filter((state) => !state.loading),
map((state) => state.data),
map((mode) => (mode ? true : this.getUrl(step.routeName)))
);
}

protected isPaymentDetailsSet(
step: CheckoutStep
): Observable<boolean | UrlTree> {
protected isPaymentDetailsSet(step: CheckoutStep): Observable<GuardResult> {
return this.checkoutPaymentFacade.getPaymentDetailsState().pipe(
filter((state) => !state.loading),
map((state) => state.data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router, UrlTree } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import { CheckoutStepType } from '@spartacus/checkout/base/root';
import { RoutingConfigService } from '@spartacus/core';
Expand Down Expand Up @@ -38,7 +38,7 @@ export class CheckoutGuard {
protected checkoutStepService: CheckoutStepService
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
const expressCheckout$ = this.expressCheckoutService
.trySetDefaultCheckoutDetails()
.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
AuthService,
Expand All @@ -26,7 +26,7 @@ export class NotCheckoutAuthGuard {
protected router: Router
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.authService.isUserLoggedIn().pipe(
map((isLoggedIn) => {
if (isLoggedIn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import { SemanticPathService } from '@spartacus/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
Expand All @@ -21,7 +21,7 @@ export class OrderCancellationGuard {
protected router: Router
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.orderAmendService.getForm().pipe(
map((form) => {
if (!form.valid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import { SemanticPathService } from '@spartacus/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
Expand All @@ -21,7 +21,7 @@ export class OrderReturnGuard {
protected router: Router
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.orderAmendService.getForm().pipe(
map((form) => {
if (!form.valid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { GuardResult, Router } from '@angular/router';
import { SemanticPathService } from '@spartacus/core';
import { OrderFacade } from '@spartacus/order/root';
import { Observable } from 'rxjs';
Expand All @@ -21,7 +21,7 @@ export class OrderConfirmationGuard {
protected semanticPathService: SemanticPathService
) {}

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.orderFacade.getOrderDetails().pipe(
map((orderDetails) => {
if (orderDetails && Object.keys(orderDetails).length !== 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { inject, Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { ActivatedRouteSnapshot, GuardResult, Router } from '@angular/router';
import {
isNotUndefined,
Product,
Expand Down Expand Up @@ -36,9 +36,7 @@ export class ProductMultiDimensionalSelectorGuard {
inject(SemanticPathService);
protected router: Router = inject(Router);

canActivate(
activatedRoute: ActivatedRouteSnapshot
): Observable<boolean | UrlTree> {
canActivate(activatedRoute: ActivatedRouteSnapshot): Observable<GuardResult> {
const productCode = activatedRoute.params?.productCode;
if (!productCode) {
return of(false);
Expand All @@ -64,7 +62,7 @@ export class ProductMultiDimensionalSelectorGuard {
* Finds a valid product code from variant options and returns a URL tree for redirection.
*
* @param {Product} product - The product with variant options.
* @returns {Observable<boolean | UrlTree>} - An observable that resolves to a `UrlTree` for
* @returns {Observable<GuardResult>} - An observable that resolves to a `UrlTree` for
* redirection if a valid product code is found, or `false` if no valid code is available.
*
* @description
Expand All @@ -74,7 +72,7 @@ export class ProductMultiDimensionalSelectorGuard {
*/
protected findValidProductCodeAndReturnUrlTree(
product: Product
): Observable<boolean | UrlTree> {
): Observable<GuardResult> {
const validVariantCode = this.getValidVariantCode(product);
const fallbackProductCode = this.getFallbackProductCode(product);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { ActivatedRouteSnapshot, GuardResult, Router } from '@angular/router';
import {
isNotUndefined,
Product,
Expand All @@ -29,9 +29,7 @@ export class ProductVariantsGuard {
protected semanticPathService: SemanticPathService,
protected router: Router
) {}
canActivate(
activatedRoute: ActivatedRouteSnapshot
): Observable<boolean | UrlTree> {
canActivate(activatedRoute: ActivatedRouteSnapshot): Observable<GuardResult> {
const productCode = activatedRoute.params?.productCode;
if (!productCode) {
return of(true);
Expand Down
4 changes: 2 additions & 2 deletions feature-libs/quote/components/cart-guard/quote-cart.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { inject, Injectable } from '@angular/core';
import { CanActivate, Router, UrlTree } from '@angular/router';
import { CanActivate, GuardResult, Router } from '@angular/router';

import {
RouterState,
Expand All @@ -31,7 +31,7 @@ export class QuoteCartGuard implements CanActivate {
protected router = inject(Router);
protected semanticPathService = inject(SemanticPathService);

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return combineLatest([
this.quoteCartService.isQuoteCartActive(),
this.quoteCartService.getQuoteId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable, inject } from '@angular/core';
import { CanActivate, UrlTree } from '@angular/router';
import { CanActivate, GuardResult } from '@angular/router';
import { GigyaRaasComponentData } from '@spartacus/cdc/core';
import {
AuthGuard,
Expand All @@ -26,7 +26,7 @@ export class GigyaRaasGuard implements CanActivate {
protected authGuard = inject(AuthGuard);
protected notAuthGuard = inject(NotAuthGuard);

canActivate(): Observable<boolean | UrlTree> {
canActivate(): Observable<GuardResult> {
return this.getComponentData().pipe(
switchMap((componentData) => {
if (Object.keys(componentData).length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable, inject, isDevMode } from '@angular/core';
import { RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { GuardResult, Router, RouterStateSnapshot } from '@angular/router';
import {
AuthRedirectService,
AuthService,
Expand All @@ -29,7 +29,7 @@ export class OppsLoginRequiredGuard {
canActivate(
route: CmsActivatedRouteSnapshot,
_state: RouterStateSnapshot
): Observable<boolean | UrlTree> {
): Observable<GuardResult> {
const navigation = this.router.getCurrentNavigation();
return this.authService.isUserLoggedIn().pipe(
take(1),
Expand Down
Loading

0 comments on commit c6b88bc

Please sign in to comment.