Add ServerNoticeButton to UserBulkActionButtons
This adds the button to send "Server Notices" to many users at once.
This commit is contained in:
parent
c41b8ab846
commit
6bd2c0ff19
@ -6,9 +6,10 @@ import {
|
||||
TextInput,
|
||||
Toolbar,
|
||||
required,
|
||||
useCreate,
|
||||
useMutation,
|
||||
useNotify,
|
||||
useTranslate,
|
||||
useUnselectAll,
|
||||
} from "react-admin";
|
||||
import MessageIcon from "@material-ui/icons/Message";
|
||||
import IconCancel from "@material-ui/icons/Cancel";
|
||||
@ -59,17 +60,23 @@ const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const ServerNoticeButton = ({ record }) => {
|
||||
export const ServerNoticeButton = ({ record, selectedIds }) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const notify = useNotify();
|
||||
const [create, { loading }] = useCreate("servernotices");
|
||||
const unselectAll = useUnselectAll();
|
||||
const [create, { loading }] = useMutation();
|
||||
|
||||
const handleDialogOpen = () => setOpen(true);
|
||||
const handleDialogClose = () => setOpen(false);
|
||||
|
||||
const handleSend = values => {
|
||||
if (record) {
|
||||
create(
|
||||
{ payload: { data: { id: record.id, ...values } } },
|
||||
{
|
||||
type: "create",
|
||||
resource: "servernotices",
|
||||
payload: { data: { id: record.id, ...values } },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
notify("resources.servernotices.action.send_success");
|
||||
@ -79,6 +86,24 @@ export const ServerNoticeButton = ({ record }) => {
|
||||
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 (
|
||||
|
@ -51,6 +51,7 @@ const UserBulkActionButtons = props => {
|
||||
const translate = useTranslate();
|
||||
return (
|
||||
<Fragment>
|
||||
<ServerNoticeButton {...props} />
|
||||
<BulkDeleteButton
|
||||
{...props}
|
||||
label="resources.users.action.erase"
|
||||
|
@ -221,6 +221,29 @@ const dataProvider = {
|
||||
}));
|
||||
},
|
||||
|
||||
createMany: (resource, params) => {
|
||||
console.log("createMany " + resource);
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
if (!("create" in res)) return Promise.reject();
|
||||
|
||||
return Promise.all(
|
||||
params.ids.map(id => {
|
||||
params.data.id = id;
|
||||
const cre = res["create"](params.data);
|
||||
const endpoint_url = homeserver + cre.endpoint;
|
||||
return jsonClient(endpoint_url, {
|
||||
method: cre.method,
|
||||
body: JSON.stringify(cre.body, filterNullValues),
|
||||
});
|
||||
})
|
||||
).then(responses => ({
|
||||
data: responses.map(({ json }) => json),
|
||||
}));
|
||||
},
|
||||
|
||||
delete: (resource, params) => {
|
||||
console.log("delete " + resource);
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
|
Loading…
Reference in New Issue
Block a user