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

Restored alerts in Editor

parent 1373af74
......@@ -235,6 +235,14 @@ export default class CrudActions {
);
}
clearUpdateFailed() {
return {
type: this.types.updateFailed,
failed: false,
error: null
};
}
///////////
// NOT CRUD FUNCTIONS
///////////
......
......@@ -9,7 +9,6 @@ import Typography from "@material-ui/core/Typography";
import CloseIcon from "@material-ui/icons/Close";
import Alert from "./Alert";
// import renderFieldsMixIn from "./editorFunctions/renderFieldsMixIn";
// Form is imported only for type hints
......@@ -34,17 +33,20 @@ import Form from "./Form";
*/
class Editor extends Component {
// handling of the alert
state = {
alert: {
open: false,
},
}
formRef = React.createRef();
// Store the "saving" notification so that we can delete it when done saving.
savingNotification = null;
// reference to the form that the editor should contain
formRef = React.createRef();
/**
* Returns the form instance associated with the editor
*
......@@ -142,6 +144,8 @@ class Editor extends Component {
componentDidUpdate(prevProps) {
// 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) {
this.props.openFullScreenDialog(this.renderEditor());
}
......@@ -159,9 +163,8 @@ class Editor extends Component {
// we make to notify if saving to the server has errors
const { savingHasError } = this.props;
if (savingHasError.status && !this.state.alert.open) {
this.alertSaveFailed();
this.handleCloseNotificationRequest(savingHasError.error);
if (savingHasError.failed && !this.state.alert.open) {
this.alertSaveFailed(savingHasError.error.content.toString());
}
//saving data was successful
......@@ -241,7 +244,40 @@ class Editor extends Component {
///////////////////
// 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() {
this.setState({
......@@ -249,7 +285,8 @@ class Editor extends Component {
});
}
// Notifications and alerts from JS
// Notifications related
notifyNoChangesDetected() {
this.props.enqueueSnackbar("Aucun changement n'a été repéré.", { variant: "info" });
......@@ -264,44 +301,15 @@ class Editor extends Component {
}
notifySaveSuccessful(message) {
this.props.closeSnackbar(this.savingNotification);
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()
// }
// });
this.removeSavingNotification();
this.props.enqueueSnackbar(message, { variant: "success", autoHideDuration: 5000 });
}
alertChangesNotSaved() {
console.log("changes not saved");
// 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.props.handleEditorWasClosed();
// }
// },
// }
// });
removeSavingNotification() {
if (this.savingNotification) {
this.props.closeSnackbar(this.savingNotification);
this.savingNotification = null;
}
}
}
......@@ -311,14 +319,14 @@ class Editor extends Component {
Editor.propTypes = {
classes: PropTypes.object.isRequired,
// clearSaveError: PropTypes.func.isRequired,
savingHasError: PropTypes.object.isRequired,
clearSaveError: PropTypes.func.isRequired,
lastUpdateTimeInModel: PropTypes.string,
lastSaveTime: PropTypes.number,
forceSave: PropTypes.bool.isRequired,
__apiAttr: PropTypes.oneOf([PropTypes.number, PropTypes.string, ""]),
modelData: PropTypes.object.isRequired,
open: PropTypes.bool.isRequired,
open: PropTypes.bool.isRequired, // should the editor be opened
handleEditorWasClosed: PropTypes.func.isRequired,
dataToSave: PropTypes.object,
// props added in subclasses but are absolutely required to handle redux
......
......@@ -14,7 +14,7 @@ export default function getMapDispatchToPropsForEditor(name) {
saveData: (data) => dispatch(getActions(name).update(data)),
openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)),
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