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

Added onSuccessCallback to create and update actions and improve handling

of both
parent 145099a1
......@@ -148,10 +148,11 @@ export default class CrudActions {
* If `data` contains an `id` then the behavior should be similar to an update.
*
* @param {object} data
* @param {function} [onSuccessCallback=(newData) => { }] CallBack called if the update was successful besides calling updateSucceeded
* @returns {function}
* @memberof CrudActions
*/
create(data) {
create(data, onSuccessCallback = () => { }) {
const self = this;
function isCreating(status) {
......@@ -185,18 +186,29 @@ export default class CrudActions {
this.apiEndPoint,
isCreating,
createSucceeded,
createFailed
createFailed,
onSuccessCallback,
);
}
clearCreateFailed() {
return {
type: this.types.createFailed,
failed: false,
error: null
};
}
/**
* Function to update an entry
*
* @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}
* @memberof CrudActions
*/
update(data) {
update(data, onSuccessCallback = () => { }) {
const self = this;
function isUpdating(status) {
......@@ -231,7 +243,8 @@ export default class CrudActions {
this.apiEndPoint,
isUpdating,
updateSucceeded,
updateFailed
updateFailed,
onSuccessCallback,
);
}
......
......@@ -84,12 +84,13 @@ export default class SmartActions {
* @param {object} data, to update the data, `data` should contains the `id` field.
* @param {string} apiEndPoint
* @param {function} isSavingSpecific
* @param {function} readSpecificSucceeded
* @param {function} saveSpecificSucceeded
* @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.
* @memberof SmartActions
*/
_ElSaveData(data, apiEndPoint, isSavingSpecific, readSpecificSucceeded, failedSpecific) {
_ElSaveData(data, apiEndPoint, isSavingSpecific, saveSpecificSucceeded, failedSpecific, onSuccessCallback = () => { }) {
return (dispatch) => {
let method = "POST";
let pk = "";
......@@ -124,7 +125,8 @@ export default class SmartActions {
return response.json();
})
.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));
})
.catch((e) => {
......
......@@ -10,11 +10,33 @@ import { openFullScreenDialog, closeFullScreenDialog } from "../../../../actions
*/
export default function getMapDispatchToPropsForEditor(name) {
return (dispatch) => {
let lastSave = "update";
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)),
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