From 06ff1e8ecfa73abf9c66d78bb9842245c9e6ffb6 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Mon, 8 Feb 2021 10:40:23 +0100 Subject: [PATCH 1/2] Fix DeleteWithConfirmButton Does Not Allow to Override resource --- .../button/DeleteWithConfirmButton.spec.tsx | 43 +++++++++++++++++++ .../src/button/DeleteWithConfirmButton.tsx | 3 +- .../ra-ui-materialui/src/form/Toolbar.tsx | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.spec.tsx b/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.spec.tsx index 06c3a750102..2a62c98d989 100644 --- a/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.spec.tsx +++ b/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.spec.tsx @@ -74,6 +74,49 @@ describe('', () => { undoable: false, }; + it('should allow to override the resource', async () => { + const dataProvider = ({ + getOne: () => + Promise.resolve({ + data: { id: 123, title: 'lorem' }, + }), + delete: jest.fn().mockResolvedValueOnce({ data: { id: 123 } }), + } as unknown) as DataProvider; + const EditToolbar = props => ( + + + + ); + const { + queryByDisplayValue, + getByLabelText, + getByText, + } = renderWithRedux( + + + + }> + + + + + , + { admin: { resources: { posts: { data: {} } } } } + ); + // waitFor for the dataProvider.getOne() return + await waitFor(() => { + expect(queryByDisplayValue('lorem')).not.toBeNull(); + }); + fireEvent.click(getByLabelText('ra.action.delete')); + fireEvent.click(getByText('ra.action.confirm')); + await waitFor(() => { + expect(dataProvider.delete).toHaveBeenCalledWith('comments', { + id: 123, + previousData: { id: 123, title: 'lorem' }, + }); + }); + }); + it('should allow to override the onSuccess side effects', async () => { const dataProvider = ({ getOne: () => diff --git a/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx b/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx index 0e4a3063e6b..73d9e0b68c6 100644 --- a/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx +++ b/packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx @@ -44,6 +44,7 @@ const DeleteWithConfirmButton: FC = props => { } = props; const translate = useTranslate(); const classes = useStyles(props); + const resource = useResourceContext(props); const { open, loading, @@ -58,8 +59,8 @@ const DeleteWithConfirmButton: FC = props => { onClick, onSuccess, onFailure, + resource, }); - const resource = useResourceContext(props); return ( diff --git a/packages/ra-ui-materialui/src/form/Toolbar.tsx b/packages/ra-ui-materialui/src/form/Toolbar.tsx index f1b8d67c552..f761700df98 100644 --- a/packages/ra-ui-materialui/src/form/Toolbar.tsx +++ b/packages/ra-ui-materialui/src/form/Toolbar.tsx @@ -184,6 +184,7 @@ const Toolbar: FC = props => { button.props.undoable, undoable ), + ...button.props, }) : null ) From 0e3e4710b34f5fa7d8e72054af80b74237733432 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 12 Feb 2021 10:11:55 +0100 Subject: [PATCH 2/2] Apply Review --- packages/ra-ui-materialui/src/form/Toolbar.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/ra-ui-materialui/src/form/Toolbar.tsx b/packages/ra-ui-materialui/src/form/Toolbar.tsx index f761700df98..3b51942aecf 100644 --- a/packages/ra-ui-materialui/src/form/Toolbar.tsx +++ b/packages/ra-ui-materialui/src/form/Toolbar.tsx @@ -161,7 +161,10 @@ const Toolbar: FC = props => { Children.map(children, (button: ReactElement) => button && isValidElement(button) ? React.cloneElement(button, { - basePath, + basePath: valueOrDefault( + button.props.basePath, + basePath + ), handleSubmit: valueOrDefault( button.props.handleSubmit, handleSubmit @@ -173,8 +176,14 @@ const Toolbar: FC = props => { onSave: button.props.onSave, invalid, pristine, - record, - resource, + record: valueOrDefault( + button.props.record, + record + ), + resource: valueOrDefault( + button.props.resource, + resource + ), saving, submitOnEnter: valueOrDefault( button.props.submitOnEnter, @@ -184,7 +193,6 @@ const Toolbar: FC = props => { button.props.undoable, undoable ), - ...button.props, }) : null )