How to rewrite rxjs code without making calls within the subcribe method?

Hi I am having an angular 5 project and rxjs library used. i have code snippet below. I would like to rewrite in a better way.

 export class AccountDetailsComponent implements OnInit {
     ngOnInit() {
        this.route.paramMap
          .pipe(
            tap(paramMap => { this.id = paramMap.get('id') }),
            switchMap(paramMap =>
              forkJoin(
                this.accountsService.get(paramMap.get('id')).pipe(
                  catchError(error => {
                    this.notFoundService.checkStatus(error);
                    return _throw(error);
                  })
                ),
                this.accountsService.getAccountUsers(paramMap.get('id'))
                )
              )
            )
          .subscribe(
            ([account, users]) => {
              this.isLoading = false;
              this.account = account;
    
             let hasTransactionalReportingEnabled : boolean = this.account.features.some( feature => feature.code === FeatureCode.TRANSACTIONAL_REPORTING);
    
              if(hasTransactionalReportingEnabled) {
    
                this.accountsService.getAccountEsmeGroup(this.id).subscribe(
                   (accountEsmeGroup) => {
                     this.accountEsmeGroup = accountEsmeGroup;
                   }
                );
              }
              this.users = users;
            },
            () => {
              this.isLoading = false;
            }
          );
      }
    }

In the account-details.component.ts class have an ngOnInit() method. Its two http service calls using rxjs forkJoin function namely, this.accountsService.get(paramMap.get(‘id’)) & this.accountsService.getAccountUsers(paramMap.get(‘id’)) . Then in the subcribe I am making another call this.accountsService.getAccountEsmeGroup(this.id), only if the variable hasTransactionalReportingEnabled expression is true. Is there any better way to rewrite the above snippet of code in rxjs. I am not sure it is good idea to make another call in the subscribe method.

appreciate if you can help

thank you

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.