|
@@ -1,92 +1,12 @@
|
|
|
import { call, put, takeEvery } from 'redux-saga/effects'
|
|
|
|
|
|
-/** The basic CRUD API supports the following functionality:
|
|
|
- * /api[/module][/id|/offset[/limit]]
|
|
|
- *
|
|
|
- * Other functions
|
|
|
- */
|
|
|
-
|
|
|
-import request from 'request-promise'
|
|
|
-export function crudHttpApi (action, options) {
|
|
|
- const { uri } = options
|
|
|
- // determine the operation
|
|
|
- const [ name, actionType ] = action.type.split('/')
|
|
|
- const requestOptions = {
|
|
|
- uri: `${uri}/${name}`,
|
|
|
- method: null,
|
|
|
- body: null
|
|
|
- }
|
|
|
- switch (actionType) {
|
|
|
- case 'READ_REQUEST':
|
|
|
- options.uri = `${uri}/${action.id}`
|
|
|
- options.method = 'GET'
|
|
|
- break
|
|
|
- case 'CREATE_REQUEST':
|
|
|
- options.method = 'POST'
|
|
|
- options.body = action.data
|
|
|
- break
|
|
|
- case 'UPDATE_REQUEST':
|
|
|
- options.uri = `${uri}/${action.id}`
|
|
|
- options.method = 'PUT'
|
|
|
- break
|
|
|
- case 'DELETE_REQUEST':
|
|
|
- options.uri = `${uri}/${action.id}`
|
|
|
- options.method = 'DELETE'
|
|
|
- break
|
|
|
- }
|
|
|
- // Request from the request-promise library already returns a promise.
|
|
|
- return request(requestOptions)
|
|
|
-}
|
|
|
-
|
|
|
-export function crudLocalstorageApi (action, options) {
|
|
|
- // determine the operation
|
|
|
- const [ name, actionType ] = action.type.split('/')
|
|
|
- switch (actionType) {
|
|
|
- case 'READ_REQUEST':
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- const items = localStorage.getItem(name) || []
|
|
|
- resolve(items[action.id])
|
|
|
- })
|
|
|
- case 'CREATE_REQUEST':
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- try {
|
|
|
- const items = localStorage.getItem(name) || {}
|
|
|
- items[action.id] = action.data
|
|
|
- console.log(items)
|
|
|
- localStorage.setItem(name, items)
|
|
|
- resolve(null)
|
|
|
- } catch (error) {
|
|
|
- reject(error)
|
|
|
- }
|
|
|
- })
|
|
|
- case 'UPDATE_REQUEST':
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- try {
|
|
|
- const items = localStorage.getItem(name) || {}
|
|
|
- items[action.id] = action.data
|
|
|
- localStorage.setItem(name, items)
|
|
|
- resolve(null)
|
|
|
- } catch (error) {
|
|
|
- reject(error)
|
|
|
- }
|
|
|
- })
|
|
|
- case 'DELETE_REQUEST':
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- try {
|
|
|
- const items = localStorage.getItem(name) || {}
|
|
|
- delete items[action.id]
|
|
|
- localStorage.setItem(name, items)
|
|
|
- resolve(null)
|
|
|
- } catch (error) {
|
|
|
- reject(error)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/** reduxCrudGen
|
|
|
*
|
|
|
- * Generates CRUD action types, actions, reducers, workers and watchers.
|
|
|
+ * Generates action types, actions, the reducer and redux sagas.
|
|
|
+ * @param {string} name - Name of the module
|
|
|
+ * @param {string} prefix - Prefix used for the actions
|
|
|
+ * @param {function} api - API called by the worker function
|
|
|
+ * @return {object} - { actionTypes, actions, state, reducer, worker, watcher }
|
|
|
*/
|
|
|
export default function reduxCrudGen (name, prefix, api) {
|
|
|
const actionTypes = {}
|
|
@@ -128,7 +48,7 @@ export default function reduxCrudGen (name, prefix, api) {
|
|
|
/** The watcher function hijacks the _REQUEST actions and calls
|
|
|
* asynchronous workers. Once they yield their result, a _SUCCESS
|
|
|
* or _FAILURE action is triggered. */
|
|
|
- function * watcher () {
|
|
|
+ function * saga () {
|
|
|
let action
|
|
|
for (action of actionList) {
|
|
|
yield takeEvery(`${name}/${action.toUpperCase()}_REQUEST`, worker)
|
|
@@ -169,6 +89,6 @@ export default function reduxCrudGen (name, prefix, api) {
|
|
|
state,
|
|
|
reducer,
|
|
|
worker,
|
|
|
- watcher
|
|
|
+ saga
|
|
|
}
|
|
|
}
|