Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed [BluePrint]: Create Type Modal Displays Data from Previously Opened Edit Type Modal, and Field Data Is Missing in Reverse Scenario #2131

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions src/components/ModalsContainer/BlueprintModal/Body/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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 (
<Flex>
Expand Down Expand Up @@ -469,7 +480,7 @@ export const Editor = ({
setDisplayParentError(false)
}}
options={selectedNodeParentOptions || []}
selectedValue={resolvedSelectedParentValue()}
selectedValue={resolvedSelectedParentValue}
/>
{errMessage && <StyledError>{errMessage}</StyledError>}
</Flex>
Expand Down
Loading