From 175ea78c4756901f632089258ec92e7c5c51e5ab Mon Sep 17 00:00:00 2001
From: dklimpel <5740567+dklimpel@users.noreply.github.com>
Date: Wed, 22 Apr 2020 20:39:07 +0200
Subject: [PATCH] Updates after review (outsource ServerNotices, update
dataProvider)
---
src/components/ServerNotices.js | 106 ++++++++++++++++++++++++++++++++
src/components/users.js | 105 ++-----------------------------
src/i18n/en.js | 2 +-
src/synapse/dataProvider.js | 104 ++++++++++++++++---------------
4 files changed, 167 insertions(+), 150 deletions(-)
create mode 100644 src/components/ServerNotices.js
diff --git a/src/components/ServerNotices.js b/src/components/ServerNotices.js
new file mode 100644
index 0000000..f7e64eb
--- /dev/null
+++ b/src/components/ServerNotices.js
@@ -0,0 +1,106 @@
+import React, { Fragment, useState } from "react";
+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";
+import {
+ TextInput,
+ SaveButton,
+ useTranslate,
+ Button,
+ useNotify,
+ useUnselectAll,
+ required,
+ Toolbar,
+ SimpleForm,
+ useMutation,
+} from "react-admin";
+
+const ServerNoticesButton = ({ record, selectedIds }) => {
+ const [open, setOpen] = useState(false);
+ const notify = useNotify();
+ const unselectAll = useUnselectAll();
+ const translate = useTranslate();
+ const handleDialogOpen = () => setOpen(true);
+ const handleDialogClose = () => setOpen(false);
+ const [mutate, { loading }] = useMutation();
+ const handleConfirm = values => {
+ if (Array.isArray(selectedIds)) {
+ mutate(
+ {
+ type: "sendMessageMany",
+ resource: "servernotices",
+ payload: { ids: selectedIds, data: values },
+ },
+ {
+ onSuccess: ({ data }) => {
+ notify("resources.servernotices.action.send_success");
+ unselectAll("users");
+ },
+ onFailure: error =>
+ notify("resources.servernotices.action.send_failure", "error"),
+ }
+ );
+ } else {
+ mutate(
+ {
+ type: "sendMessage",
+ resource: "servernotices",
+ payload: { id: record.id, data: values },
+ },
+ {
+ onSuccess: ({ data }) =>
+ notify("resources.servernotices.action.send_success"),
+ onFailure: error =>
+ notify("resources.servernotices.action.send_failure", "error"),
+ }
+ );
+ }
+ handleDialogClose();
+ };
+
+ const ServernoticesToolbar = props => (
+
+
+
+
+ );
+
+ return (
+
+
+
+
+ );
+};
+
+export default ServerNoticesButton;
diff --git a/src/components/users.js b/src/components/users.js
index fd5e9e9..bab11d6 100644
--- a/src/components/users.js
+++ b/src/components/users.js
@@ -1,8 +1,7 @@
-import React, { Fragment, useState } from "react";
+import React, { Fragment } from "react";
import PersonPinIcon from "@material-ui/icons/PersonPin";
import SettingsInputComponentIcon from "@material-ui/icons/SettingsInputComponent";
-import MessageIcon from "@material-ui/icons/Message";
-import IconCancel from "@material-ui/icons/Cancel";
+import ServerNoticesButton from "./ServerNotices";
import {
ArrayInput,
ArrayField,
@@ -31,108 +30,12 @@ import {
regex,
useTranslate,
Pagination,
- Button,
- useNotify,
- useUnselectAll,
- required,
- fetchStart,
- fetchEnd,
} from "react-admin";
-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";
-import dataProvider from "../synapse/dataProvider.js";
const UserPagination = props => (
);
-const ServernoticesButton = ({ record, selectedIds, method = "single" }) => {
- const [open, setOpen] = useState(false);
- const notify = useNotify();
- const unselectAll = useUnselectAll();
- const translate = useTranslate();
- const handleDialogOpen = () => setOpen(true);
- const handleDialogClose = () => setOpen(false);
- const handleConfirm = values => {
- fetchStart();
-
- if (method === "multi") {
- console.log(method);
- dataProvider
- .updateMany("servernotices", { ids: selectedIds, data: values })
- .then(({ response }) => {
- notify("resources.servernotices.action.send_success");
- unselectAll("users");
- })
- .catch(error => {
- notify("resources.servernotices.action.send_failure", "error");
- })
- .finally(() => {
- fetchEnd();
- });
- } else {
- dataProvider
- .update("servernotices", { data: { ...values, ...{ id: record.id } } })
- .then(({ response }) => {
- notify("resources.servernotices.action.send_success");
- })
- .catch(error => {
- notify("resources.servernotices.action.send_failure", "error");
- })
- .finally(() => {
- fetchEnd();
- });
- }
- handleDialogClose();
- };
-
- const ServernoticesToolbar = props => (
-
-
-
-
- );
-
- return (
-
-
-
-
- );
-};
-
const UserFilter = props => (
@@ -148,7 +51,7 @@ const UserBulkActionButtons = props => {
const translate = useTranslate();
return (
-
+
{
label="resources.users.action.erase"
title={translate("resources.users.helper.erase")}
/>
-
+
);
};
diff --git a/src/i18n/en.js b/src/i18n/en.js
index 54e20c9..4f92669 100644
--- a/src/i18n/en.js
+++ b/src/i18n/en.js
@@ -64,7 +64,7 @@ export default {
name: "Server Notices",
send: "Send server notices",
fields: {
- body: "Nachricht",
+ body: "Message",
},
action: {
send: "Send note",
diff --git a/src/synapse/dataProvider.js b/src/synapse/dataProvider.js
index 448c225..79a3f13 100644
--- a/src/synapse/dataProvider.js
+++ b/src/synapse/dataProvider.js
@@ -58,13 +58,9 @@ const resourceMap = {
data: "connections",
},
servernotices: {
- map: s => ({
- ...s,
- id: s.user_id,
- }),
data: "servernotices",
- update: (id, params) => ({
- endpoint: `/_synapse/admin/v1/send_server_notice`,
+ sendMessage: (id, params) => ({
+ endpoint: "/_synapse/admin/v1/send_server_notice",
body: {
user_id: `${id}`,
content: {
@@ -181,24 +177,13 @@ const dataProvider = {
const res = resourceMap[resource];
- if ("update" in res) {
- const upd = res["update"](params.data.id, params.data);
- const homeserver_url = homeserver + upd.endpoint;
- return jsonClient(homeserver_url, {
- method: upd.method,
- body: JSON.stringify(upd.body),
- }).then(({ json }) => ({
- data: json,
- }));
- } else {
- const homeserver_url = homeserver + res.path;
- return jsonClient(`${homeserver_url}/${params.data.id}`, {
- method: "PUT",
- body: JSON.stringify(params.data, filterNullValues),
- }).then(({ json }) => ({
- data: res.map(json),
- }));
- }
+ const homeserver_url = homeserver + res.path;
+ return jsonClient(`${homeserver_url}/${params.data.id}`, {
+ method: "PUT",
+ body: JSON.stringify(params.data, filterNullValues),
+ }).then(({ json }) => ({
+ data: res.map(json),
+ }));
},
updateMany: (resource, params) => {
@@ -208,30 +193,15 @@ const dataProvider = {
const res = resourceMap[resource];
- if ("update" in res) {
- return Promise.all(
- params.ids.map(id => {
- const upd = res["update"](id, params.data);
- const homeserver_url = homeserver + upd.endpoint;
- return jsonClient(homeserver_url, {
- method: upd.method,
- body: JSON.stringify(upd.body),
- });
- })
- ).then(responses => ({
- data: responses.map(({ json }) => json),
- }));
- } else {
- const homeserver_url = homeserver + res.path;
- return Promise.all(
- params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), {
- method: "PUT",
- body: JSON.stringify(params.data, filterNullValues),
- })
- ).then(responses => ({
- data: responses.map(({ json }) => json),
- }));
- }
+ const homeserver_url = homeserver + res.path;
+ return Promise.all(
+ params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), {
+ method: "PUT",
+ body: JSON.stringify(params.data, filterNullValues),
+ })
+ ).then(responses => ({
+ data: responses.map(({ json }) => json),
+ }));
},
create: (resource, params) => {
@@ -311,6 +281,44 @@ const dataProvider = {
}));
}
},
+
+ sendMessage: (resource, params) => {
+ console.log("sendMessage " + resource);
+ const homeserver = localStorage.getItem("base_url");
+ if (!homeserver || !(resource in resourceMap)) return Promise.reject();
+
+ const res = resourceMap[resource];
+
+ const sendmsg = res["sendMessage"](params.id, params.data);
+ const homeserver_url = homeserver + sendmsg.endpoint;
+ return jsonClient(homeserver_url, {
+ method: sendmsg.method,
+ body: JSON.stringify(sendmsg.body),
+ }).then(({ json }) => ({
+ data: json,
+ }));
+ },
+
+ sendMessageMany: (resource, params) => {
+ console.log("sendMessageMany " + resource);
+ const homeserver = localStorage.getItem("base_url");
+ if (!homeserver || !(resource in resourceMap)) return Promise.reject();
+
+ const res = resourceMap[resource];
+
+ return Promise.all(
+ params.ids.map(id => {
+ const sendmsg = res["sendMessage"](id, params.data);
+ const homeserver_url = homeserver + sendmsg.endpoint;
+ return jsonClient(homeserver_url, {
+ method: sendmsg.method,
+ body: JSON.stringify(sendmsg.body),
+ });
+ })
+ ).then(responses => ({
+ data: responses.map(({ json }) => json),
+ }));
+ },
};
export default dataProvider;