forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRejectButton.tsx
63 lines (58 loc) · 1.67 KB
/
RejectButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as React from 'react';
import PropTypes from 'prop-types';
import Button from '@mui/material/Button';
import ThumbDown from '@mui/icons-material/ThumbDown';
import {
useTranslate,
useUpdate,
useNotify,
useRedirect,
useRecordContext,
} from 'react-admin';
import { Review } from '../types';
/**
* This custom button demonstrate using a custom action to update data
*/
const RejectButton = () => {
const translate = useTranslate();
const notify = useNotify();
const redirectTo = useRedirect();
const record = useRecordContext<Review>();
const [reject, { isLoading }] = useUpdate(
'reviews',
{ id: record.id, data: { status: 'rejected' }, previousData: record },
{
mutationMode: 'undoable',
onSuccess: () => {
notify('resources.reviews.notification.rejected_success', {
type: 'info',
undoable: true,
});
redirectTo('/reviews');
},
onError: () => {
notify('resources.reviews.notification.rejected_error', {
type: 'error',
});
},
}
);
return record && record.status === 'pending' ? (
<Button
variant="outlined"
color="primary"
size="small"
onClick={() => reject()}
startIcon={<ThumbDown sx={{ color: 'red' }} />}
disabled={isLoading}
>
{translate('resources.reviews.action.reject')}
</Button>
) : (
<span />
);
};
RejectButton.propTypes = {
record: PropTypes.any,
};
export default RejectButton;