libs/cart/routing/src/guards/cart-items/cart-items.guard.ts
import {
Injectable,
Inject,
} from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import {
tap,
take,
map,
} from 'rxjs/operators';
import { DaffCartFacade } from '@daffodil/cart/state';
import { DaffCartItemsGuardRedirectUrl } from './cart-items-guard-redirect.token';
/**
* A routing guard that will ensure that the cart is not empty before allowing activation of a route.
* If the cart has items in it, then `canActivate` will emit true. If not, it will emit false and redirect to a specific path.
* The url is `/` by default but can be overridden with the {@link DaffCartItemsGuardRedirectUrl} injection token.
* The guard will not wait until the cart has been resolved before performing the check and emitting.
* Ensure that the cart is resolved prior to running this guard with the {@link DaffResolvedCartGuard}.
*/
@Injectable({
providedIn: 'root',
})
export class DaffCartItemsGuard {
constructor(
private facade: DaffCartFacade,
private router: Router,
@Inject(DaffCartItemsGuardRedirectUrl) private redirectUrl: string,
) {}
canActivate(): Observable<boolean> {
return this.facade.isCartEmpty$.pipe(
map(isCartEmpty => !isCartEmpty),
take(1),
tap(hasNonEmptyCart => {
if (!hasNonEmptyCart) {
this.router.navigateByUrl(this.redirectUrl);
}
}),
);
}
}