<commit_before>import {
  INIT_STATE,
  SOCKET_CONNECT,
  SOCKET_RECONNECT,
  AUTH_STRATEGIES,
  REGISTER_START,
  REGISTER_COMPLETE,
  LOGIN_START,
  LOGIN_COMPLETE,
  SET_TOKEN,
  LOGOUT_START,
  LOGOUT_COMPLETE,
  RESET_PASSWORD_COMPLETE,
  LOAD_ALL_PLAYLISTS_START,
} from '../constants/ActionTypes';
import * as Session from '../utils/Session';
import { get, post, del } from './RequestActionCreators';
import { loadHistory } from './BoothActionCreators';
import { setPlaylists, loadPlaylist } from './PlaylistActionCreators';
import { syncTimestamps } from './TickerActionCreators';
import { closeLoginDialog } from './DialogActionCreators';
import { tokenSelector } from '../selectors/userSelectors';

export function socketConnect() {
  return { type: SOCKET_CONNECT };
}

export function socketReconnect() {
  return { type: SOCKET_RECONNECT };
}

export function setAuthenticationStrategies(strategies) {
  return {
    type: AUTH_STRATEGIES,
    payload: { strategies },
  };
}

export function loginComplete({ token, socketToken, user }) {
  return (dispatch) => {
    dispatch({
      type: LOGIN_COMPLETE,
      payload: {
        token,
        socketToken,
        user,
      },
    });
    dispatch(closeLoginDialog());
  };
}

export function loadedState(state) {
  return (dispatch, getState) => {
    dispatch({
      type: INIT_STATE,
      payload: state,
    });
    if (state.user) {
      const token = tokenSelector(getState());
      dispatch(loginComplete({
        token,
        socketToken: state.socketToken,
        user: state.user,
      }));
    }
    if (state.activePlaylist) {
      dispatch(loadPlaylist(state.activePlaylist));
    }
  };
}

export function initState() {
  const beforeTime = Date.now();

  return get('/now', {
    onStart: () => ({ type: LOAD_ALL_PLAYLISTS_START }),
    onComplete: (state) => (dispatch) => {
      dispatch(syncTimestamps(beforeTime, state.time));
      dispatch(loadedState(state));
      dispatch(loadHistory());
      return state;
    },
  });
}

export function setSessionToken(token) {
  return {
    type: SET_TOKEN,
    payload: { token },
  };
}

function loginStart() {
  return { type: LOGIN_START };
}

export function login({ email, password }) {
  const sessionType = Session.preferredSessionType();
  return post(`/auth/login?session=${sessionType}`, { email, password }, {
    onStart: loginStart,
    onComplete: (res) => (dispatch) => {
      Session.set(res.meta.jwt);
      dispatch(setSessionToken(res.meta.jwt));
      dispatch(initState());
    },
    onError: (error) => ({
      type: LOGIN_COMPLETE,
      error: true,
      payload: error,
    }),
  });<commit_msg>Keep login dialog open until new state is loaded (#1900)<commit_after>import {
  INIT_STATE,
  SOCKET_CONNECT,
  SOCKET_RECONNECT,
  AUTH_STRATEGIES,
  REGISTER_START,
  REGISTER_COMPLETE,
  LOGIN_START,
  LOGIN_COMPLETE,
  SET_TOKEN,
  LOGOUT_START,
  LOGOUT_COMPLETE,
  RESET_PASSWORD_COMPLETE,
  LOAD_ALL_PLAYLISTS_START,
} from '../constants/ActionTypes';
import * as Session from '../utils/Session';
import { get, post, del } from './RequestActionCreators';
import { loadHistory } from './BoothActionCreators';
import { setPlaylists, loadPlaylist } from './PlaylistActionCreators';
import { syncTimestamps } from './TickerActionCreators';
import { closeLoginDialog } from './DialogActionCreators';
import { tokenSelector } from '../selectors/userSelectors';

export function socketConnect() {
  return { type: SOCKET_CONNECT };
}

export function socketReconnect() {
  return { type: SOCKET_RECONNECT };
}

export function setAuthenticationStrategies(strategies) {
  return {
    type: AUTH_STRATEGIES,
    payload: { strategies },
  };
}

export function loginComplete({ token, socketToken, user }) {
  return (dispatch) => {
    dispatch({
      type: LOGIN_COMPLETE,
      payload: {
        token,
        socketToken,
        user,
      },
    });
    dispatch(closeLoginDialog());
  };
}

export function loadedState(state) {
  return (dispatch, getState) => {
    dispatch({
      type: INIT_STATE,
      payload: state,
    });
    if (state.user) {
      const token = tokenSelector(getState());
      dispatch(loginComplete({
        token,
        socketToken: state.socketToken,
        user: state.user,
      }));
    }
    if (state.activePlaylist) {
      dispatch(loadPlaylist(state.activePlaylist));
    }
  };
}

export function initState() {
  const beforeTime = Date.now();

  return get('/now', {
    onStart: () => ({ type: LOAD_ALL_PLAYLISTS_START }),
    onComplete: (state) => (dispatch) => {
      dispatch(syncTimestamps(beforeTime, state.time));
      dispatch(loadedState(state));
      dispatch(loadHistory());
      return state;
    },
  });
}

export function setSessionToken(token) {
  return {
    type: SET_TOKEN,
    payload: { token },
  };
}

function loginStart() {
  return { type: LOGIN_START };
}

export function login({ email, password }) {
  const sessionType = Session.preferredSessionType();
  return post(`/auth/login?session=${sessionType}`, { email, password }, {
    onStart: loginStart,
    onComplete: (res) => (dispatch) => {
      Session.set(res.meta.jwt);
      dispatch(setSessionToken(res.meta.jwt));
      return dispatch(initState());
    },
    onError: (error) => ({
      type: LOGIN_COMPLETE,
      error: true,
      payload: error,
    }),
  });
{'fraction_non_alphanumeric': 0.13339517625231911, 'fraction_numerical': 0.0007421150278293135, 'mean_word_length': 3.379366368805849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7229747', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1691, 'n_words': 350}