Files
synapse-admin/src/components/ServerNotices.jsx
T

156 lines
3.7 KiB
React
Raw Normal View History

2024-02-03 13:56:33 +01:00
import React, { useState } from "react";
2020-04-23 10:00:46 +02:00
import {
Button,
SaveButton,
SimpleForm,
TextInput,
Toolbar,
required,
useCreate,
2024-02-06 14:17:00 +01:00
useDataProvider,
2023-02-04 16:57:37 +01:00
useListContext,
2020-04-23 10:00:46 +02:00
useNotify,
useRecordContext,
2020-04-23 10:00:46 +02:00
useTranslate,
useUnselectAll,
2020-04-23 10:00:46 +02:00
} from "react-admin";
2023-02-04 16:57:37 +01:00
import { useMutation } from "react-query";
import MessageIcon from "@mui/icons-material/Message";
import IconCancel from "@mui/icons-material/Cancel";
import {
Dialog,
DialogContent,
DialogContentText,
DialogTitle,
} from "@mui/material";
2020-04-23 10:00:46 +02:00
2024-02-05 22:23:09 +01:00
const ServerNoticeDialog = ({ open, loading, onClose, onSubmit }) => {
2020-04-23 10:00:46 +02:00
const translate = useTranslate();
const ServerNoticeToolbar = props => (
<Toolbar {...props}>
<SaveButton
label="resources.servernotices.action.send"
disabled={props.pristine}
/>
2020-04-23 10:00:46 +02:00
<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>
2024-02-06 14:17:00 +01:00
<SimpleForm toolbar={<ServerNoticeToolbar />} onSubmit={onSubmit}>
2020-04-23 10:00:46 +02:00
<TextInput
source="body"
label="resources.servernotices.fields.body"
fullWidth
multiline
rows="4"
resettable
validate={required()}
/>
</SimpleForm>
</DialogContent>
</Dialog>
);
};
2023-02-04 16:57:37 +01:00
export const ServerNoticeButton = () => {
const record = useRecordContext();
2020-04-23 10:00:46 +02:00
const [open, setOpen] = useState(false);
const notify = useNotify();
2023-02-08 09:24:02 +01:00
const [create, { isloading }] = useCreate();
2020-04-23 10:00:46 +02:00
const handleDialogOpen = () => setOpen(true);
const handleDialogClose = () => setOpen(false);
const handleSend = values => {
create(
2023-02-08 09:24:02 +01:00
"servernotices",
{ data: { id: record.id, ...values } },
2020-04-23 10:00:46 +02:00
{
onSuccess: () => {
notify("resources.servernotices.action.send_success");
handleDialogClose();
},
2023-02-04 16:57:37 +01:00
onError: () =>
2023-01-24 15:28:01 +01:00
notify("resources.servernotices.action.send_failure", {
type: "error",
}),
2020-04-23 10:00:46 +02:00
}
);
};
return (
2024-02-03 13:56:33 +01:00
<>
2020-04-23 10:00:46 +02:00
<Button
label="resources.servernotices.send"
onClick={handleDialogOpen}
2023-02-04 16:57:37 +01:00
disabled={isloading}
2020-04-23 10:00:46 +02:00
>
<MessageIcon />
</Button>
<ServerNoticeDialog
open={open}
onClose={handleDialogClose}
2024-02-05 22:23:09 +01:00
onSubmit={handleSend}
2020-04-23 10:00:46 +02:00
/>
2024-02-03 13:56:33 +01:00
</>
2020-04-23 10:00:46 +02:00
);
};
2023-02-04 16:57:37 +01:00
export const ServerNoticeBulkButton = () => {
const { selectedIds } = useListContext();
const [open, setOpen] = useState(false);
2024-02-06 14:17:00 +01:00
const openDialog = () => setOpen(true);
const closeDialog = () => setOpen(false);
const notify = useNotify();
2024-02-05 22:03:34 +01:00
const unselectAllUsers = useUnselectAll("users");
2024-02-06 14:17:00 +01:00
const dataProvider = useDataProvider();
2024-02-06 14:17:00 +01:00
const { mutate: sendNotices, isLoading } = useMutation(
data =>
dataProvider.createMany("servernotices", {
ids: selectedIds,
data: data,
}),
{
onSuccess: () => {
notify("resources.servernotices.action.send_success");
unselectAllUsers();
closeDialog();
},
onError: () =>
notify("resources.servernotices.action.send_failure", {
type: "error",
}),
}
);
return (
2024-02-03 13:56:33 +01:00
<>
<Button
label="resources.servernotices.send"
2024-02-06 14:17:00 +01:00
onClick={openDialog}
disabled={isLoading}
>
<MessageIcon />
</Button>
<ServerNoticeDialog
open={open}
2024-02-06 14:17:00 +01:00
onClose={closeDialog}
onSubmit={sendNotices}
/>
2024-02-03 13:56:33 +01:00
</>
);
};