2024-02-03 15:56:33 +03:00
|
|
|
import React, { useState } from "react";
|
2020-04-23 11:00:46 +03:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
SaveButton,
|
|
|
|
SimpleForm,
|
|
|
|
TextInput,
|
|
|
|
Toolbar,
|
|
|
|
required,
|
|
|
|
useCreate,
|
2023-02-04 18:57:37 +03:00
|
|
|
useListContext,
|
2020-04-23 11:00:46 +03:00
|
|
|
useNotify,
|
2023-01-24 18:36:08 +03:00
|
|
|
useRecordContext,
|
2020-04-23 11:00:46 +03:00
|
|
|
useTranslate,
|
2020-05-06 10:03:33 +03:00
|
|
|
useUnselectAll,
|
2020-04-23 11:00:46 +03:00
|
|
|
} from "react-admin";
|
2023-02-04 18:57:37 +03:00
|
|
|
import { useMutation } from "react-query";
|
2023-01-24 23:27:02 +03:00
|
|
|
import MessageIcon from "@mui/icons-material/Message";
|
|
|
|
import IconCancel from "@mui/icons-material/Cancel";
|
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
|
|
|
DialogTitle,
|
|
|
|
} from "@mui/material";
|
2020-04-23 11:00:46 +03:00
|
|
|
|
|
|
|
const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
|
|
|
|
const translate = useTranslate();
|
|
|
|
|
|
|
|
const ServerNoticeToolbar = props => (
|
|
|
|
<Toolbar {...props}>
|
2021-11-15 23:02:47 +03:00
|
|
|
<SaveButton
|
|
|
|
label="resources.servernotices.action.send"
|
|
|
|
disabled={props.pristine}
|
|
|
|
/>
|
2020-04-23 11:00:46 +03:00
|
|
|
<Button label="ra.action.cancel" onClick={onClose}>
|
|
|
|
<IconCancel />
|
|
|
|
</Button>
|
|
|
|
</Toolbar>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={open} onClose={onClose} loading={loading}>
|
|
|
|
<DialogTitle>
|
|
|
|
{translate("resources.servernotices.action.send")}
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
{translate("resources.servernotices.helper.send")}
|
|
|
|
</DialogContentText>
|
|
|
|
<SimpleForm
|
|
|
|
toolbar={<ServerNoticeToolbar />}
|
|
|
|
redirect={false}
|
|
|
|
save={onSend}
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
source="body"
|
|
|
|
label="resources.servernotices.fields.body"
|
|
|
|
fullWidth
|
|
|
|
multiline
|
|
|
|
rows="4"
|
|
|
|
resettable
|
|
|
|
validate={required()}
|
|
|
|
/>
|
|
|
|
</SimpleForm>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-02-04 18:57:37 +03:00
|
|
|
export const ServerNoticeButton = () => {
|
2023-01-24 18:36:08 +03:00
|
|
|
const record = useRecordContext();
|
2020-04-23 11:00:46 +03:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const notify = useNotify();
|
2023-02-04 18:57:37 +03:00
|
|
|
const [create, { isloading }] = useCreate("servernotices");
|
2020-04-23 11:00:46 +03:00
|
|
|
|
|
|
|
const handleDialogOpen = () => setOpen(true);
|
|
|
|
const handleDialogClose = () => setOpen(false);
|
|
|
|
|
|
|
|
const handleSend = values => {
|
|
|
|
create(
|
|
|
|
{ payload: { data: { id: record.id, ...values } } },
|
|
|
|
{
|
|
|
|
onSuccess: () => {
|
|
|
|
notify("resources.servernotices.action.send_success");
|
|
|
|
handleDialogClose();
|
|
|
|
},
|
2023-02-04 18:57:37 +03:00
|
|
|
onError: () =>
|
2023-01-24 17:28:01 +03:00
|
|
|
notify("resources.servernotices.action.send_failure", {
|
|
|
|
type: "error",
|
|
|
|
}),
|
2020-04-23 11:00:46 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-02-03 15:56:33 +03:00
|
|
|
<>
|
2020-04-23 11:00:46 +03:00
|
|
|
<Button
|
|
|
|
label="resources.servernotices.send"
|
|
|
|
onClick={handleDialogOpen}
|
2023-02-04 18:57:37 +03:00
|
|
|
disabled={isloading}
|
2020-04-23 11:00:46 +03:00
|
|
|
>
|
|
|
|
<MessageIcon />
|
|
|
|
</Button>
|
|
|
|
<ServerNoticeDialog
|
|
|
|
open={open}
|
|
|
|
onClose={handleDialogClose}
|
|
|
|
onSend={handleSend}
|
|
|
|
/>
|
2024-02-03 15:56:33 +03:00
|
|
|
</>
|
2020-04-23 11:00:46 +03:00
|
|
|
);
|
|
|
|
};
|
2020-05-06 10:03:33 +03:00
|
|
|
|
2023-02-04 18:57:37 +03:00
|
|
|
export const ServerNoticeBulkButton = () => {
|
|
|
|
const { selectedIds } = useListContext();
|
2020-05-06 10:03:33 +03:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const notify = useNotify();
|
|
|
|
const unselectAll = useUnselectAll();
|
2023-02-04 18:57:37 +03:00
|
|
|
const { createMany, isloading } = useMutation();
|
2020-05-06 10:03:33 +03:00
|
|
|
|
|
|
|
const handleDialogOpen = () => setOpen(true);
|
|
|
|
const handleDialogClose = () => setOpen(false);
|
|
|
|
|
|
|
|
const handleSend = values => {
|
|
|
|
createMany(
|
2023-02-04 18:57:37 +03:00
|
|
|
["servernotices", "createMany", { ids: selectedIds, data: values }],
|
2020-05-06 10:03:33 +03:00
|
|
|
{
|
2023-02-04 18:57:37 +03:00
|
|
|
onSuccess: data => {
|
2020-05-06 10:03:33 +03:00
|
|
|
notify("resources.servernotices.action.send_success");
|
|
|
|
unselectAll("users");
|
|
|
|
handleDialogClose();
|
|
|
|
},
|
2023-02-04 18:57:37 +03:00
|
|
|
onError: error =>
|
2023-01-24 17:28:01 +03:00
|
|
|
notify("resources.servernotices.action.send_failure", {
|
|
|
|
type: "error",
|
|
|
|
}),
|
2020-05-06 10:03:33 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-02-03 15:56:33 +03:00
|
|
|
<>
|
2020-05-06 10:03:33 +03:00
|
|
|
<Button
|
|
|
|
label="resources.servernotices.send"
|
|
|
|
onClick={handleDialogOpen}
|
2023-02-04 18:57:37 +03:00
|
|
|
disabled={isloading}
|
2020-05-06 10:03:33 +03:00
|
|
|
>
|
|
|
|
<MessageIcon />
|
|
|
|
</Button>
|
|
|
|
<ServerNoticeDialog
|
|
|
|
open={open}
|
|
|
|
onClose={handleDialogClose}
|
|
|
|
onSend={handleSend}
|
|
|
|
/>
|
2024-02-03 15:56:33 +03:00
|
|
|
</>
|
2020-05-06 10:03:33 +03:00
|
|
|
);
|
|
|
|
};
|