refactor ServerNotices
This commit is contained in:
parent
5f2964a5c0
commit
2b98ef0270
@ -7,6 +7,7 @@ import {
|
|||||||
Toolbar,
|
Toolbar,
|
||||||
required,
|
required,
|
||||||
useCreate,
|
useCreate,
|
||||||
|
useDataProvider,
|
||||||
useListContext,
|
useListContext,
|
||||||
useNotify,
|
useNotify,
|
||||||
useRecordContext,
|
useRecordContext,
|
||||||
@ -23,11 +24,11 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
|
|
||||||
const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
|
const ServerNoticeDialog = ({ open, loading, onClose, onSubmit }) => {
|
||||||
const translate = useTranslate();
|
const translate = useTranslate();
|
||||||
|
|
||||||
const ServerNoticeToolbar = props => (
|
const ServerNoticeToolbar = props => (
|
||||||
<Toolbar {...props}>
|
<Toolbar>
|
||||||
<SaveButton
|
<SaveButton
|
||||||
label="resources.servernotices.action.send"
|
label="resources.servernotices.action.send"
|
||||||
disabled={props.pristine}
|
disabled={props.pristine}
|
||||||
@ -47,11 +48,7 @@ const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
|
|||||||
<DialogContentText>
|
<DialogContentText>
|
||||||
{translate("resources.servernotices.helper.send")}
|
{translate("resources.servernotices.helper.send")}
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
<SimpleForm
|
<SimpleForm toolbar={<ServerNoticeToolbar />} onSubmit={onSubmit}>
|
||||||
toolbar={<ServerNoticeToolbar />}
|
|
||||||
redirect={false}
|
|
||||||
onSubmit={onSend}
|
|
||||||
>
|
|
||||||
<TextInput
|
<TextInput
|
||||||
source="body"
|
source="body"
|
||||||
label="resources.servernotices.fields.body"
|
label="resources.servernotices.fields.body"
|
||||||
@ -71,14 +68,15 @@ export const ServerNoticeButton = () => {
|
|||||||
const record = useRecordContext();
|
const record = useRecordContext();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const notify = useNotify();
|
const notify = useNotify();
|
||||||
const [create, { isloading }] = useCreate("servernotices");
|
const [create, { isloading }] = useCreate();
|
||||||
|
|
||||||
const handleDialogOpen = () => setOpen(true);
|
const handleDialogOpen = () => setOpen(true);
|
||||||
const handleDialogClose = () => setOpen(false);
|
const handleDialogClose = () => setOpen(false);
|
||||||
|
|
||||||
const handleSend = values => {
|
const handleSend = values => {
|
||||||
create(
|
create(
|
||||||
{ payload: { data: { id: record.id, ...values } } },
|
"servernotices",
|
||||||
|
{ data: { id: record.id, ...values } },
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
notify("resources.servernotices.action.send_success");
|
notify("resources.servernotices.action.send_success");
|
||||||
@ -104,38 +102,39 @@ export const ServerNoticeButton = () => {
|
|||||||
<ServerNoticeDialog
|
<ServerNoticeDialog
|
||||||
open={open}
|
open={open}
|
||||||
onClose={handleDialogClose}
|
onClose={handleDialogClose}
|
||||||
onSend={handleSend}
|
onSubmit={handleSend}
|
||||||
/>
|
/>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ServerNoticeBulkButton = () => {
|
export const ServerNoticeBulkButton = ({ body }) => {
|
||||||
const { selectedIds } = useListContext();
|
const { selectedIds } = useListContext();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const notify = useNotify();
|
|
||||||
const unselectAll = useUnselectAll("users");
|
|
||||||
const { createMany, isloading } = useMutation();
|
|
||||||
|
|
||||||
const handleDialogOpen = () => setOpen(true);
|
const handleDialogOpen = () => setOpen(true);
|
||||||
const handleDialogClose = () => setOpen(false);
|
const handleDialogClose = () => setOpen(false);
|
||||||
|
const notify = useNotify();
|
||||||
|
const unselectAll = useUnselectAll("users");
|
||||||
|
const dataProvider = useDataProvider();
|
||||||
|
|
||||||
const handleSend = values => {
|
const { mutate, isloading } = useMutation(
|
||||||
createMany(
|
data =>
|
||||||
["servernotices", "createMany", { ids: selectedIds, data: values }],
|
dataProvider.createMany("servernotices", {
|
||||||
{
|
ids: selectedIds,
|
||||||
onSuccess: data => {
|
data: data,
|
||||||
notify("resources.servernotices.action.send_success");
|
}),
|
||||||
unselectAll();
|
{
|
||||||
handleDialogClose();
|
onSuccess: () => {
|
||||||
},
|
notify("resources.servernotices.action.send_success");
|
||||||
onError: error =>
|
unselectAll();
|
||||||
notify("resources.servernotices.action.send_failure", {
|
handleDialogClose();
|
||||||
type: "error",
|
},
|
||||||
}),
|
onError: () =>
|
||||||
}
|
notify("resources.servernotices.action.send_failure", {
|
||||||
);
|
type: "error",
|
||||||
};
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
@ -149,7 +148,7 @@ export const ServerNoticeBulkButton = () => {
|
|||||||
<ServerNoticeDialog
|
<ServerNoticeDialog
|
||||||
open={open}
|
open={open}
|
||||||
onClose={handleDialogClose}
|
onClose={handleDialogClose}
|
||||||
onSend={handleSend}
|
onSubmit={mutate}
|
||||||
/>
|
/>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user