Force Languages to refresh after Login and reset active language if initial language not in new list

Viewed 96

We have a custom back-end language functionality, for which the back-end returns a smaller list of languages based on some permissions after the user logs in.

Currently, in the Spartacus language.reducer, on success, the language entities are extended, so I cannot override the language entities, to be fewer.

What would be the best approach and where to dispatch LoadLanguages action (to request the new list of languages) and save the list, and also setting the active language from the user languages list? (Initial active language, might not be in the new list, so this should be changed also).

I tried dispatching SiteContextActions.LoadLanguages on login/logout, however CmsActions.LoadCmsPageData is already dispatched at this point in Spartacus page.effects, and requests are already active.

  refreshLanguages$ = createEffect(() =>
    this.actions$.pipe(
      ofType(AuthActions.LOGIN, AuthActions.LOGOUT),
      switchMap(() =>
        this.routingService.getRouterState().pipe(
          filter(
            routerState =>
              routerState &&
              routerState.state &&
              routerState.state.cmsRequired &&
              !routerState.nextState
          ),
          take(1),
          map(routerState => routerState.state.context),
          mergeMap(context => of(
            new SiteContextActions.LoadLanguages(),
          ))
        )
      )
    )
  );
0 Answers
Related