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

[RFR] Ensure DeleteButton is usable when record is not defined at mount time #3652

Merged
merged 1 commit into from
Sep 5, 2019
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
24 changes: 16 additions & 8 deletions packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, useState } from 'react';
import React, { Fragment, useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/core/styles';
import { fade } from '@material-ui/core/styles/colorManipulator';
Expand Down Expand Up @@ -64,7 +64,12 @@ const DeleteWithConfirmButton = ({
const redirect = useRedirect();
const refresh = useRefresh();
const classes = useStyles({ classes: classesOverride });
const [deleteOne, { loading }] = useDelete(resource, record.id, record, {

// We don't pass the action payload (the record and its identifier) at
// declaration time to avoid errors for people using the button in a
// component which may not have the record loaded immediately (for exemple
// in the actions of an Edit component)
const [deleteOne, { loading }] = useDelete(resource, undefined, undefined, {
onSuccess: () => {
notify('ra.notification.deleted', 'info', { smart_count: 1 });
redirect(redirectTo, basePath);
Expand All @@ -90,12 +95,15 @@ const DeleteWithConfirmButton = ({
e.stopPropagation();
};

const handleDelete = () => {
deleteOne();
if (typeof onClick === 'function') {
onClick();
}
};
const handleDelete = useCallback(
event => {
deleteOne(event, { id: record.id, previousData: record });
if (typeof onClick === 'function') {
onClick();
}
},
[deleteOne, onClick, record]
);

return (
<Fragment>
Expand Down
26 changes: 17 additions & 9 deletions packages/ra-ui-materialui/src/button/DeleteWithUndoButton.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/core/styles';
import { fade } from '@material-ui/core/styles/colorManipulator';
Expand Down Expand Up @@ -55,7 +55,12 @@ const DeleteWithUndoButton = ({
const notify = useNotify();
const redirect = useRedirect();
const refresh = useRefresh();
const [deleteOne, { loading }] = useDelete(resource, record.id, record, {

// We don't pass the action payload (the record and its identifier) at
// declaration time to avoid errors for people using the button in a
// component which may not have the record loaded immediately (for exemple
// in the actions of an Edit component)
const [deleteOne, { loading }] = useDelete(resource, undefined, undefined, {
onSuccess: () => {
notify('ra.notification.deleted', 'info', { smart_count: 1 }, true);
redirect(redirectTo, basePath);
Expand All @@ -70,13 +75,16 @@ const DeleteWithUndoButton = ({
),
undoable: true,
});
const handleDelete = event => {
event.stopPropagation();
deleteOne();
if (typeof onClick === 'function') {
onClick();
}
};
const handleDelete = useCallback(
event => {
event.stopPropagation();
deleteOne(event, { id: record.id, previousData: record });
if (typeof onClick === 'function') {
onClick();
}
},
[deleteOne, onClick, record]
);

return (
<Button
Expand Down