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

View File

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