Skip to content

Commit

Permalink
Add ServerNoticeButton to UserEditToolbar
Browse files Browse the repository at this point in the history
For this, the feature "Server Notices" must be activated on the server.

Change-Id: If3873dc5548822a06a7be0c55e48835c9fb8f78f
  • Loading branch information
awesome-manuel committed May 5, 2020
1 parent 7f16f78 commit c41b8ab
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const App = () => (
/>
<Resource name="rooms" list={RoomList} icon={RoomIcon} />
<Resource name="connections" />
<Resource name="servernotices" />
</Admin>
);

Expand Down
100 changes: 100 additions & 0 deletions src/components/ServerNotices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, { Fragment, useState } from "react";
import {
Button,
SaveButton,
SimpleForm,
TextInput,
Toolbar,
required,
useCreate,
useNotify,
useTranslate,
} from "react-admin";
import MessageIcon from "@material-ui/icons/Message";
import IconCancel from "@material-ui/icons/Cancel";
import Dialog from "@material-ui/core/Dialog";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";

const ServerNoticeDialog = ({ open, loading, onClose, onSend }) => {
const translate = useTranslate();

const ServerNoticeToolbar = props => (
<Toolbar {...props}>
<SaveButton label="resources.servernotices.action.send" />
<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 />}
submitOnEnter={false}
redirect={false}
save={onSend}
>
<TextInput
source="body"
label="resources.servernotices.fields.body"
fullWidth
multiline
rows="4"
resettable
validate={required()}
/>
</SimpleForm>
</DialogContent>
</Dialog>
);
};

export const ServerNoticeButton = ({ record }) => {
const [open, setOpen] = useState(false);
const notify = useNotify();
const [create, { loading }] = useCreate("servernotices");

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();
},
onFailure: () =>
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>
);
};
2 changes: 2 additions & 0 deletions src/components/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
useTranslate,
Pagination,
} from "react-admin";
import { ServerNoticeButton } from "./ServerNotices";

const UserPagination = props => (
<Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />
Expand Down Expand Up @@ -108,6 +109,7 @@ const UserEditToolbar = props => {
label="resources.users.action.erase"
title={translate("resources.users.helper.erase")}
/>
<ServerNoticeButton />
</Toolbar>
);
};
Expand Down
16 changes: 16 additions & 0 deletions src/i18n/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ export default {
user_agent: "User Agent",
},
},
servernotices: {
name: "Serverbenachrichtigungen",
send: "Servernachricht versenden",
fields: {
body: "Nachricht",
},
action: {
send: "Sende Nachricht",
send_success: "Nachricht erfolgreich versendet.",
send_failure: "Beim Versenden ist ein Fehler aufgetreten.",
},
helper: {
send:
'Sendet eine Serverbenachrichtigung an die ausgewählten Nutzer. Hierfür muss das Feature "Server Notices" auf dem Server aktiviert sein.',
},
},
},
ra: {
...germanMessages.ra,
Expand Down
16 changes: 16 additions & 0 deletions src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,21 @@ export default {
user_agent: "User agent",
},
},
servernotices: {
name: "Server Notices",
send: "Send server notices",
fields: {
body: "Message",
},
action: {
send: "Send note",
send_success: "Server notice successfully sent.",
send_failure: "An error has occurred.",
},
helper: {
send:
'Sends a server notice to the selected users. The feature "Server Notices" has to be activated at the server.',
},
},
},
};
14 changes: 14 additions & 0 deletions src/synapse/dataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ const resourceMap = {
}),
data: "connections",
},
servernotices: {
map: n => ({ id: n.event_id }),
create: data => ({
endpoint: "/_synapse/admin/v1/send_server_notice",
body: {
user_id: data.id,
content: {
msgtype: "m.text",
body: data.body,
},
},
method: "POST",
}),
},
};

function filterNullValues(key, value) {
Expand Down

0 comments on commit c41b8ab

Please sign in to comment.