Merge pull request #2573 from luismsousa/feat/notifications-delete
Notification channel Delete:
This commit is contained in:
4 files changed
+51
-17
No files matched your search
@@ -65,14 +65,16 @@ const useDeleteNotification = () => {
|
||||
const [error, setError] = useState(null);
|
||||
const { t } = useTranslation();
|
||||
|
||||
const deleteNotification = async (id, triggerUpdate) => {
|
||||
const deleteNotification = async (id, callback) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await networkService.deleteNotificationById({ id });
|
||||
createToast({
|
||||
body: t("notifications.delete.success"),
|
||||
});
|
||||
triggerUpdate();
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
} catch (error) {
|
||||
setError(error);
|
||||
createToast({
|
||||
|
||||
@@ -9,20 +9,24 @@ import { useState } from "react";
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import PropTypes from "prop-types";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
const ActionMenu = ({ notification, onDelete }) => {
|
||||
const theme = useTheme();
|
||||
const navigate = useNavigate();
|
||||
const [anchorEl, setAnchorEl] = useState(null);
|
||||
const open = Boolean(anchorEl);
|
||||
|
||||
const { t } = useTranslation();
|
||||
// Handlers
|
||||
const handleClick = (event) => {
|
||||
event.stopPropagation();
|
||||
setAnchorEl(event.currentTarget);
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
const handleClose = (event) => {
|
||||
if (event) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
setAnchorEl(null);
|
||||
};
|
||||
|
||||
@@ -32,7 +36,8 @@ const ActionMenu = ({ notification, onDelete }) => {
|
||||
handleClose();
|
||||
};
|
||||
|
||||
const handleConfigure = () => {
|
||||
const handleConfigure = (e) => {
|
||||
e.stopPropagation();
|
||||
navigate(`/notifications/${notification._id}`);
|
||||
handleClose();
|
||||
};
|
||||
@@ -40,8 +45,9 @@ const ActionMenu = ({ notification, onDelete }) => {
|
||||
return (
|
||||
<>
|
||||
<IconButton
|
||||
aria-label="monitor actions"
|
||||
aria-label="notification actions"
|
||||
onClick={handleClick}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
>
|
||||
<SettingsOutlinedIcon />
|
||||
</IconButton>
|
||||
@@ -51,13 +57,15 @@ const ActionMenu = ({ notification, onDelete }) => {
|
||||
open={open}
|
||||
onClose={handleClose}
|
||||
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
>
|
||||
<MenuItem onClick={handleConfigure}>Configure</MenuItem>
|
||||
<MenuItem onClick={handleConfigure}>{t("configure")}</MenuItem>
|
||||
<MenuItem
|
||||
onClick={handleRemove}
|
||||
sx={{ "&.MuiButtonBase-root": { color: theme.palette.error.main } }}
|
||||
>
|
||||
Remove
|
||||
{t("delete")}
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
</>
|
||||
|
||||
@@ -16,11 +16,12 @@ import {
|
||||
useGetNotificationById,
|
||||
useEditNotification,
|
||||
useTestNotification,
|
||||
useDeleteNotification,
|
||||
} from "../../../Hooks/useNotifications";
|
||||
import { notificationValidation } from "../../../Validation/validation";
|
||||
import { createToast } from "../../../Utils/toastUtils";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { useParams, useNavigate } from "react-router-dom";
|
||||
import {
|
||||
NOTIFICATION_TYPES,
|
||||
TITLE_MAP,
|
||||
@@ -33,15 +34,19 @@ import {
|
||||
|
||||
const CreateNotifications = () => {
|
||||
const { notificationId } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const theme = useTheme();
|
||||
const [createNotification, isCreating, createNotificationError] =
|
||||
useCreateNotification();
|
||||
const [editNotification, isEditing, editNotificationError] = useEditNotification();
|
||||
const [testNotification, isTesting, testNotificationError] = useTestNotification();
|
||||
const [createNotification, isCreating] = useCreateNotification();
|
||||
const [editNotification, isEditing] = useEditNotification();
|
||||
const [testNotification, isTesting] = useTestNotification();
|
||||
const [deleteNotification, isDeleting] = useDeleteNotification();
|
||||
|
||||
const BREADCRUMBS = [
|
||||
{ name: "notifications", path: "/notifications" },
|
||||
{ name: "create", path: "/notifications/create" },
|
||||
{
|
||||
name: notificationId ? "edit" : "create",
|
||||
path: notificationId ? `/notifications/${notificationId}` : "/notifications/create",
|
||||
},
|
||||
];
|
||||
|
||||
// Redux state
|
||||
@@ -55,7 +60,7 @@ const CreateNotifications = () => {
|
||||
const [errors, setErrors] = useState({});
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [notificationIsLoading, getNotificationError] = useGetNotificationById(
|
||||
const [notificationIsLoading] = useGetNotificationById(
|
||||
notificationId,
|
||||
setNotification
|
||||
);
|
||||
@@ -128,6 +133,12 @@ const CreateNotifications = () => {
|
||||
testNotification(form);
|
||||
};
|
||||
|
||||
const onDelete = () => {
|
||||
if (notificationId) {
|
||||
deleteNotification(notificationId, () => navigate("/notifications"));
|
||||
}
|
||||
};
|
||||
|
||||
const type = NOTIFICATION_TYPES.find((type) => type._id === notification.type).value;
|
||||
return (
|
||||
<Stack gap={theme.spacing(10)}>
|
||||
@@ -209,15 +220,25 @@ const CreateNotifications = () => {
|
||||
color="secondary"
|
||||
onClick={onTestNotification}
|
||||
>
|
||||
Test notification
|
||||
{t("testNotification")}
|
||||
</Button>
|
||||
{notificationId && (
|
||||
<Button
|
||||
loading={isDeleting}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={onDelete}
|
||||
>
|
||||
{t("delete")}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
loading={isCreating || isEditing || notificationIsLoading}
|
||||
type="submit"
|
||||
variant="contained"
|
||||
color="accent"
|
||||
>
|
||||
Submit
|
||||
{t("submit")}
|
||||
</Button>
|
||||
</Stack>
|
||||
</Stack>
|
||||
|
||||
@@ -62,6 +62,9 @@ const Notifications = () => {
|
||||
{
|
||||
id: "actions",
|
||||
content: "Actions",
|
||||
onClick: (e) => {
|
||||
e.stopPropagation();
|
||||
},
|
||||
render: (row) => {
|
||||
return (
|
||||
<ActionMenu
|
||||
|
||||
Reference in new issue
Block a user