export function setAccount()

in src/actions/app-actions.ts [1084:1142]


export function setAccount(notificationRouteData: NotificationRouteData | null): ReduxAction {
  return async (dispatch: ReduxThunkDispatch, getState: ReduxStateGetter) => {
    const state: StorageState = await storage.populateStorage();
    await dispatch(populateAccounts());
    const backendUrl: string | undefined = notificationRouteData?.backendUrl;

    if (
      backendUrl &&
      state?.config &&
      backendUrl !== state.config?.backendUrl
    ) {
      const notificationIssueAccount = await appActionsHelper.targetAccountToSwitchTo(
        backendUrl,
        state.config?.backendUrl,
      );

      if (notificationIssueAccount) {
        await dispatch(updateOtherAccounts(notificationIssueAccount));
        storage.flushStoragePart({
          config: notificationIssueAccount.config,
        });
      }
    }

    const targetConfig: AppConfig | null = storage.getStorageState().config;
    if (targetConfig) {
      log.info('App Actions(setAccount): config found', targetConfig);
      log.info(`App Actions(setAccount): push notification data: ${JSON.stringify(notificationRouteData)}`);
      dispatch(
        initializeApp(
          targetConfig,
          notificationRouteData?.issueId,
          notificationRouteData?.articleId,
          notificationRouteData?.navigateToActivity,
        ),
      );
    } else {
      log.info('App Actions: App is not configured, entering server URL');

      const navigateTo = (serverUrl: string | null) =>
        Router.EnterServer({
          serverUrl,
        });

      try {
        const url = await Linking.getInitialURL();

        if (!url) {
          navigateTo(null);
        } else {
          const host = UrlParse(url).host;
          navigateTo(host);
        }
      } catch (e) {
        navigateTo(null);
      }
    }
  };
}