{"size": 1500, "ext": "js", "max_stars_repo_path": "src/store/actionCreators.js", "max_stars_repo_name": "astridlyre/miniBank", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/store/actionCreators.js", "max_issues_repo_name": "astridlyre/miniBank", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store/actionCreators.js", "max_forks_repo_name": "astridlyre/miniBank", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.8620689655, "max_line_length": 68, "alphanum_fraction": 0.6693333333}
import { LOGIN, LOGOUT, UPDATE_USER } from './actions'
import { strategy, APIService } from '../services/'

const actionLoginUser = (user) => ({ type: LOGIN, payload: user })
const actionLogoutUser = () => ({ type: LOGOUT })
const actionUpdateUser = (user) => ({
  type: UPDATE_USER,
  payload: user,
})

const login = (user) => async (dispatch) => {
  const userService = strategy('user')

  try {
    const savedUser = await userService.save('user', user)
    dispatch(actionLoginUser(savedUser))
  } catch (error) {
    console.error(error)
  }
}

const logout = () => async (dispatch) => {
  const userService = strategy('user')

  try {
    await userService.remove('user')
    dispatch(actionLogoutUser())
  } catch (error) {
    console.error(error)
  }
}

const withdrawFunds = (funds) => async (dispatch, getState) => {
  const userService = strategy('user')

  try {
    const updatedUser = await APIService.withdraw(getState(), funds)
    const savedUser = await userService.save('user', updatedUser)
    dispatch(actionUpdateUser(savedUser))
  } catch (error) {
    console.error(error)
  }
}

const depositFunds = (funds) => async (dispatch, getState) => {
  const userService = strategy('user')

  try {
    const updatedUser = await APIService.deposit(getState(), funds)
    const savedUser = await userService.save('user', updatedUser)
    dispatch(actionUpdateUser(savedUser))
  } catch (error) {
    console.error(error)
  }
}

export { login, logout, withdrawFunds, depositFunds }
