Source: project/state.js

/** @module project/state */

/**
 * state.js
 *
 * Collection of everything which has to do with state changes.
 * - actionTypes
 * - actions
 **/

import { NAME } from './constants'
// import { call, put, takeEvery } from 'redux-saga/effects'

/** actionTypes define what actions are handeled by the reducer. */
export const actionTypes = {
  CREATE_REQ: `${NAME}/CREATE_REQ`,
  UPDATE_REQ: `${NAME}/UPDATE_REQ`,
  DELETE_REQ: `${NAME}/DELETE_REQ`
}

/** actions is an object with references to all action creators */
function createProject (project) {
  return {
    type: actionTypes.CREATE_REQ,
    project
  }
}

function updateProject (projectId, project) {
  return {
    type: actionTypes.UPDATE_REQ,
    projectId,
    project
  }
}

function removeProject (projectId) {
  return {
    type: actionTypes.REMOVE_REQ,
    projectId
  }
}
export const actions = { createProject, updateProject, removeProject }

/** state definition */
export const state = []

/** reducer is called by the redux dispatcher and handles all component actions */
export function reducer (state = {}, action) {
  switch (action.type) {
    case actionTypes.CREATE:
      return {
        ...state,
        [action.project.projectId]: action.project
      }
    case actionTypes.UPDATE:
      return {
        ...state,
        [action.projectId]: action.project
      }
    case actionTypes.REMOVE:
      return {
        ...state,
        [action.projectId]: null
      }
    default:
      return state
  }
}

/** sagas are asynchronous workers (JS generators) to handle the state.
// Worker
export function * incrementAsync () {
  try {
    const data = yield call(Api.isIncrementOk)
    yield put({ type: 'INCREMENT_SUCCESS', data })
  } catch (error) {
    yield put({ type: 'INCREMENT_FAIL', error})
  }
}

// Watcher (intercepts INCREMENT_REQUEST, dispatches INCREMENT_SUCCESS or INCREMENT_FAIL in return.)
export function * watchIncrementAsync () {
  yield takeEvery('INCREMENT_REQUEST', incrementAsync)
}

// export all sagas in parallel
function * sagas () {
  yield takeEvery('INCREMENT_REQUEST')
  yield takeEvery('DECREMENT_REQUEST')
} */
export const sagas = null