feat(frontend): add support for updating node template on code change in CodeAreaModal

fix(API): fix UpdateTemplate function return type to match the actual response
fix(vite.config.ts): add dynamic_node route to apiRoutes array to proxy requests to backend
This commit is contained in:
anovazzi1 2023-06-22 17:30:04 -03:00
commit a47dc9ae92
7 changed files with 86 additions and 46 deletions

View file

@ -8,7 +8,7 @@ import "ace-builds/src-noconflict/theme-twilight";
import "ace-builds/src-noconflict/ext-language_tools";
// import "ace-builds/webpack-resolver";
import { darkContext } from "../../contexts/darkContext";
import { postValidateCode } from "../../controllers/API";
import { UpdateTemplate, postValidateCode } from "../../controllers/API";
import { alertContext } from "../../contexts/alertContext";
import { TabsContext } from "../../contexts/tabsContext";
import {
@ -22,16 +22,23 @@ import {
} from "../../components/ui/dialog";
import { Button } from "../../components/ui/button";
import { CODE_PROMPT_DIALOG_SUBTITLE } from "../../constants";
import Loading from "../../components/ui/loading";
import { APITemplateType } from "../../types/api";
export default function CodeAreaModal({
value,
setValue,
template,
setTemplate
}: {
setValue: (value: string) => void;
value: string;
template: APITemplateType,
setTemplate: (template: APITemplateType) => void;
}) {
const [open, setOpen] = useState(true);
const [code, setCode] = useState(value);
const [loading, setLoading] = useState(false);
const { dark } = useContext(darkContext);
const { setErrorData, setSuccessData } = useContext(alertContext);
const { closePopUp } = useContext(PopUpContext);
@ -44,6 +51,58 @@ export default function CodeAreaModal({
}, 300);
}
}
function handleClick() {
setLoading(true);
postValidateCode(code)
.then((apiReturn) => {
setLoading(false);
if (apiReturn.data) {
let importsErrors = apiReturn.data.imports.errors;
let funcErrors = apiReturn.data.function.errors;
if (funcErrors.length === 0 && importsErrors.length === 0) {
setSuccessData({
title: "Code is ready to run",
});
// setValue(code);
} else {
if (funcErrors.length !== 0) {
setErrorData({
title: "There is an error in your function",
list: funcErrors,
});
}
if (importsErrors.length !== 0) {
setErrorData({
title: "There is an error in your imports",
list: importsErrors,
});
}
}
} else {
setErrorData({
title: "Something went wrong, please try again",
});
}
})
.catch((_) => {
setLoading(false);
setErrorData({
title:
"There is something wrong with this code, please review it",
})
}
);
UpdateTemplate('code',template).then((apiReturn) => {
const data = apiReturn.data;
if (data.template) {
console.log('updated')
setTemplate(data.template);
setModalOpen(false);
}
})
}
return (
<Dialog open={true} onOpenChange={setModalOpen}>
<DialogTrigger></DialogTrigger>
@ -80,47 +139,10 @@ export default function CodeAreaModal({
<DialogFooter>
<Button
className="mt-3"
onClick={() => {
postValidateCode(code)
.then((apiReturn) => {
if (apiReturn.data) {
let importsErrors = apiReturn.data.imports.errors;
let funcErrors = apiReturn.data.function.errors;
if (funcErrors.length === 0 && importsErrors.length === 0) {
setSuccessData({
title: "Code is ready to run",
});
setModalOpen(false);
setValue(code);
} else {
if (funcErrors.length !== 0) {
setErrorData({
title: "There is an error in your function",
list: funcErrors,
});
}
if (importsErrors.length !== 0) {
setErrorData({
title: "There is an error in your imports",
list: importsErrors,
});
}
}
} else {
setErrorData({
title: "Something went wrong, please try again",
});
}
})
.catch((_) =>
setErrorData({
title:
"There is something wrong with this code, please review it",
})
);
}}
onClick={handleClick}
type="submit"
>
{/* {loading?(<Loading/>):'Check & Save'} */}
Check & Save
</Button>
</DialogFooter>