From fb38823deb4fd76b1c896edf83d04fd2324a3d6c Mon Sep 17 00:00:00 2001 From: Sophie Turner Date: Thu, 12 Sep 2024 19:31:52 +0500 Subject: [PATCH] fix(blue-print): data sync issue between Type and Edit Type modals --- .../BlueprintModal/Body/Editor/index.tsx | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/src/components/ModalsContainer/BlueprintModal/Body/Editor/index.tsx b/src/components/ModalsContainer/BlueprintModal/Body/Editor/index.tsx index d2fe9bdc1..0fd65b448 100644 --- a/src/components/ModalsContainer/BlueprintModal/Body/Editor/index.tsx +++ b/src/components/ModalsContainer/BlueprintModal/Body/Editor/index.tsx @@ -221,29 +221,36 @@ export const Editor = ({ }, [selectedSchema]) useEffect(() => { - const init = async () => { - if (selectedSchema) { - setValue('type', selectedSchema?.type as string) - setValue('parent', selectedSchema.parent) - - let parsedDataDefault: parsedObjProps[] = [{ required: false, type: 'string', key: '' }] + const resetForm = () => { + reset(defaultValues) + setParsedData([{ required: false, type: 'string', key: '' }]) + setDeletedAttributes([]) + + setMediaOptions({ + videoAudio: false, + image: false, + sourceLink: false, + }) + } - if (selectedSchema.type !== NoParent.value.toLowerCase()) { - const data = await getNodeType(selectedSchema.type as string) + resetForm() - parsedDataDefault = data ? parseJson(data) : parsedDataDefault - } + if (selectedSchema) { + setValue('type', selectedSchema.type as string) + setValue('parent', selectedSchema.parent) - parsedDataDefault = parsedDataDefault.filter((x) => x.key !== 'node_key') + if (selectedSchema.type !== NoParent.value.toLowerCase()) { + getNodeType(selectedSchema.type as string).then((data) => { + const parsedDataDefault = data ? parseJson(data) : [{ required: false, type: 'string', key: '' }] + const filteredData = parsedDataDefault.filter((x) => x.key !== 'node_key') - setParsedData(parsedDataDefault) - - await fetchAndSetOptions(setSelectedNodeParentOptions, (schema) => schema.type !== selectedSchema.type) + setParsedData(filteredData) + }) } - } - init() - }, [selectedSchema, setValue]) + fetchAndSetOptions(setSelectedNodeParentOptions, (schema) => schema.type !== selectedSchema.type) + } + }, [selectedSchema, setValue, reset]) const parent = watch('parent') @@ -373,19 +380,23 @@ export const Editor = ({ const resolvedParentValue = () => parentOptions?.find((i) => i.value === parent) - const resolvedSelectedParentValue = (): TAutocompleteOption | undefined => { - const option = selectedNodeParentOptions?.find((i) => i.value === parent) + const resolvedSelectedParentValue = useMemo((): TAutocompleteOption | undefined => { + if (!selectedSchema) { + return undefined + } + + const option = selectedNodeParentOptions?.find((i) => i.value === selectedSchema.parent) if (option) { return option } - if (parent) { - return { label: parent, value: parent } + if (selectedSchema.parent) { + return { label: selectedSchema.parent, value: selectedSchema.parent } } return undefined - } + }, [selectedSchema, selectedNodeParentOptions]) return ( @@ -469,7 +480,7 @@ export const Editor = ({ setDisplayParentError(false) }} options={selectedNodeParentOptions || []} - selectedValue={resolvedSelectedParentValue()} + selectedValue={resolvedSelectedParentValue} /> {errMessage && {errMessage}}