🔧 fix(parameterComponent): remove unused imports and variables to improve code cleanliness and readability

🔧 fix(parameterComponent): update useState initial value for dictArr to be an array of objects instead of an empty array
🔧 fix(parameterComponent): update value prop in ParameterInputListComponent to handle cases where data.node!.template[name].value is null or an empty array
🔧 fix(parameterComponent): update onChange prop in ParameterInputListComponent to handle any type of newValue instead of just string[]
🔧 fix(keypairListComponent): update className condition to check if value is null or undefined before checking its length
🔧 fix(editNodeModal): remove unused imports and variables to improve code cleanliness and readability
🔧 fix(editNodeModal): update useState initial value for dictArr to be an array of objects instead of an empty array
🔧 fix(editNodeModal): update value prop in KeypairListComponent to handle cases where myData.node.template[templateParam].value is null or an empty array
🔧 fix(editNodeModal): update onChange prop in KeypairListComponent to handle any type of newValue instead of just string[]
🔧 fix(types): update onChange type in KeyPairListComponentType to accept an array of objects instead of an array of strings
🔧 fix(reactflowUtils): remove unused convertArrayToObj function
This commit is contained in:
Cristhian Zanforlin Lousa 2023-09-15 15:49:37 -03:00
commit ef5a3fb355
5 changed files with 47 additions and 60 deletions

View file

@ -27,8 +27,6 @@ import { typesContext } from "../../../../contexts/typesContext";
import { ParameterComponentType } from "../../../../types/components"; import { ParameterComponentType } from "../../../../types/components";
import { TabsState } from "../../../../types/tabs"; import { TabsState } from "../../../../types/tabs";
import { import {
convertArrayToObj,
convertObjToArray,
hasDuplicateKeys, hasDuplicateKeys,
isValidConnection, isValidConnection,
} from "../../../../utils/reactflowUtils"; } from "../../../../utils/reactflowUtils";
@ -113,10 +111,9 @@ export default function ParameterComponent({
}); });
const [errorDuplicateKey, setErrorDuplicateKey] = useState(false); const [errorDuplicateKey, setErrorDuplicateKey] = useState(false);
const [dict, setDict] = useState({ const [dictArr, setDictArr] = useState([
yourKey: "yourValue", { yourKey: "yourValue" },
} as {}); ] as Object[]);
const [dictArr, setDictArr] = useState([] as string[]);
useEffect(() => { useEffect(() => {
if (name === "openai_api_base") console.log(info); if (name === "openai_api_base") console.log(info);
@ -384,18 +381,21 @@ export default function ParameterComponent({
disabled={disabled} disabled={disabled}
editNode={false} editNode={false}
value={ value={
convertObjToArray(data.node!.template[name].value).length === 0 data.node!.template[name].value?.length === 0 ||
? convertObjToArray(dict) !data.node!.template[name].value
: convertObjToArray(data.node!.template[name].value) ? dictArr
: data.node!.template[name].value
} }
duplicateKey={errorDuplicateKey} duplicateKey={errorDuplicateKey}
onChange={(newValue: string[]) => { onChange={(newValue) => {
setErrorDuplicateKey(hasDuplicateKeys(newValue)); setErrorDuplicateKey(hasDuplicateKeys(newValue));
if (hasDuplicateKeys(newValue)) { if (hasDuplicateKeys(newValue)) {
setDictArr(newValue); setDictArr(newValue);
} else { } else {
setDict(convertArrayToObj(newValue)); setDictArr(newValue);
data.node!.template[name].value = convertArrayToObj(newValue); console.log(newValue);
data.node!.template[name].value = newValue;
} }
}} }}
/> />

View file

@ -37,11 +37,11 @@ export default function KeypairListComponent({
return ( return (
<div <div
className={classNames( className={classNames(
value.length > 1 && editNode ? "my-1" : "", value?.length > 1 && editNode ? "my-1" : "",
"flex flex-col gap-3" "flex flex-col gap-3"
)} )}
> >
{value.map((obj, index) => { {value?.map((obj, index) => {
return Object.keys(obj).map((key, idx) => { return Object.keys(obj).map((key, idx) => {
return ( return (
<div key={idx} className="flex w-full gap-3"> <div key={idx} className="flex w-full gap-3">

View file

@ -28,11 +28,7 @@ import { TabsContext } from "../../contexts/tabsContext";
import { typesContext } from "../../contexts/typesContext"; import { typesContext } from "../../contexts/typesContext";
import { NodeDataType } from "../../types/flow"; import { NodeDataType } from "../../types/flow";
import { TabsState } from "../../types/tabs"; import { TabsState } from "../../types/tabs";
import { import { hasDuplicateKeys } from "../../utils/reactflowUtils";
convertArrayToObj,
convertObjToArray,
hasDuplicateKeys,
} from "../../utils/reactflowUtils";
import { classNames } from "../../utils/utils"; import { classNames } from "../../utils/utils";
import BaseModal from "../baseModal"; import BaseModal from "../baseModal";
@ -100,14 +96,9 @@ const EditNodeModal = forwardRef(
}); });
const [errorDuplicateKey, setErrorDuplicateKey] = useState(false); const [errorDuplicateKey, setErrorDuplicateKey] = useState(false);
const [dict, setDict] = useState({ const [dictArr, setDictArr] = useState([
yourKey: "yourValue", { yourKey: "yourValue" },
} as {}); ] as Object[]);
const [dictArr, setDictArr] = useState([] as string[]);
useEffect(() => {
setDictArr(convertObjToArray(dict));
}, [dict]);
return ( return (
<BaseModal size="large-h-full" open={modalOpen} setOpen={setModalOpen}> <BaseModal size="large-h-full" open={modalOpen} setOpen={setModalOpen}>
@ -214,19 +205,29 @@ const EditNodeModal = forwardRef(
<div className="mt-2 w-full"> <div className="mt-2 w-full">
<KeypairListComponent <KeypairListComponent
disabled={disabled} disabled={disabled}
editNode={true} editNode={false}
value={dictArr} value={
myData.node.template[templateParam]
.value?.length === 0 ||
!myData.node.template[templateParam]
.value
? dictArr
: myData.node.template[
templateParam
].value
}
duplicateKey={errorDuplicateKey} duplicateKey={errorDuplicateKey}
onChange={(newValue: string[]) => { onChange={(newValue) => {
setErrorDuplicateKey( setErrorDuplicateKey(
hasDuplicateKeys(newValue) hasDuplicateKeys(newValue)
); );
if (hasDuplicateKeys(newValue)) { if (hasDuplicateKeys(newValue)) {
setDictArr(newValue); setDictArr(newValue);
} else { } else {
setDict( setDictArr(newValue);
convertArrayToObj(newValue) myData.node!.template[
); templateParam
].value = newValue;
} }
}} }}
/> />

View file

@ -56,7 +56,7 @@ export type InputListComponentType = {
export type KeyPairListComponentType = { export type KeyPairListComponentType = {
value: any; value: any;
onChange: (value: string[]) => void; onChange: (value: Object[]) => void;
disabled: boolean; disabled: boolean;
editNode?: boolean; editNode?: boolean;
duplicateKey?: boolean; duplicateKey?: boolean;

View file

@ -298,23 +298,9 @@ export function convertObjToArray(singleObject) {
arrConverted.push(newObj); arrConverted.push(newObj);
} }
} }
return arrConverted; return arrConverted;
} }
export function convertArrayToObj(newValue) {
const flattenedObject = {};
for (const obj of newValue) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
flattenedObject[key] = obj[key]; //added space to dont order when add new keys to object
}
}
}
let newData = _.cloneDeep(flattenedObject);
return newData;
}
export function hasDuplicateKeys(array) { export function hasDuplicateKeys(array) {
const keys = {}; const keys = {};
for (const obj of array) { for (const obj of array) {