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:
parent
bf650ecec5
commit
f328179d82
5 changed files with 90 additions and 41 deletions
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
};
|
||||||
|
|
@ -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,43 +66,48 @@ 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) {
|
||||||
if (funcErrors.length === 0 && importsErrors.length === 0) {
|
let importsErrors = apiReturn.imports.errors;
|
||||||
setSuccessData({
|
let funcErrors = apiReturn.function.errors;
|
||||||
title: CODE_SUCCESS_ALERT,
|
if (funcErrors.length === 0 && importsErrors.length === 0) {
|
||||||
});
|
setSuccessData({
|
||||||
setOpen(false);
|
title: CODE_SUCCESS_ALERT,
|
||||||
setValue(code);
|
});
|
||||||
// setValue(code);
|
setOpen(false);
|
||||||
|
setValue(code);
|
||||||
|
// setValue(code);
|
||||||
|
} else {
|
||||||
|
if (funcErrors.length !== 0) {
|
||||||
|
setErrorData({
|
||||||
|
title: FUNC_ERROR_ALERT,
|
||||||
|
list: funcErrors,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (importsErrors.length !== 0) {
|
||||||
|
setErrorData({
|
||||||
|
title: IMPORT_ERROR_ALERT,
|
||||||
|
list: importsErrors,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (funcErrors.length !== 0) {
|
setErrorData({
|
||||||
setErrorData({
|
title: BUG_ALERT,
|
||||||
title: FUNC_ERROR_ALERT,
|
});
|
||||||
list: funcErrors,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (importsErrors.length !== 0) {
|
|
||||||
setErrorData({
|
|
||||||
title: IMPORT_ERROR_ALERT,
|
|
||||||
list: importsErrors,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
|
onError: (error) => {
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: BUG_ALERT,
|
title: CODE_ERROR_ALERT,
|
||||||
|
list: [error.response.data.detail],
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
})
|
},
|
||||||
.catch((_) => {
|
);
|
||||||
setErrorData({
|
|
||||||
title: CODE_ERROR_ALERT,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDynamicField() {
|
function processDynamicField() {
|
||||||
|
|
|
||||||
|
|
@ -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>;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue