added toggle rule function to enable or disable the rule

This commit is contained in:
Hari Krishna
2024-06-12 04:48:34 +00:00
committed by satti-hari-krishna-reddy
parent e1d0857fd7
commit c9da78c781
+87 -62
View File
@@ -19,86 +19,109 @@ const ConnectedButton = styled(Button)({
color: "white", color: "white",
}); });
const disableRule = (fileId) => { const RuleCard = ({ ruleName, description, file_id, globalUrl, ...otherProps }) => {
}
const RuleCard = ({ ruleName, description, ...otherProps }) => {
const [additionalProps, setAdditionalProps] = React.useState(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,
}));
});
};
return ( return (
<Card variant="outlined" sx={{ mb: 2 }}> <Card variant="outlined" sx={{ mb: 2 }}>
<CardContent> <CardContent>
<div <div
style={{ style={{
display: "flex", display: 'flex',
justifyContent: "space-between", justifyContent: 'space-between',
alignItems: "center", alignItems: 'center',
mb: 2, marginBottom: 16,
}} }}
> >
<Typography variant="h6">{ruleName}</Typography> <Typography variant="h6">{ruleName}</Typography>
<div style={{ display: "flex", alignItems: "center" }}> <div style={{ display: 'flex', alignItems: 'center' }}>
<IconButton> <IconButton>
<EditIcon /> <EditIcon />
</IconButton> </IconButton>
<Switch <Switch
checked={additionalProps.is_enabled} checked={additionalProps.is_enabled}
disabled={!additionalProps.is_enabled} onChange={handleSwitchChange}
/> />
</div>
</div> </div>
</div> <Typography variant="body2" style={{ marginTop: '2%' }}>
<Typography variant="body2" style={{ marginTop: "2%" }}> {description}
{description} </Typography>
</Typography> </CardContent>
<Box </Card>
sx={{ );
display: "flex", };
justifyContent: "space-between",
alignItems: "center", const toggleRule = (fileId, isCurrentlyEnabled, globalUrl, callback) => {
mt: 2, const action = isCurrentlyEnabled ? "disable" : "enable";
}} const url = `${globalUrl}/api/v1/files/${fileId}/${action}_rule`;
>
<Box sx={{ display: "flex", gap: 1 }}> fetch(url, {
{/* we need icons here ??? */} method: "PUT",
</Box> credentials: "include",
</Box> headers: {
</CardContent> "Content-Type": "application/json",
</Card>) },
} })
.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 Detection = (props) => {
const {globalUrl} = props; const { globalUrl } = props;
const [ruleInfo, setRuleInfo] = React.useState([]); const [ruleInfo, setRuleInfo] = React.useState([]);
const getSigmaInfo = () => { const getSigmaInfo = () => {
const url = globalUrl + "/api/v1/files/detection/sigma_rules" const url = globalUrl + "/api/v1/files/detection/sigma_rules";
fetch(url, { fetch(url, {
method: "GET", method: "GET",
credentials: "include", credentials: "include",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}) .then((response) => })
response.json().then((responseJson) => { .then((response) =>
if (responseJson["success"] === false) { response.json().then((responseJson) => {
toast("Failed to get sigma rules"); if (responseJson["success"] === false) {
} else { toast("Failed to get sigma rules");
} else {
setRuleInfo(responseJson); setRuleInfo(responseJson);
} }
})
}), )
) .catch((error) => {
.catch((error) => { console.log("Error in getting sigma files: ", error);
console.log("Error in geting sigma files: ", error); toast("An error occurred while fetching sigma rules");
}); });
} };
React.useEffect(() => { React.useEffect(() => {
getSigmaInfo() getSigmaInfo();
}, []); }, []);
return ( return (
<Container sx={{ mt: 4 }}> <Container sx={{ mt: 4 }}>
<Box sx={{ border: "1px solid #ccc", borderRadius: 2, p: 3 }}> <Box sx={{ border: "1px solid #ccc", borderRadius: 2, p: 3 }}>
@@ -136,9 +159,11 @@ const Detection = (props) => {
{ruleInfo.length > 0 && {ruleInfo.length > 0 &&
ruleInfo.map((card) => ( ruleInfo.map((card) => (
<RuleCard <RuleCard
key={card.title} key={card.file_id}
ruleName={card.title} ruleName={card.title}
description={card.description} description={card.description}
file_id={card.file_id}
globalUrl={globalUrl}
{...card} {...card}
/> />
))} ))}