From b12abbe8785eeec12c8fd4e64af1df57eb6e1801 Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Wed, 12 Jun 2024 04:48:34 +0000 Subject: [PATCH] added toggle rule function to enable or disable the rule --- frontend/src/views/Detection.jsx | 147 ++++++++++++++++++------------- 1 file changed, 86 insertions(+), 61 deletions(-) diff --git a/frontend/src/views/Detection.jsx b/frontend/src/views/Detection.jsx index e542c5f9b..79cb71b7d 100644 --- a/frontend/src/views/Detection.jsx +++ b/frontend/src/views/Detection.jsx @@ -19,86 +19,109 @@ const ConnectedButton = styled(Button)({ color: "white", }); -const disableRule = (fileId) => { - -} +const RuleCard = ({ ruleName, description, file_id, globalUrl, ...otherProps }) => { + const [additionalProps, setAdditionalProps] = React.useState(otherProps); + const handleSwitchChange = (event) => { + const isEnabled = event.target.checked; + toggleRule(file_id, !isEnabled, globalUrl, () => { + setAdditionalProps((prevProps) => ({ + ...prevProps, + is_enabled: isEnabled, + })); + }); + }; -const RuleCard = ({ ruleName, description, ...otherProps }) => { - const [additionalProps, setAdditionalProps] = React.useState(otherProps); return ( - - -
- {ruleName} -
- - - - + +
+ {ruleName} +
+ + + + +
-
- - {description} - - - - {/* we need icons here ??? */} - - - - ) -} + + {description} + + + + ); +}; + +const toggleRule = (fileId, isCurrentlyEnabled, globalUrl, callback) => { + const action = isCurrentlyEnabled ? "disable" : "enable"; + const url = `${globalUrl}/api/v1/files/${fileId}/${action}_rule`; + + fetch(url, { + method: "PUT", + credentials: "include", + headers: { + "Content-Type": "application/json", + }, + }) + .then((response) => + response.json().then((responseJson) => { + if (responseJson["success"] === false) { + toast(`Failed to ${action} the rule`); + } else { + toast(`Rule ${action}d successfully`); + callback(); + } + }) + ) + .catch((error) => { + console.log(`Error in ${action}ing the rule: `, error); + toast(`An error occurred while ${action}ing the rule`); + }); +}; const Detection = (props) => { - const {globalUrl} = props; + const { globalUrl } = props; const [ruleInfo, setRuleInfo] = React.useState([]); const getSigmaInfo = () => { - const url = globalUrl + "/api/v1/files/detection/sigma_rules" - + const url = globalUrl + "/api/v1/files/detection/sigma_rules"; + fetch(url, { method: "GET", credentials: "include", headers: { "Content-Type": "application/json", }, - }) .then((response) => - response.json().then((responseJson) => { - if (responseJson["success"] === false) { - toast("Failed to get sigma rules"); - } else { + }) + .then((response) => + response.json().then((responseJson) => { + if (responseJson["success"] === false) { + toast("Failed to get sigma rules"); + } else { setRuleInfo(responseJson); - } - - }), - ) - .catch((error) => { - console.log("Error in geting sigma files: ", error); - }); - } + } + }) + ) + .catch((error) => { + console.log("Error in getting sigma files: ", error); + toast("An error occurred while fetching sigma rules"); + }); + }; React.useEffect(() => { - getSigmaInfo() -}, []); - + getSigmaInfo(); + }, []); + return ( @@ -136,9 +159,11 @@ const Detection = (props) => { {ruleInfo.length > 0 && ruleInfo.map((card) => ( ))}