refactor: add code validation functionality on tanstack mutation (#3469)

* Added Validate endpoint

* Added API Code Validate type

* Added post validate code hook

* Used mutation instead of API call to validate code

* Removed validate code api call
This commit is contained in:
Lucas Oliveira 2024-08-21 10:24:54 -03:00 • committed by GitHub
commit f328179d82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 90 additions and 41 deletions

View file

@ -18,6 +18,7 @@ export const URLs = {
FLOWS: `flows`, FLOWS: `flows`,
FOLDERS: `folders`, FOLDERS: `folders`,
VARIABLES: `variables`, VARIABLES: `variables`,
VALIDATE: `validate`,
CONFIG: `config`, CONFIG: `config`,
} as const; } as const;

View file

@ -21,7 +21,6 @@ import {
InitTypeAPI, InitTypeAPI,
PromptTypeAPI, PromptTypeAPI,
UploadFileTypeAPI, UploadFileTypeAPI,
errorsTypeAPI,
} from "./../../types/api/index"; } from "./../../types/api/index";
/** /**
@ -58,12 +57,6 @@ export async function sendAll(data: sendAllProps) {
return await api.post(`${BASE_URL_API}predict`, data); return await api.post(`${BASE_URL_API}predict`, data);
} }
export async function postValidateCode(
code: string,
): Promise<AxiosResponse<errorsTypeAPI>> {
return await api.post(`${BASE_URL_API}validate/code`, { code });
}
/** /**
* Checks the prompt for the code block by sending it to an API endpoint. * Checks the prompt for the code block by sending it to an API endpoint.
* @param {string} name - The name of the field to check. * @param {string} name - The name of the field to check.

View file

@ -0,0 +1,43 @@
import {
APICodeValidateType,
ResponseErrorDetailAPI,
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 IPostValidateCode {
code: string;
}
export const usePostValidateCode: useMutationFunctionType<
undefined,
IPostValidateCode,
APICodeValidateType,
ResponseErrorDetailAPI
> = (options?) => {
const { mutate } = UseRequestProcessor();
const postValidateCodeFn = async (
payload: IPostValidateCode,
): Promise<APICodeValidateType> => {
const response = await api.post<APICodeValidateType>(
getURL("VALIDATE", { 1: "code" }),
{
code: payload.code,
},
);
return response.data;
};
const mutation: UseMutationResult<
APICodeValidateType,
ResponseErrorDetailAPI,
IPostValidateCode
> = mutate(["usePostValidateCode"], postValidateCodeFn, options);
return mutation;
};

View file

@ -4,6 +4,7 @@ import "ace-builds/src-noconflict/mode-python";
import "ace-builds/src-noconflict/theme-github"; import "ace-builds/src-noconflict/theme-github";
import "ace-builds/src-noconflict/theme-twilight"; import "ace-builds/src-noconflict/theme-twilight";
// import "ace-builds/webpack-resolver"; // import "ace-builds/webpack-resolver";
import { usePostValidateCode } from "@/controllers/API/queries/nodes/use-post-validate-code";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import AceEditor from "react-ace"; import AceEditor from "react-ace";
import ReactAce from "react-ace/lib/ace"; import ReactAce from "react-ace/lib/ace";
@ -21,7 +22,7 @@ import {
CODE_PROMPT_DIALOG_SUBTITLE, CODE_PROMPT_DIALOG_SUBTITLE,
EDIT_CODE_TITLE, EDIT_CODE_TITLE,
} from "../../constants/constants"; } from "../../constants/constants";
import { postCustomComponent, postValidateCode } from "../../controllers/API"; import { postCustomComponent } from "../../controllers/API";
import useAlertStore from "../../stores/alertStore"; import useAlertStore from "../../stores/alertStore";
import { useDarkStore } from "../../stores/darkStore"; import { useDarkStore } from "../../stores/darkStore";
import { CodeErrorDataTypeAPI } from "../../types/api"; import { CodeErrorDataTypeAPI } from "../../types/api";
@ -51,6 +52,7 @@ export default function CodeAreaModal({
const setErrorData = useAlertStore((state) => state.setErrorData); const setErrorData = useAlertStore((state) => state.setErrorData);
const [openConfirmation, setOpenConfirmation] = useState(false); const [openConfirmation, setOpenConfirmation] = useState(false);
const codeRef = useRef<ReactAce | null>(null); const codeRef = useRef<ReactAce | null>(null);
const { mutate, isPending } = usePostValidateCode();
const [error, setError] = useState<{ const [error, setError] = useState<{
detail: CodeErrorDataTypeAPI; detail: CodeErrorDataTypeAPI;
} | null>(null); } | null>(null);
@ -64,11 +66,13 @@ export default function CodeAreaModal({
}, []); }, []);
function processNonDynamicField() { function processNonDynamicField() {
postValidateCode(code) mutate(
.then((apiReturn) => { { code },
if (apiReturn.data) { {
let importsErrors = apiReturn.data.imports.errors; onSuccess: (apiReturn) => {
let funcErrors = apiReturn.data.function.errors; if (apiReturn) {
let importsErrors = apiReturn.imports.errors;
let funcErrors = apiReturn.function.errors;
if (funcErrors.length === 0 && importsErrors.length === 0) { if (funcErrors.length === 0 && importsErrors.length === 0) {
setSuccessData({ setSuccessData({
title: CODE_SUCCESS_ALERT, title: CODE_SUCCESS_ALERT,
@ -95,12 +99,15 @@ export default function CodeAreaModal({
title: BUG_ALERT, title: BUG_ALERT,
}); });
} }
}) },
.catch((_) => { onError: (error) => {
setErrorData({ setErrorData({
title: CODE_ERROR_ALERT, title: CODE_ERROR_ALERT,
list: [error.response.data.detail],
}); });
}); },
},
);
} }
function processDynamicField() { function processDynamicField() {

View file

@ -15,6 +15,11 @@ export type APITemplateType = {
[key: string]: InputFieldType; [key: string]: InputFieldType;
}; };
export type APICodeValidateType = {
imports: { errors: Array<string> };
function: { errors: Array<string> };
};
export type CustomFieldsType = { export type CustomFieldsType = {
[key: string]: Array<string>; [key: string]: Array<string>;
}; };