<commit_before>/* @flow */
import fetch from 'isomorphic-fetch'
// ------------------------------------
// Constants
// ------------------------------------
export const SET_MODE = 'SET_MODE'
export const GET_MODE = 'GET_MODE'

export const REQUEST_STATE = 'REQUEST_STATE'
export const RECEIVE_STATE = 'RECEIVE_STATE'

// ------------------------------------
// Actions
// ------------------------------------
// NOTE: "Action" is a Flow interface defined in https://github.com/TechnologyAdvice/flow-interfaces
// If you're unfamiliar with Flow, you are completely welcome to avoid annotating your code, but
// if you'd like to learn more you can check out: flowtype.org.
// DOUBLE NOTE: there is currently a bug with babel-eslint where a `space-infix-ops` error is
// incorrectly thrown when using arrow functions, hence the oddity.
export function setMode (mode, token):Action {
  return function (dispatch) {
    dispatch(requestState())
    console.log(`setting mode, token: ${token}`)
    console.log('setting mode')
    return fetch('/api/state', {
      method: 'POST',
      credentials: 'include',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
      },
      body: JSON.stringify({
        mode: mode
      })
    }).then((response) => response.json())
      .then((json) => dispatch(receiveState(json))
      )
  }
}

export function getMode (mode):Action {
  return {
    type: GET_MODE,
    payload: mode
  }
}

export function requestState () {
  return {
    type: REQUEST_STATE
  }
}

export function receiveState (json) {
  console.log(json)
  return {
    type: RECEIVE_STATE,
    payload: json.mode,
    receivedAt: Date.now()
  }
}

export function fetchState (token) {
  if (typeof token === 'undefined') {
    token = ''
  }
  console.log(`fetching state, token: ${token}`)
  return function (dispatch) {
    dispatch(requestState())
    return fetch('/api/state', {
      credentials: 'include',
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
      .then((response) => response.json())
      .then((json) => dispatch(receiveState(json))
      )
    // TODO: should also catch any error in the network call.
  }
}

export const actions = {
  setMode,
  getMode,
  requestState,
  receiveState,
  fetchState
}
// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [SET_MODE]: (state:mode, action:{payload: mode}):mode => action.payload,
  [RECEIVE_STATE]: (state:mode, action:{payload: mode}):mode => action.payload
}

// ------------------------------------
// Reducer
// ------------------------------------
let initialState = 'fetching data..'
export default function modeReducer (state:mode = initialState, action:Action):mode {
  const handler = ACTION_HANDLERS[action.type]<commit_msg>error handling for fetch state<commit_after>/* @flow */
import fetch from 'isomorphic-fetch'
import {push} from 'react-router-redux'
import loginUserFailure from './auth'
// ------------------------------------
// Constants
// ------------------------------------
export const SET_MODE = 'SET_MODE'
export const GET_MODE = 'GET_MODE'

export const REQUEST_STATE = 'REQUEST_STATE'
export const RECEIVE_STATE = 'RECEIVE_STATE'

// ------------------------------------
// Actions
// ------------------------------------
// NOTE: "Action" is a Flow interface defined in https://github.com/TechnologyAdvice/flow-interfaces
// If you're unfamiliar with Flow, you are completely welcome to avoid annotating your code, but
// if you'd like to learn more you can check out: flowtype.org.
// DOUBLE NOTE: there is currently a bug with babel-eslint where a `space-infix-ops` error is
// incorrectly thrown when using arrow functions, hence the oddity.
export function setMode (mode, token):Action {
  return function (dispatch) {
    dispatch(requestState())
    return fetch('/api/state', {
      method: 'POST',
      credentials: 'include',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
      },
      body: JSON.stringify({
        mode: mode
      })
    }).then((response) => response.json())
      .then((json) => dispatch(receiveState(json))
      )
  }
}

export function getMode (mode):Action {
  return {
    type: GET_MODE,
    payload: mode
  }
}

export function requestState () {
  return {
    type: REQUEST_STATE
  }
}

export function receiveState (json) {
  return {
    type: RECEIVE_STATE,
    payload: json.mode,
    receivedAt: Date.now()
  }
}

export function fetchState (token) {
  if (typeof token === 'undefined') {
    token = ''
  }
  return function (dispatch) {
    dispatch(requestState())
    return fetch('/api/state', {
      credentials: 'include',
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
      .then((response) => response.json())
      .then((json) => dispatch(receiveState(json))
      ).catch((error) => {
        if (error.response.status === 401) {
          dispatch(loginUserFailure(error))
          dispatch(push('/login'))
        }
      })
  }
}

export const actions = {
  setMode,
  getMode,
  requestState,
  receiveState,
  fetchState
}
// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
  [SET_MODE]: (state:mode, action:{payload: mode}):mode => action.payload,
  [RECEIVE_STATE]: (state:mode, action:{payload: mode}):mode => action.payload
}

// ------------------------------------
// Reducer
// ------------------------------------
let initialState = 'fetching data..'
export default function modeReducer (state:mode = initialState, action:Action):mode {
  const handler = ACTION_HANDLERS[action.type]
{'fraction_non_alphanumeric': 0.22197577205255076, 'fraction_numerical': 0.0005118580447022692, 'mean_word_length': 2.173795343800758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '4541006', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1616, 'n_words': 440}