Skip to content

Commit

Permalink
#9 revert old state if save request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jschroeter committed Apr 16, 2018
1 parent 89aa344 commit f99b985
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/metaEditable/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ const actions = ({ setState }) => ({
},

handleSave: (state, newMeta, src) => {
// we're optimistic, so update client state
// keep old state for the case we need to revert
const oldData = JSON.stringify(state.data);

// we're optimistic, so update client state immediately
const newData = setNewMetaInTree(
JSON.parse(JSON.stringify(state.data)),
newMeta
Expand All @@ -63,7 +66,8 @@ const actions = ({ setState }) => ({
}).then(response => {
if (!response.ok) {
alert(`Error while saving: ${response.statusText} ${response.url}`);
// TODO revert client state
// revert client state
setState({ data: JSON.parse(oldData) });
}
});
}
Expand Down

0 comments on commit f99b985

Please sign in to comment.