| 1 |
import * as React from "react"; |
| 2 |
import { useState } from "react"; |
| 3 |
import { |
| 4 |
Button, |
| 5 |
TextField, |
| 6 |
Dialog, |
| 7 |
DialogContent, |
| 8 |
DialogContentText, |
| 9 |
DialogTitle, |
| 10 |
Alert, |
| 11 |
FormControl, |
| 12 |
Select, |
| 13 |
useMediaQuery, |
| 14 |
MenuItem, |
| 15 |
ListItemIcon, |
| 16 |
ListItemText, |
| 17 |
useTheme, |
| 18 |
} from "@mui/material"; |
| 19 |
import { useTranslation } from "react-i18next"; |
| 20 |
import Check from "@mui/icons-material/Check"; |
| 21 |
import DeleteForever from "@mui/icons-material/DeleteForever"; |
| 22 |
import { validTopic } from "../app/utils"; |
| 23 |
import DialogFooter from "./DialogFooter"; |
| 24 |
import session from "../app/Session"; |
| 25 |
import routes from "./routes"; |
| 26 |
import accountApi, { Permission } from "../app/AccountApi"; |
| 27 |
import ReserveTopicSelect from "./ReserveTopicSelect"; |
| 28 |
import { TopicReservedError, UnauthorizedError } from "../app/errors"; |
| 29 |
|
| 30 |
export const ReserveAddDialog = (props) => { |
| 31 |
const theme = useTheme(); |
| 32 |
const { t } = useTranslation(); |
| 33 |
const [error, setError] = useState(""); |
| 34 |
const [topic, setTopic] = useState(props.topic || ""); |
| 35 |
const [everyone, setEveryone] = useState(Permission.DENY_ALL); |
| 36 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 37 |
const allowTopicEdit = !props.topic; |
| 38 |
const alreadyReserved = props.reservations.filter((r) => r.topic === topic).length > 0; |
| 39 |
const submitButtonEnabled = validTopic(topic) && !alreadyReserved; |
| 40 |
|
| 41 |
const handleSubmit = async () => { |
| 42 |
try { |
| 43 |
await accountApi.upsertReservation(topic, everyone); |
| 44 |
console.debug(`[ReserveAddDialog] Added reservation for topic ${topic}: ${everyone}`); |
| 45 |
} catch (e) { |
| 46 |
console.log(`[ReserveAddDialog] Error adding topic reservation.`, e); |
| 47 |
if (e instanceof UnauthorizedError) { |
| 48 |
await session.resetAndRedirect(routes.login); |
| 49 |
} else if (e instanceof TopicReservedError) { |
| 50 |
setError(t("subscribe_dialog_error_topic_already_reserved")); |
| 51 |
return; |
| 52 |
} else { |
| 53 |
setError(e.message); |
| 54 |
return; |
| 55 |
} |
| 56 |
} |
| 57 |
props.onClose(); |
| 58 |
}; |
| 59 |
|
| 60 |
return ( |
| 61 |
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}> |
| 62 |
<DialogTitle>{t("prefs_reservations_dialog_title_add")}</DialogTitle> |
| 63 |
<DialogContent> |
| 64 |
<DialogContentText>{t("prefs_reservations_dialog_description")}</DialogContentText> |
| 65 |
{allowTopicEdit && ( |
| 66 |
<TextField |
| 67 |
autoFocus |
| 68 |
margin="dense" |
| 69 |
id="topic" |
| 70 |
label={t("prefs_reservations_dialog_topic_label")} |
| 71 |
aria-label={t("prefs_reservations_dialog_topic_label")} |
| 72 |
value={topic} |
| 73 |
onChange={(ev) => setTopic(ev.target.value)} |
| 74 |
type="url" |
| 75 |
fullWidth |
| 76 |
variant="standard" |
| 77 |
/> |
| 78 |
)} |
| 79 |
<ReserveTopicSelect value={everyone} onChange={setEveryone} sx={{ mt: 1 }} /> |
| 80 |
</DialogContent> |
| 81 |
<DialogFooter status={error}> |
| 82 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 83 |
<Button onClick={handleSubmit} disabled={!submitButtonEnabled}> |
| 84 |
{t("common_add")} |
| 85 |
</Button> |
| 86 |
</DialogFooter> |
| 87 |
</Dialog> |
| 88 |
); |
| 89 |
}; |
| 90 |
|
| 91 |
export const ReserveEditDialog = (props) => { |
| 92 |
const theme = useTheme(); |
| 93 |
const { t } = useTranslation(); |
| 94 |
const [error, setError] = useState(""); |
| 95 |
const [everyone, setEveryone] = useState(props.reservation?.everyone || Permission.DENY_ALL); |
| 96 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 97 |
|
| 98 |
const handleSubmit = async () => { |
| 99 |
try { |
| 100 |
await accountApi.upsertReservation(props.reservation.topic, everyone); |
| 101 |
console.debug(`[ReserveEditDialog] Updated reservation for topic ${t}: ${everyone}`); |
| 102 |
} catch (e) { |
| 103 |
console.log(`[ReserveEditDialog] Error updating topic reservation.`, e); |
| 104 |
if (e instanceof UnauthorizedError) { |
| 105 |
await session.resetAndRedirect(routes.login); |
| 106 |
} else { |
| 107 |
setError(e.message); |
| 108 |
return; |
| 109 |
} |
| 110 |
} |
| 111 |
props.onClose(); |
| 112 |
}; |
| 113 |
|
| 114 |
return ( |
| 115 |
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}> |
| 116 |
<DialogTitle>{t("prefs_reservations_dialog_title_edit")}</DialogTitle> |
| 117 |
<DialogContent> |
| 118 |
<DialogContentText>{t("prefs_reservations_dialog_description")}</DialogContentText> |
| 119 |
<ReserveTopicSelect value={everyone} onChange={setEveryone} sx={{ mt: 1 }} /> |
| 120 |
</DialogContent> |
| 121 |
<DialogFooter status={error}> |
| 122 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 123 |
<Button onClick={handleSubmit}>{t("common_save")}</Button> |
| 124 |
</DialogFooter> |
| 125 |
</Dialog> |
| 126 |
); |
| 127 |
}; |
| 128 |
|
| 129 |
export const ReserveDeleteDialog = (props) => { |
| 130 |
const theme = useTheme(); |
| 131 |
const { t } = useTranslation(); |
| 132 |
const [error, setError] = useState(""); |
| 133 |
const [deleteMessages, setDeleteMessages] = useState(false); |
| 134 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 135 |
|
| 136 |
const handleSubmit = async () => { |
| 137 |
try { |
| 138 |
await accountApi.deleteReservation(props.topic, deleteMessages); |
| 139 |
console.debug(`[ReserveDeleteDialog] Deleted reservation for topic ${props.topic}`); |
| 140 |
} catch (e) { |
| 141 |
console.log(`[ReserveDeleteDialog] Error deleting topic reservation.`, e); |
| 142 |
if (e instanceof UnauthorizedError) { |
| 143 |
await session.resetAndRedirect(routes.login); |
| 144 |
} else { |
| 145 |
setError(e.message); |
| 146 |
return; |
| 147 |
} |
| 148 |
} |
| 149 |
props.onClose(); |
| 150 |
}; |
| 151 |
|
| 152 |
return ( |
| 153 |
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}> |
| 154 |
<DialogTitle>{t("prefs_reservations_dialog_title_delete")}</DialogTitle> |
| 155 |
<DialogContent> |
| 156 |
<DialogContentText>{t("reservation_delete_dialog_description")}</DialogContentText> |
| 157 |
<FormControl fullWidth variant="standard"> |
| 158 |
<Select |
| 159 |
value={deleteMessages} |
| 160 |
onChange={(ev) => setDeleteMessages(ev.target.value)} |
| 161 |
sx={{ |
| 162 |
"& .MuiSelect-select": { |
| 163 |
display: "flex", |
| 164 |
alignItems: "center", |
| 165 |
paddingTop: "4px", |
| 166 |
paddingBottom: "4px", |
| 167 |
}, |
| 168 |
}} |
| 169 |
> |
| 170 |
<MenuItem value={false}> |
| 171 |
<ListItemIcon> |
| 172 |
<Check /> |
| 173 |
</ListItemIcon> |
| 174 |
<ListItemText primary={t("reservation_delete_dialog_action_keep_title")} /> |
| 175 |
</MenuItem> |
| 176 |
<MenuItem value> |
| 177 |
<ListItemIcon> |
| 178 |
<DeleteForever /> |
| 179 |
</ListItemIcon> |
| 180 |
<ListItemText primary={t("reservation_delete_dialog_action_delete_title")} /> |
| 181 |
</MenuItem> |
| 182 |
</Select> |
| 183 |
</FormControl> |
| 184 |
{!deleteMessages && ( |
| 185 |
<Alert severity="info" sx={{ mt: 1 }}> |
| 186 |
{t("reservation_delete_dialog_action_keep_description")} |
| 187 |
</Alert> |
| 188 |
)} |
| 189 |
{deleteMessages && ( |
| 190 |
<Alert severity="warning" sx={{ mt: 1 }}> |
| 191 |
{t("reservation_delete_dialog_action_delete_description")} |
| 192 |
</Alert> |
| 193 |
)} |
| 194 |
</DialogContent> |
| 195 |
<DialogFooter status={error}> |
| 196 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 197 |
<Button onClick={handleSubmit} color="error"> |
| 198 |
{t("reservation_delete_dialog_submit_button")} |
| 199 |
</Button> |
| 200 |
</DialogFooter> |
| 201 |
</Dialog> |
| 202 |
); |
| 203 |
}; |