0

I have the following resolver that calls an api service. based on the result I get back I want to redirect the user to specific screen:

@Injectable()
export class TenantStatusResolver implements Resolve<any> {

  tenantStatus: OnboardStatusEnum = OnboardStatusEnum.TenantyType;

  constructor(
    private _httpClient: HttpClient,
    private _router: Router) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {

    if (this.tenantStatus === OnboardStatusEnum.Completed) {
      return;
    }

    return new Promise((resolve, reject) => {
      this._httpClient.get(environment.api.onboarding.tenantStatus).subscribe((response: TenantOnboardStatusDto) => {
        this.tenantStatus = response.onboardStatus;
        this._router.navigate(["/main/onboarding/tenant-type"]);
        resolve();
      }, reject);
    });

  }
}

and in my main.module.ts I have

const routes = [
    {
      path: '',
      resolve: {
        tenantStatus: TenantStatusResolver
      },
      children: [
        {
          path        : 'onboarding',
          loadChildren: './onboarding/onboarding.module#OnboardingModule',
          canLoad: [AuthenticationRouteGuard]
        },

Problem is that resolver gets in an infinite loop. What is the issue here?

pantonis
  • 5,601
  • 12
  • 58
  • 115
  • routing shouldn't happen in a resolver anyway. resolver is to resolve data for your component. Sounds like you want to use a guard. – AT82 Sep 24 '19 at 18:00
  • From what I understand Guards are used for A&A. This is a business rule check that redirects the user to specific page to complete registration. – pantonis Sep 24 '19 at 18:05
  • Well however you want to solve this, my opinion is that a resolver is not the correct way to go. Its purpose is to only preload data that a component needs before rendering the component. I would use a guard for this, that's my two cents :) The purose here seems to be that not allow user to route if some condition. Exactly what guards would be for, I'd say :) – AT82 Sep 24 '19 at 18:14
  • but resolvers are type of guards if I am not mistaken. – pantonis Sep 24 '19 at 18:20
  • in the angular docs it kinda states that it is for pre-fetching data: https://angular.io/guide/router#resolve-pre-fetching-component-data – AT82 Sep 24 '19 at 18:23
  • But that is just my opinion. No sense "fighting" about it. Hopefully someone can help you with this issue and give you a solution :) – AT82 Sep 24 '19 at 18:27
  • I totally respect your opinion. thank you very much for your help. – pantonis Sep 24 '19 at 18:31
  • maybe if you could create a small stackblitz I'd be happy to take a look and try to help. I kinda tried to emulate the scenario with stackblitz from docs, but it does not seem to be caused by the resolver. Must be something else. See if you can reproduce your scenario: https://stackblitz.com/edit/angular-bbrw8e?file=src/app/crisis-center/crisis-detail-resolver.service.ts (look at the crisis resolver) – AT82 Sep 24 '19 at 18:42

0 Answers0