import {combineReducers} from 'redux'
import cuid from 'cuid'
import * as api from '../api'

// ** action types **
export const FETCH_PROJECTS_REQUESTED = 'FETCH_PROJECTS_REQUESTED'
export const FETCH_PROJECTS_SUCCESSFUL = 'FETCH_PROJECTS_SUCCESSFUL'
export const FETCH_PROJECTS_FAILED = 'FETCH_PROJECTS_FAILED'

export const FETCH_PROJECT_SUCCESSFUL = 'FETCH_PROJECT_SUCCESSFUL'

export const CREATE_PROJECT_SUCCESSFUL = 'CREATE_PROJECT_SUCCESSFUL'
// ** action creators **

// ** async actions **
// these use thunks
export const fetchProject = (id) => (dispatch) => {
  api.fetchProject(id).then(response => {
    dispatch({
      type: FETCH_PROJECT_SUCCESSFUL,
      response
    })
  })
}

export const fetchProjects = () => (dispatch) => {
  dispatch({type: FETCH_PROJECTS_REQUESTED})
  api.fetchProjects().then(
    response => {
      dispatch({
        type: FETCH_PROJECTS_SUCCESSFUL,
        response
      })
    },
    error => {
      dispatch({
        type: FETCH_PROJECTS_FAILED,
        message: error.message || 'something went wrong'
      })
    }
  )
}

export const createProject = ({
  id = cuid(),
  ownerId = '',
  name = '',
  summary = '',
  tags = [],
  description = '',
  roles = [],
  status = 'open',
  timeStamp = Date.now(),
  lastUpdated = Date.now()
}) => (dispatch) => {
  api.createProject({
    id,
    ownerId,
    name,
    summary,
    tags,
    description,
    roles,
    status,
    timeStamp,
    lastUpdated
  }).then(response =>
    dispatch({
      type: CREATE_PROJECT_SUCCESSFUL,
      response
    })
  )
}

const defaultState = {
  byId: {},
  allIds: []
}

export const byId = (state = defaultState.byId, action) => {
  const { type, response } = action
  switch (type) {
    case FETCH_PROJECTS_SUCCESSFUL:
      return {...state, ...response}
    case CREATE_PROJECT_SUCCESSFUL:
      return {...state, [response.id]: response}
    default:
      return state
  }
}

export const allIds = (state = defaultState.allIds, action) => {
  const { type, response } = action
  switch (type) {
    case FETCH_PROJECTS_SUCCESSFUL:
      const newIds = Object.keys(response).filter(k => state.indexOf(k) <= 0)
      return [...state, ...newIds]
    case CREATE_PROJECT_SUCCESSFUL:
      return [...state, response.id]
    default:
      return state
  }
}

export const isFetching = (state = false, action) => {
  const { type } = action
  switch (type) {
    case FETCH_PROJECTS_REQUESTED:
      return true
    case FETCH_PROJECTS_SUCCESSFUL:
    case FETCH_PROJECTS_FAILED:
      return false
    default:
      return state
  }
}

export const errorMessage = (state = null, action) => {
  const { type } = action
  switch (type) {
    case FETCH_PROJECTS_FAILED:
      return action.message
    case FETCH_PROJECTS_REQUESTED:
    case FETCH_PROJECT_SUCCESSFUL:
      return null
    default:
      return state
  }
}

export default combineReducers({byId, allIds, isFetching, errorMessage})

@@ -1555,16 +1555,34 @@
 %0A  )%0A%7D%0A%0A
+// ** reducers **%0A
 const de
@@ -1620,16 +1620,59 @@
 lIds: %5B%5D
+,%0A  isFetching: false,%0A  errorMessage: null
 %0A%7D%0A%0Aexpo
@@ -2394,21 +2394,39 @@
 state = 
-false
+defaultState.isFetching
 , action
@@ -2692,20 +2692,41 @@
 state = 
-null
+defaultState.errorMessage
 , action

refactor defaultState