From fec575597d1a2fed91766e04dda146def7a24f2e Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Tue, 17 Dec 2019 10:47:48 -0300 Subject: [PATCH] Refresh list on failure --- cypress/integration/edit.js | 26 +++++++++++++++++++ examples/simple/src/dataProvider.js | 7 +++++ .../src/controller/useEditController.ts | 10 ++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/cypress/integration/edit.js b/cypress/integration/edit.js index fc045d9c070..c8972e13339 100644 --- a/cypress/integration/edit.js +++ b/cypress/integration/edit.js @@ -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', () => { @@ -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')); + }); }); diff --git a/examples/simple/src/dataProvider.js b/examples/simple/src/dataProvider.js index b6328174ba1..6c2d718f774 100644 --- a/examples/simple/src/dataProvider.js +++ b/examples/simple/src/dataProvider.js @@ -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); }, }); diff --git a/packages/ra-core/src/controller/useEditController.ts b/packages/ra-core/src/controller/useEditController.ts index 7c6df1c1201..644ff9d6060 100644 --- a/packages/ra-core/src/controller/useEditController.ts +++ b/packages/ra-core/src/controller/useEditController.ts @@ -124,18 +124,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 {