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