Commit d85a5f7f authored by Florent Chehab's avatar Florent Chehab
Browse files

World class error handling and closing editor on save

parent 30fb58ff
Pipeline #27264 passed with stages
in 2 minutes and 41 seconds
...@@ -70,10 +70,11 @@ function _ElSaveData(data, api_end_point, _ElIsSaving, _ElFetchDataSuccess, _ElI ...@@ -70,10 +70,11 @@ function _ElSaveData(data, api_end_point, _ElIsSaving, _ElFetchDataSuccess, _ElI
method = "PUT"; method = "PUT";
pk = data.id + '/'; pk = data.id + '/';
} }
let errorStatusText = '';
dispatch(_ElIsSaving(true)); dispatch(_ElIsSaving(true));
let token = Cookies.get('csrftoken'); let token = Cookies.get('csrftoken');
fetch(api_end_point+pk, { let f = fetch(api_end_point+pk, {
method: method, method: method,
credentials: 'same-origin', credentials: 'same-origin',
headers: { headers: {
...@@ -85,21 +86,29 @@ function _ElSaveData(data, api_end_point, _ElIsSaving, _ElFetchDataSuccess, _ElI ...@@ -85,21 +86,29 @@ function _ElSaveData(data, api_end_point, _ElIsSaving, _ElFetchDataSuccess, _ElI
}) })
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw Error(response.statusText); errorStatusText = response.statusText;
throw response;
} }
return response.json();
return response;
}) })
.then((response) => response.json())
.then((_El) => { .then((_El) => {
dispatch(_ElInvalidated(false)); dispatch(_ElInvalidated(false));
dispatch(_ElFetchDataSuccess(_El)); // we use the same here dispatch(_ElFetchDataSuccess(_El)); // we use the same here
dispatch(_ElIsSaving(false)); dispatch(_ElIsSaving(false));
}) })
.catch((e) => { .catch((e) => {
dispatch(_ElHasError(true, e)) if (typeof e.json == 'function'){
dispatch(_ElIsSaving(false)); return e.json()
}); } else {
return new Promise(function(resolve, reject) {resolve(e)});
}
})
.then( (errorContent) => {
if (typeof errorContent != 'undefined'){
dispatch(_ElHasError(true, {message: errorStatusText, content: errorContent}));
dispatch(_ElIsSaving(false));
}
})
}; };
} }
......
...@@ -10,21 +10,20 @@ import DialogTitle from '@material-ui/core/DialogTitle'; ...@@ -10,21 +10,20 @@ import DialogTitle from '@material-ui/core/DialogTitle';
class Alert extends React.Component { class Alert extends React.Component {
render() { render() {
console.log("alert props", this.props)
return ( return (
<Dialog <Dialog
open={this.props.open} open={this.props.open}
> >
<DialogTitle>{this.props.title}</DialogTitle> <DialogTitle>{this.props.title}</DialogTitle>
<DialogContent> <DialogContent>
<DialogContentText> <DialogContentText style={{whiteSpace: 'pre-wrap'}}>
{this.props.description} {this.props.description}
</DialogContentText> </DialogContentText>
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
{ {
this.props.info ? this.props.info ?
<Button onClick={() => { console.log("click"); this.props.handleClose(); this.props.handleResponse() }} color="primary"> <Button onClick={() => { this.props.handleClose(); this.props.handleResponse() }} color="primary">
{this.props.infoText} {this.props.infoText}
</Button> </Button>
: :
......
...@@ -26,7 +26,8 @@ class Editor extends MyComponent { ...@@ -26,7 +26,8 @@ class Editor extends MyComponent {
state = { state = {
alert: { alert: {
open: false, open: false,
} },
lastSave: this.props.lastSave,
} }
myComponentDidUpdate() { myComponentDidUpdate() {
...@@ -38,25 +39,29 @@ class Editor extends MyComponent { ...@@ -38,25 +39,29 @@ class Editor extends MyComponent {
open: true, open: true,
info: true, info: true,
title: "L'enregistrement sur le serveur a échoué.", title: "L'enregistrement sur le serveur a échoué.",
description: savingHasError.error + "\n \n Vous pourrez réessayer après avoir fermer cette alerte. Si l'erreur persiste, vérifier votre connexion internet ou contacter les administrateurs du site.", description: JSON.stringify(savingHasError.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", infoText: "J'ai compris",
handleResponse: () => this.props.clearSaveError() handleResponse: () => this.props.clearSaveError()
} }
}) })
} }
} }
if (this.state.lastSave < this.props.lastSave) {
//saving data was successfull
this.setState({ lastSave: this.props.lastSave });
this.props.handleClose();
}
}; };
handleCloseAlert() { handleCloseAlert() {
console.log("closing")
this.setState({ this.setState({
alert: { open: false } alert: { open: false }
}) })
} }
setFormDataAndError(field, data, type = null) { setFormDataAndError(field, data, type = null) {
console.log(data, type)
let correctData = data.data; let correctData = data.data;
if (type == 'date') { if (type == 'date') {
correctData = dateToDateStr(correctData); correctData = dateToDateStr(correctData);
......
...@@ -75,6 +75,7 @@ UniversitySemestersDatesEditor.propTypes = { ...@@ -75,6 +75,7 @@ UniversitySemestersDatesEditor.propTypes = {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
savingHasError: state.universitiesSemestersDatesEl.savingHasError, savingHasError: state.universitiesSemestersDatesEl.savingHasError,
lastSave: state.universitiesSemestersDatesEl.fetched.fetchedAt,
}; };
}; };
......
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