Updates after review (outsource ServerNotices, update dataProvider)
This commit is contained in:
parent
136487bbe1
commit
175ea78c47
106
src/components/ServerNotices.js
Normal file
106
src/components/ServerNotices.js
Normal file
@ -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 => (
|
||||
<Toolbar {...props}>
|
||||
<SaveButton label="resources.servernotices.action.send" />
|
||||
<Button label="ra.action.cancel" onClick={handleDialogClose}>
|
||||
<IconCancel />
|
||||
</Button>
|
||||
</Toolbar>
|
||||
);
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<Button label="resources.servernotices.send" onClick={handleDialogOpen}>
|
||||
<MessageIcon />
|
||||
</Button>
|
||||
<Dialog open={open} onClose={handleDialogClose} loading={loading}>
|
||||
<DialogTitle>
|
||||
{translate("resources.servernotices.action.send")}
|
||||
</DialogTitle>
|
||||
<DialogContent>
|
||||
<DialogContentText>
|
||||
{translate("resources.servernotices.helper.send")}
|
||||
</DialogContentText>
|
||||
<SimpleForm
|
||||
toolbar={<ServernoticesToolbar />}
|
||||
submitOnEnter={false}
|
||||
redirect={false}
|
||||
save={handleConfirm}
|
||||
>
|
||||
<TextInput
|
||||
source="body"
|
||||
label="resources.servernotices.fields.body"
|
||||
multiline
|
||||
resettable
|
||||
validate={required()}
|
||||
/>
|
||||
</SimpleForm>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServerNoticesButton;
|
||||
@ -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 => (
|
||||
<Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />
|
||||
);
|
||||
|
||||
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 => (
|
||||
<Toolbar {...props}>
|
||||
<SaveButton
|
||||
label="resources.servernotices.action.send"
|
||||
submitOnEnter={false}
|
||||
/>
|
||||
<Button label="ra.action.cancel" onClick={handleDialogClose}>
|
||||
<IconCancel />
|
||||
</Button>
|
||||
</Toolbar>
|
||||
);
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<Button label="resources.servernotices.send" onClick={handleDialogOpen}>
|
||||
<MessageIcon />
|
||||
</Button>
|
||||
<Dialog fullWidth open={open} onClose={handleDialogClose}>
|
||||
<DialogTitle>
|
||||
{translate("resources.servernotices.action.send")}
|
||||
</DialogTitle>
|
||||
<DialogContent>
|
||||
<DialogContentText>
|
||||
{translate("resources.servernotices.helper.send")}
|
||||
</DialogContentText>
|
||||
<SimpleForm
|
||||
toolbar={<ServernoticesToolbar />}
|
||||
submitOnEnter={false}
|
||||
redirect={false}
|
||||
save={handleConfirm}
|
||||
>
|
||||
<TextInput
|
||||
source="body"
|
||||
label="resources.servernotices.fields.body"
|
||||
multiline
|
||||
resettable
|
||||
validate={required()}
|
||||
/>
|
||||
</SimpleForm>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
const UserFilter = props => (
|
||||
<Filter {...props}>
|
||||
<BooleanInput source="guests" alwaysOn />
|
||||
@ -148,7 +51,7 @@ const UserBulkActionButtons = props => {
|
||||
const translate = useTranslate();
|
||||
return (
|
||||
<Fragment>
|
||||
<ServernoticesButton {...props} method="multi" />
|
||||
<ServerNoticesButton {...props} />
|
||||
<BulkDeleteButton
|
||||
{...props}
|
||||
label="resources.users.action.erase"
|
||||
@ -207,7 +110,7 @@ const UserEditToolbar = props => {
|
||||
label="resources.users.action.erase"
|
||||
title={translate("resources.users.helper.erase")}
|
||||
/>
|
||||
<ServernoticesButton {...props} method="single" />
|
||||
<ServerNoticesButton {...props} />
|
||||
</Toolbar>
|
||||
);
|
||||
};
|
||||
|
||||
@ -64,7 +64,7 @@ export default {
|
||||
name: "Server Notices",
|
||||
send: "Send server notices",
|
||||
fields: {
|
||||
body: "Nachricht",
|
||||
body: "Message",
|
||||
},
|
||||
action: {
|
||||
send: "Send note",
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user