Commit 32a0a937 authored by Florent Chehab's avatar Florent Chehab

Restored alerts in Editor

parent 1373af74
...@@ -235,6 +235,14 @@ export default class CrudActions { ...@@ -235,6 +235,14 @@ export default class CrudActions {
); );
} }
clearUpdateFailed() {
return {
type: this.types.updateFailed,
failed: false,
error: null
};
}
/////////// ///////////
// NOT CRUD FUNCTIONS // NOT CRUD FUNCTIONS
/////////// ///////////
......
...@@ -9,7 +9,6 @@ import Typography from "@material-ui/core/Typography"; ...@@ -9,7 +9,6 @@ import Typography from "@material-ui/core/Typography";
import CloseIcon from "@material-ui/icons/Close"; import CloseIcon from "@material-ui/icons/Close";
import Alert from "./Alert"; import Alert from "./Alert";
// import renderFieldsMixIn from "./editorFunctions/renderFieldsMixIn"; // import renderFieldsMixIn from "./editorFunctions/renderFieldsMixIn";
// Form is imported only for type hints // Form is imported only for type hints
...@@ -34,17 +33,20 @@ import Form from "./Form"; ...@@ -34,17 +33,20 @@ import Form from "./Form";
*/ */
class Editor extends Component { class Editor extends Component {
// handling of the alert
state = { state = {
alert: { alert: {
open: false, open: false,
}, },
} }
formRef = React.createRef();
// Store the "saving" notification so that we can delete it when done saving. // Store the "saving" notification so that we can delete it when done saving.
savingNotification = null; savingNotification = null;
// reference to the form that the editor should contain
formRef = React.createRef();
/** /**
* Returns the form instance associated with the editor * Returns the form instance associated with the editor
* *
...@@ -142,6 +144,8 @@ class Editor extends Component { ...@@ -142,6 +144,8 @@ class Editor extends Component {
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
// Open the editor if needed // Open the editor if needed
// don't check the if prevProps was false before, we need to rerender it anyway so that we can
// display the alert.
if (this.props.open) { if (this.props.open) {
this.props.openFullScreenDialog(this.renderEditor()); this.props.openFullScreenDialog(this.renderEditor());
} }
...@@ -159,9 +163,8 @@ class Editor extends Component { ...@@ -159,9 +163,8 @@ class Editor extends Component {
// we make to notify if saving to the server has errors // we make to notify if saving to the server has errors
const { savingHasError } = this.props; const { savingHasError } = this.props;
if (savingHasError.status && !this.state.alert.open) { if (savingHasError.failed && !this.state.alert.open) {
this.alertSaveFailed(); this.alertSaveFailed(savingHasError.error.content.toString());
this.handleCloseNotificationRequest(savingHasError.error);
} }
//saving data was successful //saving data was successful
...@@ -241,7 +244,40 @@ class Editor extends Component { ...@@ -241,7 +244,40 @@ class Editor extends Component {
/////////////////// ///////////////////
// Notification and alert related functions // Notification and alert related functions
// user request handlers // Alerts related
alertSaveFailed(error) {
this.removeSavingNotification();
this.setState({
alert: {
open: true,
info: true,
title: "L'enregistrement sur le serveur a échoué.",
description: error + "\n \nVous pourrez réessayer après avoir fermer cette alerte. Si l'erreur persiste, vérifier votre connexion internet ou contacter les administrateurs du site.",
infoText: "J'ai compris",
handleResponse: () => this.props.clearSaveError()
}
});
}
alertChangesNotSaved() {
this.setState({
alert: {
open: true,
info: false,
title: "Modifications non enregistrées !",
description: "Vous avez des modifications qui n'ont pas été sauvegardées. Voulez-vous les enregistrer ?",
agreeText: "Oui, je les enregistre",
disagreeText: "Non",
handleResponse: (agree) => {
if (agree) {
this.handleSaveEditorRequest();
} else {
this.closeEditor();
}
},
}
});
}
handleCloseAlertRequest() { handleCloseAlertRequest() {
this.setState({ this.setState({
...@@ -249,7 +285,8 @@ class Editor extends Component { ...@@ -249,7 +285,8 @@ class Editor extends Component {
}); });
} }
// Notifications and alerts from JS
// Notifications related
notifyNoChangesDetected() { notifyNoChangesDetected() {
this.props.enqueueSnackbar("Aucun changement n'a été repéré.", { variant: "info" }); this.props.enqueueSnackbar("Aucun changement n'a été repéré.", { variant: "info" });
...@@ -264,44 +301,15 @@ class Editor extends Component { ...@@ -264,44 +301,15 @@ class Editor extends Component {
} }
notifySaveSuccessful(message) { notifySaveSuccessful(message) {
this.props.closeSnackbar(this.savingNotification); this.removeSavingNotification();
this.props.enqueueSnackbar(message, { variant: "success", autoHideDuration: 5000}); this.props.enqueueSnackbar(message, { variant: "success", autoHideDuration: 5000 });
}
alertSaveFailed(error) {
console.log("Save failed");
// this.setState({
// alert: {
// open: true,
// info: true,
// title: "L'enregistrement sur le serveur a échoué.",
// description: JSON.stringify(error, null, 2) + "\n \nVous pourrez réessayer après avoir fermer cette alerte. Si l'erreur persiste, vérifier votre connexion internet ou contacter les administrateurs du site.",
// infoText: "J'ai compris",
// // handleResponse: () => this.props.clearSaveError()
// }
// });
} }
alertChangesNotSaved() { removeSavingNotification() {
console.log("changes not saved"); if (this.savingNotification) {
// this.setState({ this.props.closeSnackbar(this.savingNotification);
// alert: { this.savingNotification = null;
// open: true, }
// info: false,
// title: "Modifications non enregistrées !",
// description: "Vous avez des modifications qui n'ont pas été sauvegardées. Voulez-vous les enregistrer ?",
// agreeText: "Oui, je les enregistre",
// disagreeText: "Non",
// handleResponse: (agree) => {
// if (agree) {
// this.handleSaveEditorRequest();
// } else {
// this.props.handleEditorWasClosed();
// }
// },
// }
// });
} }
} }
...@@ -311,14 +319,14 @@ class Editor extends Component { ...@@ -311,14 +319,14 @@ class Editor extends Component {
Editor.propTypes = { Editor.propTypes = {
classes: PropTypes.object.isRequired, classes: PropTypes.object.isRequired,
// clearSaveError: PropTypes.func.isRequired,
savingHasError: PropTypes.object.isRequired, savingHasError: PropTypes.object.isRequired,
clearSaveError: PropTypes.func.isRequired,
lastUpdateTimeInModel: PropTypes.string, lastUpdateTimeInModel: PropTypes.string,
lastSaveTime: PropTypes.number, lastSaveTime: PropTypes.number,
forceSave: PropTypes.bool.isRequired, forceSave: PropTypes.bool.isRequired,
__apiAttr: PropTypes.oneOf([PropTypes.number, PropTypes.string, ""]), __apiAttr: PropTypes.oneOf([PropTypes.number, PropTypes.string, ""]),
modelData: PropTypes.object.isRequired, modelData: PropTypes.object.isRequired,
open: PropTypes.bool.isRequired, open: PropTypes.bool.isRequired, // should the editor be opened
handleEditorWasClosed: PropTypes.func.isRequired, handleEditorWasClosed: PropTypes.func.isRequired,
dataToSave: PropTypes.object, dataToSave: PropTypes.object,
// props added in subclasses but are absolutely required to handle redux // props added in subclasses but are absolutely required to handle redux
......
...@@ -14,7 +14,7 @@ export default function getMapDispatchToPropsForEditor(name) { ...@@ -14,7 +14,7 @@ export default function getMapDispatchToPropsForEditor(name) {
saveData: (data) => dispatch(getActions(name).update(data)), saveData: (data) => dispatch(getActions(name).update(data)),
openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)), openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)),
closeFullScreenDialog: () => dispatch(closeFullScreenDialog()), closeFullScreenDialog: () => dispatch(closeFullScreenDialog()),
// clearSaveError: () => dispatch(savingHasErrorAction(false)) clearSaveError: () => dispatch(getActions(name).clearUpdateFailed()),
}; };
}; };
} }
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