1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-25 20:59:36 +01:00
ntfy/web/src/components/ReserveDialogs.js

200 lines
8.4 KiB
JavaScript
Raw Normal View History

2023-02-01 03:39:30 +01:00
import * as React from 'react';
2023-02-02 21:19:37 +01:00
import {useState} from 'react';
2023-02-01 03:39:30 +01:00
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
2023-02-02 21:19:37 +01:00
import {Alert, FormControl, Select, useMediaQuery} from "@mui/material";
2023-02-01 03:39:30 +01:00
import theme from "./theme";
2023-02-02 21:19:37 +01:00
import {validTopic} from "../app/utils";
2023-02-01 03:39:30 +01:00
import DialogFooter from "./DialogFooter";
import {useTranslation} from "react-i18next";
import session from "../app/Session";
import routes from "./routes";
2023-02-02 21:19:37 +01:00
import accountApi, {Permission} from "../app/AccountApi";
2023-02-01 03:39:30 +01:00
import ReserveTopicSelect from "./ReserveTopicSelect";
import MenuItem from "@mui/material/MenuItem";
import ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import {Check, DeleteForever} from "@mui/icons-material";
2023-02-02 21:19:37 +01:00
import {TopicReservedError, UnauthorizedError} from "../app/errors";
2023-02-01 03:39:30 +01:00
export const ReserveAddDialog = (props) => {
const { t } = useTranslation();
2023-02-02 21:19:37 +01:00
const [error, setError] = useState("");
2023-02-01 03:39:30 +01:00
const [topic, setTopic] = useState(props.topic || "");
const [everyone, setEveryone] = useState(Permission.DENY_ALL);
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const allowTopicEdit = !props.topic;
const alreadyReserved = props.reservations.filter(r => r.topic === topic).length > 0;
const submitButtonEnabled = validTopic(topic) && !alreadyReserved;
const handleSubmit = async () => {
try {
await accountApi.upsertReservation(topic, everyone);
console.debug(`[ReserveAddDialog] Added reservation for topic ${t}: ${everyone}`);
} catch (e) {
console.log(`[ReserveAddDialog] Error adding topic reservation.`, e);
2023-02-02 21:19:37 +01:00
if (e instanceof UnauthorizedError) {
2023-02-01 03:39:30 +01:00
session.resetAndRedirect(routes.login);
2023-02-02 21:19:37 +01:00
} else if (e instanceof TopicReservedError) {
setError(t("subscribe_dialog_error_topic_already_reserved"));
return;
} else {
setError(e.message);
2023-02-01 03:39:30 +01:00
return;
}
}
props.onClose();
};
return (
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}>
<DialogTitle>{t("prefs_reservations_dialog_title_add")}</DialogTitle>
<DialogContent>
<DialogContentText>
{t("prefs_reservations_dialog_description")}
</DialogContentText>
{allowTopicEdit && <TextField
autoFocus
margin="dense"
id="topic"
label={t("prefs_reservations_dialog_topic_label")}
aria-label={t("prefs_reservations_dialog_topic_label")}
value={topic}
onChange={ev => setTopic(ev.target.value)}
type="url"
fullWidth
variant="standard"
/>}
<ReserveTopicSelect
value={everyone}
onChange={setEveryone}
sx={{mt: 1}}
/>
</DialogContent>
2023-02-02 21:19:37 +01:00
<DialogFooter status={error}>
2023-02-01 03:39:30 +01:00
<Button onClick={props.onClose}>{t("prefs_users_dialog_button_cancel")}</Button>
<Button onClick={handleSubmit} disabled={!submitButtonEnabled}>{t("prefs_users_dialog_button_add")}</Button>
</DialogFooter>
</Dialog>
);
};
export const ReserveEditDialog = (props) => {
const { t } = useTranslation();
2023-02-02 21:19:37 +01:00
const [error, setError] = useState("");
2023-02-01 03:39:30 +01:00
const [everyone, setEveryone] = useState(props.reservation?.everyone || Permission.DENY_ALL);
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const handleSubmit = async () => {
try {
await accountApi.upsertReservation(props.reservation.topic, everyone);
console.debug(`[ReserveEditDialog] Updated reservation for topic ${t}: ${everyone}`);
} catch (e) {
console.log(`[ReserveEditDialog] Error updating topic reservation.`, e);
2023-02-02 21:19:37 +01:00
if (e instanceof UnauthorizedError) {
2023-02-01 03:39:30 +01:00
session.resetAndRedirect(routes.login);
2023-02-02 21:19:37 +01:00
} else {
setError(e.message);
return;
2023-02-01 03:39:30 +01:00
}
}
props.onClose();
};
return (
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}>
<DialogTitle>{t("prefs_reservations_dialog_title_edit")}</DialogTitle>
<DialogContent>
<DialogContentText>
{t("prefs_reservations_dialog_description")}
</DialogContentText>
<ReserveTopicSelect
value={everyone}
onChange={setEveryone}
sx={{mt: 1}}
/>
</DialogContent>
2023-02-02 21:19:37 +01:00
<DialogFooter status={error}>
2023-02-01 03:39:30 +01:00
<Button onClick={props.onClose}>{t("common_cancel")}</Button>
<Button onClick={handleSubmit}>{t("common_save")}</Button>
2023-02-02 21:19:37 +01:00
</DialogFooter>
2023-02-01 03:39:30 +01:00
</Dialog>
);
};
export const ReserveDeleteDialog = (props) => {
const { t } = useTranslation();
2023-02-02 21:19:37 +01:00
const [error, setError] = useState("");
2023-02-01 03:39:30 +01:00
const [deleteMessages, setDeleteMessages] = useState(false);
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const handleSubmit = async () => {
try {
await accountApi.deleteReservation(props.topic, deleteMessages);
2023-02-02 21:19:37 +01:00
console.debug(`[ReserveDeleteDialog] Deleted reservation for topic ${props.topic}`);
2023-02-01 03:39:30 +01:00
} catch (e) {
console.log(`[ReserveDeleteDialog] Error deleting topic reservation.`, e);
2023-02-02 21:19:37 +01:00
if (e instanceof UnauthorizedError) {
2023-02-01 03:39:30 +01:00
session.resetAndRedirect(routes.login);
2023-02-02 21:19:37 +01:00
} else {
setError(e.message);
return;
2023-02-01 03:39:30 +01:00
}
}
props.onClose();
};
return (
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}>
<DialogTitle>{t("prefs_reservations_dialog_title_delete")}</DialogTitle>
<DialogContent>
<DialogContentText>
{t("reservation_delete_dialog_description")}
</DialogContentText>
<FormControl fullWidth variant="standard">
<Select
value={deleteMessages}
onChange={(ev) => setDeleteMessages(ev.target.value)}
sx={{
"& .MuiSelect-select": {
display: 'flex',
alignItems: 'center',
paddingTop: "4px",
paddingBottom: "4px",
}
}}
>
<MenuItem value={false}>
<ListItemIcon><Check/></ListItemIcon>
<ListItemText primary={t("reservation_delete_dialog_action_keep_title")}/>
</MenuItem>
<MenuItem value={true}>
<ListItemIcon><DeleteForever/></ListItemIcon>
<ListItemText primary={t("reservation_delete_dialog_action_delete_title")}/>
</MenuItem>
</Select>
</FormControl>
{!deleteMessages &&
<Alert severity="info" sx={{ mt: 1 }}>
{t("reservation_delete_dialog_action_keep_description")}
</Alert>
}
{deleteMessages &&
<Alert severity="warning" sx={{ mt: 1 }}>
{t("reservation_delete_dialog_action_delete_description")}
</Alert>
}
</DialogContent>
2023-02-02 21:19:37 +01:00
<DialogFooter status={error}>
2023-02-01 03:39:30 +01:00
<Button onClick={props.onClose}>{t("common_cancel")}</Button>
<Button onClick={handleSubmit} color="error">{t("reservation_delete_dialog_submit_button")}</Button>
2023-02-02 21:19:37 +01:00
</DialogFooter>
2023-02-01 03:39:30 +01:00
</Dialog>
);
};