refactor: Update GenericNode and related hooks to handle component type

This commit updates the GenericNode component and its related hooks to handle the component type. The code changes in the index.tsx file of the GenericNode directory include modifying the updateNodeCode function to accept an additional "type" parameter. The handleNodeClass function in the use-handle-node-class.tsx file has also been updated to handle the "type" parameter. These changes ensure that the GenericNode component can correctly handle different types of components and update the node code accordingly.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-23 17:28:52 -03:00
commit 13d53a8533
7 changed files with 26 additions and 11 deletions

View file

@ -232,9 +232,9 @@ export default function GenericNode({
if (data.node) {
postCustomComponent(currentCode, data.node)
.then((apiReturn) => {
const { data } = apiReturn;
if (data && updateNodeCode) {
updateNodeCode(data, currentCode, "code");
const { data, type } = apiReturn.data;
if (data && type && updateNodeCode) {
updateNodeCode(data, currentCode, "code", type);
setLoadingUpdate(false);
}
})

View file

@ -7,7 +7,7 @@ const useHandleNodeClass = (
setNode,
updateNodeInternals,
) => {
const handleNodeClass = (newNodeClass, code) => {
const handleNodeClass = (newNodeClass, code, type?: string) => {
if (!data.node) return;
if (data.node!.template[name].value !== code) {
takeSnapshot();
@ -22,7 +22,9 @@ const useHandleNodeClass = (
description: newNodeClass.description ?? data.node!.description,
display_name: newNodeClass.display_name ?? data.node!.display_name,
};
if (type) {
newNode.data.node.template[name].type = type;
}
newNode.data.node.template[name].value = code;
return newNode;

View file

@ -11,7 +11,7 @@ const useUpdateNodeCode = (
updateNodeInternals: (id: string) => void,
) => {
const updateNodeCode = useCallback(
(newNodeClass: APIClassType, code: string, name: string) => {
(newNodeClass: APIClassType, code: string, name: string, type: string) => {
setNode(dataId, (oldNode) => {
let newNode = cloneDeep(oldNode);
@ -22,6 +22,9 @@ const useUpdateNodeCode = (
display_name: newNodeClass.display_name ?? dataNode.display_name,
edited: false,
};
if (type) {
newNode.data.type = type;
}
newNode.data.node.template[name].value = code;
setIsOutdated(false);

View file

@ -7,6 +7,7 @@ import {
APIObjectType,
APITemplateType,
Component,
CustomComponentRequest,
LoginType,
ProfilePicturesTypeAPI,
Users,
@ -386,7 +387,7 @@ export async function getProfilePictures(): Promise<ProfilePicturesTypeAPI | nul
export async function postCustomComponent(
code: string,
apiClass: APIClassType,
): Promise<AxiosResponse<APIClassType>> {
): Promise<AxiosResponse<CustomComponentRequest>> {
// let template = apiClass.template;
return await api.post(`${BASE_URL_API}custom_component`, {
code,

View file

@ -102,9 +102,9 @@ export default function CodeAreaModal({
function processDynamicField() {
postCustomComponent(code, nodeClass!)
.then((apiReturn) => {
const { data } = apiReturn;
if (data) {
setNodeClass(data, code);
const { data, type } = apiReturn.data;
if (data && type) {
setNodeClass(data, code, type);
setError({ detail: { error: undefined, traceback: undefined } });
setOpen(false);
}

View file

@ -13,6 +13,11 @@ export type CustomFieldsType = {
[key: string]: Array<string>;
};
export type CustomComponentRequest = {
data: APIClassType;
type: string;
};
export type APIClassType = {
base_classes?: Array<string>;
description: string;

View file

@ -595,7 +595,11 @@ export type codeAreaModalPropsType = {
setOpenModal?: (bool: boolean) => void;
value: string;
nodeClass: APIClassType | undefined;
setNodeClass: (Class: APIClassType, code?: string) => void | undefined;
setNodeClass: (
Class: APIClassType,
code?: string,
type?: string,
) => void | undefined;
children: ReactNode;
dynamic?: boolean;
readonly?: boolean;