Merge pull request #2563 from bluewave-labs/fix/2471-infrastructure-action-menu
Fix/2471 infrastructure action menu
This commit is contained in:
2 files changed
+71
-11
No files matched your search
@@ -9,6 +9,8 @@ import Settings from "../../../../../assets/icons/settings-bold.svg?react";
|
|||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import Dialog from "../../../../../Components/Dialog";
|
import Dialog from "../../../../../Components/Dialog";
|
||||||
import { networkService } from "../../../../../Utils/NetworkService.js";
|
import { networkService } from "../../../../../Utils/NetworkService.js";
|
||||||
|
import { usePauseMonitor } from "../../../../../Hooks/monitorHooks";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* InfrastructureMenu Component
|
* InfrastructureMenu Component
|
||||||
@@ -19,7 +21,7 @@ import { networkService } from "../../../../../Utils/NetworkService.js";
|
|||||||
* @param {string} props.monitor.id - Unique ID of the monitor.
|
* @param {string} props.monitor.id - Unique ID of the monitor.
|
||||||
* @param {string} [props.monitor.url] - URL associated with the monitor.
|
* @param {string} [props.monitor.url] - URL associated with the monitor.
|
||||||
* @param {string} props.monitor.type - Type of monitor (e.g., uptime, infrastructure).
|
* @param {string} props.monitor.type - Type of monitor (e.g., uptime, infrastructure).
|
||||||
* @param {boolean} props.monitor.isActive - Indicates if the monitor is currently active.
|
* @param {boolean} props.monitor.isActive - Indicates if the monitor is currently active (true) or paused (false).
|
||||||
* @param {boolean} props.isAdmin - Whether the user has admin privileges.
|
* @param {boolean} props.isAdmin - Whether the user has admin privileges.
|
||||||
* @param {Function} props.updateCallback - Callback to trigger when the monitor data is updated.
|
* @param {Function} props.updateCallback - Callback to trigger when the monitor data is updated.
|
||||||
* @returns {JSX.Element} The rendered component.
|
* @returns {JSX.Element} The rendered component.
|
||||||
@@ -29,6 +31,8 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const [pauseMonitor] = usePauseMonitor();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const openMenu = (e) => {
|
const openMenu = (e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
@@ -54,14 +58,26 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
navigate(`/infrastructure/${id}`);
|
navigate(`/infrastructure/${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const openConfigure = (id) => {
|
||||||
|
navigate(`/infrastructure/configure/${id}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePause = async () => {
|
||||||
|
// Pass updateCallback as triggerUpdate to the hook
|
||||||
|
await pauseMonitor({ monitorId: monitor.id, triggerUpdate: updateCallback });
|
||||||
|
// Toast is already displayed in the hook, no need to display it again
|
||||||
|
};
|
||||||
|
|
||||||
const handleRemove = async () => {
|
const handleRemove = async () => {
|
||||||
try {
|
try {
|
||||||
await networkService.deleteMonitorById({
|
await networkService.deleteMonitorById({
|
||||||
monitorId: monitor.id,
|
monitorId: monitor.id,
|
||||||
});
|
});
|
||||||
createToast({ body: "Monitor deleted successfully." });
|
createToast({
|
||||||
|
body: t("monitorActions.deleteSuccess"),
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({ body: "Failed to delete monitor." });
|
createToast({ body: t("monitorActions.deleteFailed") });
|
||||||
} finally {
|
} finally {
|
||||||
setIsDialogOpen(false);
|
setIsDialogOpen(false);
|
||||||
updateCallback();
|
updateCallback();
|
||||||
@@ -105,15 +121,53 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{isAdmin && <MenuItem onClick={openRemove}>Remove</MenuItem>}
|
<MenuItem
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
openDetails(monitor.id);
|
||||||
|
closeMenu(e);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("monitorActions.details")}
|
||||||
|
</MenuItem>
|
||||||
|
{isAdmin && (
|
||||||
|
<MenuItem
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
openConfigure(monitor.id);
|
||||||
|
closeMenu(e);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("configure")}
|
||||||
|
</MenuItem>
|
||||||
|
)}
|
||||||
|
{isAdmin && (
|
||||||
|
<MenuItem
|
||||||
|
onClick={async (e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
await handlePause();
|
||||||
|
closeMenu(e);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{!monitor.isActive ? t("resume") : t("pause")}
|
||||||
|
</MenuItem>
|
||||||
|
)}
|
||||||
|
{isAdmin && (
|
||||||
|
<MenuItem
|
||||||
|
onClick={openRemove}
|
||||||
|
sx={{ color: theme.palette.error.main }}
|
||||||
|
>
|
||||||
|
{t("remove")}
|
||||||
|
</MenuItem>
|
||||||
|
)}
|
||||||
</Menu>
|
</Menu>
|
||||||
<Dialog
|
<Dialog
|
||||||
open={isDialogOpen}
|
open={isDialogOpen}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
title="Do you really want to delete this monitor?"
|
title={t("deleteDialogTitle")}
|
||||||
description="Once deleted, this monitor cannot be retrieved."
|
description={t("deleteDialogDescription")}
|
||||||
onCancel={cancelRemove}
|
onCancel={cancelRemove}
|
||||||
confirmationButtonLabel="Delete"
|
confirmationButtonLabel={t("delete")}
|
||||||
onConfirm={handleRemove}
|
onConfirm={handleRemove}
|
||||||
modelTitle="modal-delete-monitor"
|
modelTitle="modal-delete-monitor"
|
||||||
modelDescription="delete-monitor-confirmation"
|
modelDescription="delete-monitor-confirmation"
|
||||||
@@ -124,13 +178,16 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
|
|
||||||
InfrastructureMenu.propTypes = {
|
InfrastructureMenu.propTypes = {
|
||||||
monitor: PropTypes.shape({
|
monitor: PropTypes.shape({
|
||||||
id: PropTypes.string,
|
id: PropTypes.string.isRequired,
|
||||||
url: PropTypes.string,
|
url: PropTypes.string,
|
||||||
|
// Note: type must remain optional. Making it required (type: PropTypes.string.isRequired)
|
||||||
|
// causes runtime errors as some monitors don't have a defined type property
|
||||||
type: PropTypes.string,
|
type: PropTypes.string,
|
||||||
isActive: PropTypes.bool,
|
isActive: PropTypes.bool, // Determines whether the monitor is paused (false) or active (true)
|
||||||
|
status: PropTypes.string, // Represents the monitor's operational status (e.g., 'up', 'down', etc.)
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
isAdmin: PropTypes.bool,
|
isAdmin: PropTypes.bool.isRequired,
|
||||||
updateCallback: PropTypes.func,
|
updateCallback: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export { InfrastructureMenu };
|
export { InfrastructureMenu };
|
||||||
@@ -490,6 +490,9 @@
|
|||||||
"message": "Message",
|
"message": "Message",
|
||||||
"monitor": "monitor",
|
"monitor": "monitor",
|
||||||
"monitorActions": {
|
"monitorActions": {
|
||||||
|
"deleteSuccess": "Monitor deleted successfully",
|
||||||
|
"deleteFailed": "Failed to delete monitor",
|
||||||
|
"details": "Details",
|
||||||
"export": "Export Monitors",
|
"export": "Export Monitors",
|
||||||
"import": "Import Monitors",
|
"import": "Import Monitors",
|
||||||
"title": "Export/Import"
|
"title": "Export/Import"
|
||||||
|
|||||||
Reference in new issue
Block a user