Skip to content

Commit

Permalink
refactor: component code validation (#3470)
Browse files Browse the repository at this point in the history
* removed post custom component update

* Added post validade component code mutation

* Used post validade code mutation on component code

* removed unused console log

* used validate component code on generic node code validation
  • Loading branch information
lucaseduoli authored and ogabrielluiz committed Aug 27, 2024
1 parent 16b8b43 commit f0ca3c8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export default function NodeStatus({
};

useEffect(() => {
console.log(selected);
setBorderColor(
getNodeBorderClassName(selected, showNode, buildStatus, validationStatus),
);
Expand Down
43 changes: 24 additions & 19 deletions src/frontend/src/CustomNodes/GenericNode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { usePostValidateComponentCode } from "@/controllers/API/queries/nodes/use-post-validate-component-code";
import { useEffect, useMemo, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";
import { NodeToolbar, useUpdateNodeInternals } from "reactflow";
Expand All @@ -7,7 +8,6 @@ import IconComponent, {
import ShadTooltip from "../../components/shadTooltipComponent";
import { Button } from "../../components/ui/button";
import { TOOLTIP_OUTDATED_NODE } from "../../constants/constants";
import { postCustomComponent } from "../../controllers/API";
import NodeToolbarComponent from "../../pages/FlowPage/components/nodeToolbarComponent";
import useAlertStore from "../../stores/alertStore";
import useFlowStore from "../../stores/flowStore";
Expand Down Expand Up @@ -86,6 +86,8 @@ export default function GenericNode({

const [showHiddenOutputs, setShowHiddenOutputs] = useState(false);

const { mutate: validateComponentCode } = usePostValidateComponentCode();

const handleUpdateCode = () => {
setLoadingUpdate(true);
takeSnapshot();
Expand All @@ -97,25 +99,28 @@ export default function GenericNode({

const currentCode = thisNodeTemplate.code.value;
if (data.node) {
postCustomComponent(currentCode, data.node)
.then((apiReturn) => {
const { data, type } = apiReturn.data;
if (data && type && updateNodeCode) {
updateNodeCode(data, currentCode, "code", type);
validateComponentCode(
{ code: currentCode, frontend_node: data.node },
{
onSuccess: ({ data, type }) => {
if (data && type && updateNodeCode) {
updateNodeCode(data, currentCode, "code", type);
setLoadingUpdate(false);
}
},
onError: (error) => {
setErrorData({
title: "Error updating Compoenent code",
list: [
"There was an error updating the Component.",
"If the error persists, please report it on our Discord or GitHub.",
],
});
console.log(error);
setLoadingUpdate(false);
}
})
.catch((err) => {
setErrorData({
title: "Error updating Compoenent code",
list: [
"There was an error updating the Component.",
"If the error persists, please report it on our Discord or GitHub.",
],
});
setLoadingUpdate(false);
console.log(err);
});
},
},
);
}
};

Expand Down
15 changes: 0 additions & 15 deletions src/frontend/src/controllers/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BASE_URL_API } from "../../constants/constants";
import { api } from "../../controllers/API/api";
import {
APIObjectType,
APITemplateType,
Component,
CustomComponentRequest,
Users,
Expand Down Expand Up @@ -291,20 +290,6 @@ export async function postCustomComponent(
});
}

export async function postCustomComponentUpdate(
code: string,
template: APITemplateType,
field: string,
field_value: any,
): Promise<AxiosResponse<APIClassType>> {
return await api.post(`${BASE_URL_API}custom_component/update`, {
code,
template,
field,
field_value,
});
}

export async function renewAccessToken() {
try {
return await api.post(`${BASE_URL_API}refresh`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
APIClassType,
CustomComponentRequest,
ResponseErrorTypeAPI,
useMutationFunctionType,
} from "@/types/api";
import { UseMutationResult } from "@tanstack/react-query";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface IPostValidateComponentCode {
code: string;
frontend_node: APIClassType;
}

export const usePostValidateComponentCode: useMutationFunctionType<
undefined,
IPostValidateComponentCode,
CustomComponentRequest,
ResponseErrorTypeAPI
> = (options?) => {
const { mutate } = UseRequestProcessor();

const postValidateComponentCodeFn = async (
payload: IPostValidateComponentCode,
): Promise<CustomComponentRequest> => {
const response = await api.post<CustomComponentRequest>(
getURL("CUSTOM_COMPONENT"),
{
code: payload.code,
frontend_node: payload.frontend_node,
},
);

return response.data;
};

const mutation: UseMutationResult<
CustomComponentRequest,
ResponseErrorTypeAPI,
IPostValidateComponentCode
> = mutate(
["usePostValidateComponentCode"],
postValidateComponentCodeFn,
options,
);

return mutation;
};
37 changes: 21 additions & 16 deletions src/frontend/src/modals/codeAreaModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "ace-builds/src-noconflict/theme-github";
import "ace-builds/src-noconflict/theme-twilight";
// import "ace-builds/webpack-resolver";
import { usePostValidateCode } from "@/controllers/API/queries/nodes/use-post-validate-code";
import { usePostValidateComponentCode } from "@/controllers/API/queries/nodes/use-post-validate-component-code";
import { useEffect, useRef, useState } from "react";
import AceEditor from "react-ace";
import ReactAce from "react-ace/lib/ace";
Expand All @@ -22,7 +23,6 @@ import {
CODE_PROMPT_DIALOG_SUBTITLE,
EDIT_CODE_TITLE,
} from "../../constants/constants";
import { postCustomComponent } from "../../controllers/API";
import useAlertStore from "../../stores/alertStore";
import { useDarkStore } from "../../stores/darkStore";
import { CodeErrorDataTypeAPI } from "../../types/api";
Expand Down Expand Up @@ -52,11 +52,13 @@ export default function CodeAreaModal({
const setErrorData = useAlertStore((state) => state.setErrorData);
const [openConfirmation, setOpenConfirmation] = useState(false);
const codeRef = useRef<ReactAce | null>(null);
const { mutate, isPending } = usePostValidateCode();
const [error, setError] = useState<{
detail: CodeErrorDataTypeAPI;
} | null>(null);

const { mutate: validateCode } = usePostValidateCode();
const { mutate: validateComponentCode } = usePostValidateComponentCode();

useEffect(() => {
// if nodeClass.template has more fields other than code and dynamic is true
// do not run handleClick
Expand All @@ -66,7 +68,7 @@ export default function CodeAreaModal({
}, []);

function processNonDynamicField() {
mutate(
validateCode(
{ code },
{
onSuccess: (apiReturn) => {
Expand Down Expand Up @@ -111,19 +113,22 @@ export default function CodeAreaModal({
}

function processDynamicField() {
postCustomComponent(code, nodeClass!)
.then((apiReturn) => {
const { data, type } = apiReturn.data;
if (data && type) {
setValue(code);
setNodeClass(data, type);
setError({ detail: { error: undefined, traceback: undefined } });
setOpen(false);
}
})
.catch((err) => {
setError(err.response.data);
});
validateComponentCode(
{ code, frontend_node: nodeClass! },
{
onSuccess: ({ data, type }) => {
if (data && type) {
setValue(code);
setNodeClass(data, type);
setError({ detail: { error: undefined, traceback: undefined } });
setOpen(false);
}
},
onError: (error) => {
setError(error.response.data);
},
},
);
}

function processCode() {
Expand Down
38 changes: 0 additions & 38 deletions src/frontend/src/utils/parameterUtils.ts

This file was deleted.

0 comments on commit f0ca3c8

Please sign in to comment.