{"size": 1819, "ext": "js", "max_stars_repo_path": "client/redux/index.js", "max_stars_repo_name": "mockingbot/redux-code-sample", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/redux/index.js", "max_issues_repo_name": "mockingbot/redux-code-sample", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/redux/index.js", "max_forks_repo_name": "mockingbot/redux-code-sample", "max_forks_repo_forks_event_min_datetime": "2018-06-03T02:04:43.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-02T06:55:55.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 30.3166666667, "max_line_length": 103, "alphanum_fraction": 0.5673446949}
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'

import fetchJson from '../fetchJson'
import { Auth, Data, Error } from '../container'

import configure from './configure'

function doAuthFetch (dispatch, name, password) {
  dispatch({ type: 'auth:pending' })
  return fetchJson('/auth', { name, password })
    .then((result) => { dispatch({ type: 'auth:set', payload: { token: result.token } }) })
    .catch((error) => {
      dispatch({ type: 'error:set', payload: { error: 'Auth Error' } })
      return error
    })
}

function doDataFetch (dispatch, token) {
  dispatch({ type: 'data:pending' })
  return fetchJson('/data', { token })
    .then((result) => { dispatch({ type: 'data:set', payload: { data: result } }) })
    .catch((error) => {
      dispatch({ type: 'error:set', payload: { error: 'Data Error' } })
      return error
    })
}

window.addEventListener('load', () => {
  const rootElement = document.getElementById('root-redux')
  const { store } = configure()

  function doRender () {
    ReactDOM.render(
      <Provider store={store}>
        <div>
          <Error doReset={() => store.dispatch({ type: 'error:reset' })}/>
          <Auth
            doAuth={({ name, password }) => {
              doAuthFetch(store.dispatch, name, password)
                .then((error) => { error || doDataFetch(store.dispatch, store.getState().auth.token) })
            }}
            doReset={() => store.dispatch({ type: 'auth:reset' })}
          />
          <Data
            doFetch={(token) => doDataFetch(store.dispatch, token)}
            doReset={() => store.dispatch({ type: 'data:reset' })}
          />
        </div>
      </Provider>,
      rootElement
    )
  }

  doRender()

  window[ 'redux' ] = { doRender, rootElement, store }
})
