fixing some state management issues
This commit is contained in:
committed by
satti-hari-krishna-reddy
parent
bf745db0fb
commit
714251c07d
@@ -11,27 +11,23 @@ import { toast } from "react-toastify";
|
||||
import ShuffleCodeEditor from "../components/ShuffleCodeEditor1.jsx";
|
||||
|
||||
const RuleCard = ({ ruleName, description, file_id, globalUrl, folderDisabled, ...otherProps }) => {
|
||||
const [additionalProps, setAdditionalProps] = React.useState(otherProps);
|
||||
const [openCodeEditor, setOpenCodeEditor] = React.useState(false);
|
||||
const [fileData, setFileData] = React.useState("");
|
||||
const [isEnabled, setIsEnabled] = React.useState(otherProps.is_enabled);
|
||||
|
||||
const isCloud =
|
||||
window.location.host === "localhost:3002" ||
|
||||
window.location.host === "shuffler.io";
|
||||
const isCloud = ["localhost:3002", "shuffler.io"].includes(window.location.host);
|
||||
|
||||
const handleSwitchChange = (event) => {
|
||||
if (folderDisabled) {
|
||||
toast("enable the directory to enable individual rules");
|
||||
return;
|
||||
}
|
||||
const isEnabled = event.target.checked;
|
||||
toggleRule(file_id, !isEnabled, globalUrl, () => {
|
||||
setAdditionalProps((prevProps) => ({
|
||||
...prevProps,
|
||||
is_enabled: isEnabled,
|
||||
}));
|
||||
const newIsEnabled = event.target.checked;
|
||||
toggleRule(file_id, !newIsEnabled, globalUrl, () => {
|
||||
setIsEnabled(newIsEnabled);
|
||||
});
|
||||
};
|
||||
|
||||
const UpdateText = (text) => {
|
||||
fetch(`${globalUrl}/api/v1/files/${file_id}/edit`, {
|
||||
method: "PUT",
|
||||
@@ -55,8 +51,9 @@ const RuleCard = ({ ruleName, description, file_id, globalUrl, folderDisabled, .
|
||||
})
|
||||
.catch((error) => {
|
||||
toast("Error updating file: " + error.toString());
|
||||
})
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Card variant="outlined" sx={{ mb: 2 }}>
|
||||
<CardContent>
|
||||
@@ -74,7 +71,7 @@ const RuleCard = ({ ruleName, description, file_id, globalUrl, folderDisabled, .
|
||||
<EditIcon />
|
||||
</IconButton>
|
||||
<Switch
|
||||
checked={additionalProps.is_enabled && !folderDisabled}
|
||||
checked={isEnabled && !folderDisabled}
|
||||
onChange={handleSwitchChange}
|
||||
/>
|
||||
</div>
|
||||
@@ -96,7 +93,7 @@ const RuleCard = ({ ruleName, description, file_id, globalUrl, folderDisabled, .
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
const toggleRule = (fileId, isCurrentlyEnabled, globalUrl, callback) => {
|
||||
const action = isCurrentlyEnabled ? "disable" : "enable";
|
||||
|
||||
Reference in New Issue
Block a user