-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Angular 15): replace class-based route guards by functional …
…guards BREAKING CHANGE: Replaced class-based route guards by functional guards (see [Migrations / 3.3 to 4.0](https://github.com/intershop/intershop-pwa/blob/develop/docs/guides/migrations.md#33-to-40) for more details).
- Loading branch information
Showing
56 changed files
with
504 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 8 additions & 9 deletions
17
projects/organization-management/src/app/guards/fetch-cost-centers.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router'; | ||
import { inject } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { Observable, of } from 'rxjs'; | ||
|
||
import { loadCostCenters } from '../store/cost-centers'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class FetchCostCentersGuard implements CanActivate { | ||
constructor(private store: Store) {} | ||
/** | ||
* Fetch cost centers for cost center management page | ||
*/ | ||
export function fetchCostCentersGuard(): boolean | Observable<boolean> { | ||
const store = inject(Store); | ||
|
||
canActivate(_: ActivatedRouteSnapshot): boolean | Observable<boolean> { | ||
this.store.dispatch(loadCostCenters()); | ||
return of(true); | ||
} | ||
store.dispatch(loadCostCenters()); | ||
return of(true); | ||
} |
32 changes: 16 additions & 16 deletions
32
projects/organization-management/src/app/guards/fetch-users.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router'; | ||
import { inject } from '@angular/core'; | ||
import { ActivatedRouteSnapshot } from '@angular/router'; | ||
import { Store, select } from '@ngrx/store'; | ||
import { Observable } from 'rxjs'; | ||
import { map, tap } from 'rxjs/operators'; | ||
|
||
import { getUserCount, loadUsers } from '../store/users'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class FetchUsersGuard implements CanActivate { | ||
constructor(private store: Store) {} | ||
/** | ||
* Fetch users for user management page | ||
*/ | ||
export function fetchUsersGuard(route: ActivatedRouteSnapshot): boolean | Observable<boolean> { | ||
const store = inject(Store); | ||
|
||
canActivate(route: ActivatedRouteSnapshot): boolean | Observable<boolean> { | ||
return this.store.pipe( | ||
select(getUserCount), | ||
tap(count => { | ||
if (count <= 1 || !route.data.onlyInitialUsers) { | ||
this.store.dispatch(loadUsers()); | ||
} | ||
}), | ||
map(() => true) | ||
); | ||
} | ||
return store.pipe( | ||
select(getUserCount), | ||
tap(count => { | ||
if (count <= 1 || !route.data.onlyInitialUsers) { | ||
store.dispatch(loadUsers()); | ||
} | ||
}), | ||
map(() => true) | ||
); | ||
} |
20 changes: 10 additions & 10 deletions
20
projects/organization-management/src/app/guards/redirect-first-to-parent.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; | ||
import { inject } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class RedirectFirstToParentGuard implements CanActivate { | ||
constructor(private router: Router) {} | ||
/** | ||
* Redirects the user to the parent page if the requested page is the starting page (first page the user requested) | ||
*/ | ||
export function redirectFirstToParentGuard(_: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree { | ||
const router = inject(Router); | ||
|
||
canActivate(_: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree { | ||
if (!this.router.navigated) { | ||
return this.router.parseUrl(state.url.replace(/\/\w+$/, '')); | ||
} | ||
return true; | ||
if (!router.navigated) { | ||
return router.parseUrl(state.url.replace(/\/\w+$/, '')); | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.