Skip to content

Commit

Permalink
Add ServerNoticeButton to UserBulkActionButtons (#41)
Browse files Browse the repository at this point in the history
This adds the button to send "Server Notices" to many users at once.
  • Loading branch information
dklimpel authored May 6, 2020
1 parent c41b8ab commit 009ce80
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/components/ServerNotices.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
Toolbar,
required,
useCreate,
useMutation,
useNotify,
useTranslate,
useUnselectAll,
} from "react-admin";
import MessageIcon from "@material-ui/icons/Message";
import IconCancel from "@material-ui/icons/Cancel";
Expand Down Expand Up @@ -98,3 +100,49 @@ export const ServerNoticeButton = ({ record }) => {
</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 (
<Fragment>
<Button
label="resources.servernotices.send"
onClick={handleDialogOpen}
disabled={loading}
>
<MessageIcon />
</Button>
<ServerNoticeDialog
open={open}
onClose={handleDialogClose}
onSend={handleSend}
/>
</Fragment>
);
};
3 changes: 2 additions & 1 deletion src/components/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]} />
Expand All @@ -51,6 +51,7 @@ const UserBulkActionButtons = props => {
const translate = useTranslate();
return (
<Fragment>
<ServerNoticeBulkButton {...props} />
<BulkDeleteButton
{...props}
label="resources.users.action.erase"
Expand Down
23 changes: 23 additions & 0 deletions src/synapse/dataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 009ce80

Please sign in to comment.