diff options
Diffstat (limited to 'frontend/app/api/crud.actions.js')
| -rw-r--r-- | frontend/app/api/crud.actions.js | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/frontend/app/api/crud.actions.js b/frontend/app/api/crud.actions.js index 86c2948..54fc96d 100644 --- a/frontend/app/api/crud.actions.js +++ b/frontend/app/api/crud.actions.js @@ -1,51 +1,55 @@ -import { crud_fetch } from 'app/api/crud.fetch' -import { as_type } from 'app/api/crud.types' -import { upload_action } from 'app/api/crud.upload' -import { store } from 'app/store' +import { crud_fetch } from "app/api/crud.fetch"; +import { as_type } from "app/api/crud.types"; +import { upload_action } from "app/api/crud.upload"; +import { store } from "app/store"; export function crud_actions(type) { - const fetch_type = crud_fetch(type) - return [ - 'index', - 'show', - 'create', - 'update', - 'destroy', - ].reduce((lookup, param) => { - lookup[param] = crud_action(type, param, (q) => fetch_type[param](q)) - return lookup - }, { - action: (method, fn) => crud_action(type, method, fn), - upload: (fd) => upload_action(type, fd), - updateOption: (key, value) => dispatch => { - dispatch({ type: as_type(type, 'update_option'), key, value }) + const fetch_type = crud_fetch(type); + return ["index", "show", "create", "update", "destroy"].reduce( + (lookup, param) => { + lookup[param] = crud_action(type, param, (q) => fetch_type[param](q)); + return lookup; }, - updateOptions: opt => dispatch => { - dispatch({ type: as_type(type, 'update_options'), opt }) - }, - }) + { + action: (method, fn) => crud_action(type, method, fn), + upload: (fd) => upload_action(type, fd), + updateOption: (key, value) => (dispatch) => { + dispatch({ type: as_type(type, "update_option"), key, value }); + }, + updateOptions: (opt) => (dispatch) => { + dispatch({ type: as_type(type, "update_options"), opt }); + }, + } + ); } -export const crud_action = (type, method, fn) => (q, load_more) => dispatch => { - return new Promise ((resolve, reject) => { - if (method === 'index') { +export const crud_action = (type, method, fn) => (q, load_more) => ( + dispatch +) => { + return new Promise((resolve, reject) => { + if (method === "index") { if (store.getState()[type].index.loading) { - return resolve({}) + return resolve({}); } } - dispatch({ type: as_type(type, method + '_loading'), load_more }) - fn(q).then(data => { - if (data.status === 'ok') { - dispatch({ type: as_type(type, method), data, load_more }) - resolve(data) - } else { - dispatch({ type: as_type(type, method + '_error'), error: data.error }) - reject(data) - } - }).catch(e => { - console.log(e) - dispatch({ type: as_type(type, method + '_error') }) - reject(e) - }) - }) -} + dispatch({ type: as_type(type, method + "_loading"), load_more }); + fn(q) + .then((data) => { + if (data?.status === "ok") { + dispatch({ type: as_type(type, method), data, load_more }); + resolve(data); + } else { + dispatch({ + type: as_type(type, method + "_error"), + error: data.error, + }); + reject(data); + } + }) + .catch((e) => { + console.log(e); + dispatch({ type: as_type(type, method + "_error") }); + reject(e); + }); + }); +}; |
