Add ServerNoticeBulkButton

Seperate ServerNoticeButton and ServerNoticeBulkButton into two button.
ServerNoticeButton for 'create' ServerNotice and ServerNoticeBulkButton
for 'createMany' ServerNotices.
This commit is contained in:
dklimpel 2020-05-06 07:41:16 +02:00
parent 6bd2c0ff19
commit 7974bfd16e
2 changed files with 61 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import {
TextInput,
Toolbar,
required,
useCreate,
useMutation,
useNotify,
useTranslate,
@ -60,23 +61,17 @@ const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
);
};
export const ServerNoticeButton = ({ record, selectedIds }) => {
export const ServerNoticeButton = ({ record }) => {
const [open, setOpen] = useState(false);
const notify = useNotify();
const unselectAll = useUnselectAll();
const [create, { loading }] = useMutation();
const [create, { loading }] = useCreate("servernotices");
const handleDialogOpen = () => setOpen(true);
const handleDialogClose = () => setOpen(false);
const handleSend = values => {
if (record) {
create(
{
type: "create",
resource: "servernotices",
payload: { data: { id: record.id, ...values } },
},
{ payload: { data: { id: record.id, ...values } } },
{
onSuccess: () => {
notify("resources.servernotices.action.send_success");
@ -86,24 +81,52 @@ export const ServerNoticeButton = ({ record, selectedIds }) => {
notify("resources.servernotices.action.send_failure", "error"),
}
);
} else {
create(
{
type: "createMany",
resource: "servernotices",
payload: { ids: selectedIds, data: values },
},
{
onSuccess: ({ data }) => {
notify("resources.servernotices.action.send_success");
unselectAll("users");
handleDialogClose();
},
onFailure: error =>
notify("resources.servernotices.action.send_failure", "error"),
}
);
}
};
return (
<Fragment>
<Button
label="resources.servernotices.send"
onClick={handleDialogOpen}
disabled={loading}
>
<MessageIcon />
</Button>
<ServerNoticeDialog
open={open}
onClose={handleDialogClose}
onSend={handleSend}
/>
</Fragment>
);
};
export const ServerNoticeBulkButton = ({ selectedIds }) => {
const [open, setOpen] = useState(false);
const notify = useNotify();
const unselectAll = useUnselectAll();
const [createMany, { loading }] = useMutation();
const handleDialogOpen = () => setOpen(true);
const handleDialogClose = () => setOpen(false);
const handleSend = values => {
createMany(
{
type: "createMany",
resource: "servernotices",
payload: { ids: selectedIds, data: values },
},
{
onSuccess: ({ data }) => {
notify("resources.servernotices.action.send_success");
unselectAll("users");
handleDialogClose();
},
onFailure: error =>
notify("resources.servernotices.action.send_failure", "error"),
}
);
};
return (

View File

@ -30,7 +30,7 @@ import {
useTranslate,
Pagination,
} from "react-admin";
import { ServerNoticeButton } from "./ServerNotices";
import { ServerNoticeButton, ServerNoticeBulkButton } from "./ServerNotices";
const UserPagination = props => (
<Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />
@ -51,7 +51,7 @@ const UserBulkActionButtons = props => {
const translate = useTranslate();
return (
<Fragment>
<ServerNoticeButton {...props} />
<ServerNoticeBulkButton {...props} />
<BulkDeleteButton
{...props}
label="resources.users.action.erase"