export function changeAccount()

in src/actions/app-actions.ts [447:504]


export function changeAccount(
  account: StorageState,
  removeCurrentAccount: boolean,
  issueId?: string,
  articleId?: string,
  navigateToActivity?: string,
  searchQuery?: string,
  helpdeskFormId?: string,
): ReduxAction {
  return async (dispatch: ReduxThunkDispatch, getState: ReduxStateGetter) => {
    const state: AppState = getState();
    const config: AppConfig = (account.config as any) as AppConfig;
    const authParams: AuthParams | null = await getStoredSecurelyAuthParams(account.authParamsKey);

    if (!authParams) {
      const errorMessage: string = i18n(
        'The selected account is no longer authorized to use YouTrack Mobile. Please log in again.',
      );
      notify(errorMessage);
      throw new Error(errorMessage);
    }

    redirectToHome(account.config!.backendUrl);
    const auth: OAuth2 = createAuthInstance(config);
    dispatch(beginAccountChange());

    try {
      const userLocale: UserGeneralProfileLocale | null | undefined =
        account.currentUser?.ytCurrentUser?.profiles?.general?.locale;

      if (userLocale) {
        loadTranslation(userLocale?.locale, userLocale?.language);
      }

      await dispatch(updateOtherAccounts(account, removeCurrentAccount));
      await auth.cacheAuthParams(
        authParams as any,
        account.authParamsKey ||
        account.creationTimestamp!.toString(),
      );
      await storeConfig(config);
      await dispatch(initializeAuth(config));
      await dispatch(loadCurrentHubUserAndSetAPI());
      await dispatch(checkUserAgreement());

      if (!state.app.showUserAgreement) {
        dispatch(completeInitialization(issueId, articleId, navigateToActivity, searchQuery, false, helpdeskFormId));
      }

      log.info('App Actions: Account changed');
    } catch (err) {
      notifyError(err as AnyError);
      throw err;
    }

    dispatch(endAccountChange());
  };
}