|
@@ -8,87 +8,29 @@
|
|
|
* - actions
|
|
|
**/
|
|
|
|
|
|
-import genStuff from 'helpers'
|
|
|
+import combineReducers from 'redux'
|
|
|
+import reduxCrudGen from 'redux-gen/crud'
|
|
|
+import reduxBasicGen from 'redux-gen/basic'
|
|
|
import { NAME, DATA } from './constants'
|
|
|
// import { call, put, takeEvery } from 'redux-saga/effects'
|
|
|
|
|
|
-const herbie = genStuff('project', ['create', 'update', 'remove'], false, api = null)
|
|
|
-/** actionTypes define what actions are handeled by the reducer. */
|
|
|
-const actionTypes = {}
|
|
|
-export const actionCreators = {}
|
|
|
-
|
|
|
-// Generate default actionsTypes CREATE, UPDATE, REMOVE
|
|
|
-DATA.forEach((dataItem, idx) => {
|
|
|
- ['create_request', 'create_success', 'create_fail',
|
|
|
- 'update_request', 'update_success', 'update_fail',
|
|
|
- 'remove_request', 'remove_success', 'remove_fail'].forEach(action => {
|
|
|
- // The Redux convention is to name action types e.g. demo_module/UPDATE
|
|
|
- // For action creators, we define here the name e.g. removePrimary(id, data)
|
|
|
- // where id is the element id and data is the element itself.
|
|
|
- const actionType = `${action.toUpperCase()}_${dataItem.toUpperCase()}`
|
|
|
- const actionName = `${action}${dataItem[0].toUpperCase()}${dataItem.substring(1)}`
|
|
|
- if (idx === 0) {
|
|
|
- actionCreators[actionName] = (id, data) => { return { type: `${NAME}/${actionType}`, id, data } }
|
|
|
- } else {
|
|
|
- actionCreators[actionName] = (primaryId, id, data) => { return { type: `${NAME}/${actionType}`, primaryId, id, data } }
|
|
|
- }
|
|
|
- actionTypes[actionType] = `${NAME}/${actionType}`
|
|
|
- })
|
|
|
-})
|
|
|
+const basic = reduxBasicGen()
|
|
|
+const crud = reduxCrudGen()
|
|
|
|
|
|
-// Add specific action creators here:
|
|
|
-// actionCreators['loadSamples'] = () => { return { type: `${NAME}/LOAD_SAMPLES` } }
|
|
|
-// actionTypes['LOAD_SAMPLES'] = `${NAME}/LOAD_SAMPLES`
|
|
|
+/** actionTypes define what actions are handeled by the reducer. */
|
|
|
+const actions = { ...basic.actions, ...crud.actions }
|
|
|
+console.log('State actions', actions)
|
|
|
|
|
|
/** state definition */
|
|
|
/** It is generally easier to not have another object here. */
|
|
|
-export const state = []
|
|
|
+export const state = [ ...basic.state, ...crud.state ]
|
|
|
console.log('State state', state)
|
|
|
|
|
|
/** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
-export function reducer (state = {}, action) {
|
|
|
- switch (action.type) {
|
|
|
- case actionTypes.CREATE_REQUEST:
|
|
|
-
|
|
|
- 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
|
|
|
- }
|
|
|
-}
|
|
|
+export const reducer = combineReducers(basic.reducer, crud.reducer)
|
|
|
|
|
|
/** 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})
|
|
|
- }
|
|
|
-}
|
|
|
+export const workers = crud.workers
|
|
|
|
|
|
// 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
|
|
|
+export const watchers = crud.watchers
|