Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Refresh of Lists when an optimistic update fails #4179

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cypress/integration/edit.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import createPageFactory from '../support/CreatePage';
import editPageFactory from '../support/EditPage';
import listPageFactory from '../support/ListPage';

describe('Edit Page', () => {
const EditPostPage = editPageFactory('/#/posts/5');
const CreatePostPage = createPageFactory('/#/posts/create');
const EditCommentPage = editPageFactory('/#/comments/5');
const ListPagePosts = listPageFactory('/#/posts');

describe('Title', () => {
it('should show the correct title in the appBar', () => {
Expand Down Expand Up @@ -176,4 +178,28 @@ describe('Edit Page', () => {
expect(el).to.have.value('')
);
});

it('should refresh the list when the update fails', () => {
ListPagePosts.navigate();
ListPagePosts.nextPage(); // Ensure the record is visible in the table

EditPostPage.navigate();
EditPostPage.setInputValue('input', 'title', 'f00bar');
EditPostPage.submit();

cy.get(ListPagePosts.elements.recordRows)
.eq(2)
.should(el => expect(el).to.contain('f00bar'));

cy.get('div[role="alertdialog"]');
cy.wait(4000); // Wait for the undo notification to disappear

cy.get('div[role="alertdialog"]').should(el =>
expect(el).to.have.text('this title cannot be used')
);

cy.get(ListPagePosts.elements.recordRows)
.eq(2)
.should(el => expect(el).to.contain('Sed quo et et fugiat modi'));
});
});
7 changes: 7 additions & 0 deletions examples/simple/src/dataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const sometimesFailsDataProvider = new Proxy(uploadCapableDataProvider, {
// if (name === 'delete' && resource === 'posts') {
// return Promise.reject(new Error('deletion error'));
// }
if (
resource === 'posts' &&
params.data &&
params.data.title === 'f00bar'
) {
return Promise.reject(new Error('this title cannot be used'));
}
return uploadCapableDataProvider[name](resource, params);
},
});
Expand Down
10 changes: 7 additions & 3 deletions packages/ra-core/src/controller/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,22 @@ const useEditController = (props: EditProps): EditControllerProps => {
},
onFailure: onFailure
? onFailure
: error =>
: error => {
notify(
typeof error === 'string'
? error
: error.message ||
'ra.notification.http_error',
'warning'
),
);
if (undoable) {
refresh();
}
},
undoable,
}
),
[basePath, notify, redirect, undoable, update, successMessage]
[update, undoable, notify, successMessage, redirect, basePath, refresh]
);

return {
Expand Down