mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
21 lines
574 B
TypeScript
21 lines
574 B
TypeScript
import { inject } from '@angular/core';
|
|
import { ActivatedRouteSnapshot, Router } from '@angular/router';
|
|
import { availableBooks } from './book.model';
|
|
|
|
export const bookGuard = (
|
|
route: ActivatedRouteSnapshot,
|
|
router = inject(Router)
|
|
) => {
|
|
const searchParam = route.queryParams?.['book'].toLowerCase();
|
|
|
|
const isBookAvailable =
|
|
!!searchParam &&
|
|
availableBooks.some(
|
|
(b) =>
|
|
b.author.toLowerCase().includes(searchParam) ||
|
|
b.name.toLowerCase().includes(searchParam)
|
|
);
|
|
|
|
return isBookAvailable || router.parseUrl('no-result');
|
|
};
|