Commit 302743c1 authored by Florent Chehab's avatar Florent Chehab
Browse files

Added onSuccessCallback to create and update actions and improve handling

of both
parent 145099a1
...@@ -148,10 +148,11 @@ export default class CrudActions { ...@@ -148,10 +148,11 @@ export default class CrudActions {
* If `data` contains an `id` then the behavior should be similar to an update. * If `data` contains an `id` then the behavior should be similar to an update.
* *
* @param {object} data * @param {object} data
* @param {function} [onSuccessCallback=(newData) => { }] CallBack called if the update was successful besides calling updateSucceeded
* @returns {function} * @returns {function}
* @memberof CrudActions * @memberof CrudActions
*/ */
create(data) { create(data, onSuccessCallback = () => { }) {
const self = this; const self = this;
function isCreating(status) { function isCreating(status) {
...@@ -185,18 +186,29 @@ export default class CrudActions { ...@@ -185,18 +186,29 @@ export default class CrudActions {
this.apiEndPoint, this.apiEndPoint,
isCreating, isCreating,
createSucceeded, createSucceeded,
createFailed createFailed,
onSuccessCallback,
); );
} }
clearCreateFailed() {
return {
type: this.types.createFailed,
failed: false,
error: null
};
}
/** /**
* Function to update an entry * Function to update an entry
* *
* @param {object} data, an `id` field should be contained in this object * @param {object} data, an `id` field should be contained in this object
* @param {function} [onSuccessCallback=(newData) => { }] CallBack called if the update was successful besides calling updateSucceeded
* @returns {function} * @returns {function}
* @memberof CrudActions * @memberof CrudActions
*/ */
update(data) { update(data, onSuccessCallback = () => { }) {
const self = this; const self = this;
function isUpdating(status) { function isUpdating(status) {
...@@ -231,7 +243,8 @@ export default class CrudActions { ...@@ -231,7 +243,8 @@ export default class CrudActions {
this.apiEndPoint, this.apiEndPoint,
isUpdating, isUpdating,
updateSucceeded, updateSucceeded,
updateFailed updateFailed,
onSuccessCallback,
); );
} }
......
...@@ -84,12 +84,13 @@ export default class SmartActions { ...@@ -84,12 +84,13 @@ export default class SmartActions {
* @param {object} data, to update the data, `data` should contains the `id` field. * @param {object} data, to update the data, `data` should contains the `id` field.
* @param {string} apiEndPoint * @param {string} apiEndPoint
* @param {function} isSavingSpecific * @param {function} isSavingSpecific
* @param {function} readSpecificSucceeded * @param {function} saveSpecificSucceeded
* @param {function} failedSpecific * @param {function} failedSpecific
* @param {function} [onSuccessCallback = (newData) => {}] CallBack called if the update was successful besides calling the saveSpecificSucceeded action
* @returns {function} Function that takes the `dispatch` function of redux as argument. * @returns {function} Function that takes the `dispatch` function of redux as argument.
* @memberof SmartActions * @memberof SmartActions
*/ */
_ElSaveData(data, apiEndPoint, isSavingSpecific, readSpecificSucceeded, failedSpecific) { _ElSaveData(data, apiEndPoint, isSavingSpecific, saveSpecificSucceeded, failedSpecific, onSuccessCallback = () => { }) {
return (dispatch) => { return (dispatch) => {
let method = "POST"; let method = "POST";
let pk = ""; let pk = "";
...@@ -124,7 +125,8 @@ export default class SmartActions { ...@@ -124,7 +125,8 @@ export default class SmartActions {
return response.json(); return response.json();
}) })
.then((_El) => { .then((_El) => {
dispatch(readSpecificSucceeded(_El)); // we use the same here onSuccessCallback(_El); // don't put it after the dispatch, it might cause errors with components unMounting
dispatch(saveSpecificSucceeded(_El));
dispatch(isSavingSpecific(false)); dispatch(isSavingSpecific(false));
}) })
.catch((e) => { .catch((e) => {
......
...@@ -10,11 +10,33 @@ import { openFullScreenDialog, closeFullScreenDialog } from "../../../../actions ...@@ -10,11 +10,33 @@ import { openFullScreenDialog, closeFullScreenDialog } from "../../../../actions
*/ */
export default function getMapDispatchToPropsForEditor(name) { export default function getMapDispatchToPropsForEditor(name) {
return (dispatch) => { return (dispatch) => {
let lastSave = "update";
return { return {
saveData: (data) => dispatch(getActions(name).update(data)), // eslint-disable-next-line no-unused-vars
saveData: (data, onSuccessCallback = (newData) => { }) => {
if ("id" in data) { // it's an update
lastSave = "update";
dispatch(getActions(name).update(data, onSuccessCallback));
} else { // it's a create
lastSave = "create";
dispatch(getActions(name).create(data, onSuccessCallback));
}
},
openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)), openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)),
closeFullScreenDialog: () => dispatch(closeFullScreenDialog()), closeFullScreenDialog: () => dispatch(closeFullScreenDialog()),
clearSaveError: () => dispatch(getActions(name).clearUpdateFailed()), clearSaveError: () => {
// we need to clear the correct error
// depending on if it was a save or a create
switch (lastSave) {
case "update":
dispatch(getActions(name).clearUpdateFailed());
break;
case "create":
dispatch(getActions(name).clearCreateFailed());
break;
}
},
}; };
}; };
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment