fixing some state management issues

This commit is contained in:
Hari Krishna
2024-06-18 16:10:56 +00:00
committed by satti-hari-krishna-reddy
parent bf745db0fb
commit 714251c07d
+32 -35
View File
@@ -11,52 +11,49 @@ 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",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Accept: "application/json", Accept: "application/json",
}, },
body:text, body: text,
credentials: "include", credentials: "include",
}) })
.then((response) => { .then((response) => {
if (response.status !== 200) { if (response.status !== 200) {
console.log("Can't update file"); console.log("Can't update file");
} }
return response.json(); return response.json();
}) })
.then((responseJson) => { .then((responseJson) => {
if (responseJson.success === true) { if (responseJson.success === true) {
toast("Successfully updated file"); toast("Successfully updated file");
} }
}) })
.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>
@@ -84,19 +81,19 @@ const RuleCard = ({ ruleName, description, file_id, globalUrl, folderDisabled, .
</Typography> </Typography>
<ShuffleCodeEditor <ShuffleCodeEditor
isCloud={isCloud} isCloud={isCloud}
expansionModalOpen={openCodeEditor} expansionModalOpen={openCodeEditor}
setExpansionModalOpen={setOpenCodeEditor} setExpansionModalOpen={setOpenCodeEditor}
setcodedata = {setFileData} setcodedata={setFileData}
codedata={fileData} codedata={fileData}
isFileEditor = {true} isFileEditor={true}
key = {fileData} //https://reactjs.org/docs/reconciliation.html#recursing-on-children key={fileData} // https://reactjs.org/docs/reconciliation.html#recursing-on-children
runUpdateText = {UpdateText} runUpdateText={UpdateText}
/> />
</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";