Skip to content

Commit

Permalink
fix(form): ObjectField use default value if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
collinlokken committed Sep 12, 2023
1 parent 0e9592f commit c72d62a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
"type": "CORE:BlueprintAttribute",
"attributeType": "./Person",
"label": "CEO",
"optional": true
"optional": true,
"default": {
"name": "Elon Musk",
"type": "./Person",
"phoneNumber": 420420
}
},
{
"name": "accountant",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const AttributeField = (props: TAttributeFieldProps) => {
type={attribute.attributeType}
optional={attribute.optional ?? false}
uiAttribute={uiAttribute}
defaultValue={attribute.default}
/>
)

Expand Down
67 changes: 44 additions & 23 deletions packages/dm-core-plugins/src/form/fields/ObjectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,43 @@ const SelectReference = (props: { type: string; namePath: string }) => {
)
}

const AddObject = (props: { type: string; namePath: string }) => {
const { type, namePath } = props
const AddObject = (props: {
type: string
namePath: string
defaultValue: any
}) => {
const { type, namePath, defaultValue } = props
const { setValue } = useFormContext()
const dmssAPI = useDMSS()
const { idReference } = useRegistryContext()
const handleAdd = () => {
if (!defaultValue) {
dmssAPI
.instantiateEntity({
entity: { type: type as string },
})
.then((newEntity: AxiosResponse<any>) => addDocument(newEntity.data))
} else {
addDocument(defaultValue)
}
}
const addDocument = (document: any) => {
const options = {
shouldValidate: true,
shouldDirty: true,
shouldTouch: true,
}

dmssAPI
.instantiateEntity({
entity: { type: type as string },
.documentAdd({
address: `${idReference}.${namePath}`,
document: JSON.stringify(document),
updateUncontained: false,
})
.then(() => {
setValue(namePath, document, options)
})
.then((newEntity: AxiosResponse<any>) => {
dmssAPI
.documentAdd({
address: `${idReference}.${namePath}`,
document: JSON.stringify(newEntity.data),
updateUncontained: false,
})
.then(() => {
setValue(namePath, newEntity.data, options)
})
.catch((error: AxiosError<ErrorResponse>) => {
console.error(error)
})
.catch((error: AxiosError<ErrorResponse>) => {
console.error(error)
})
}
return (
Expand Down Expand Up @@ -158,6 +166,7 @@ export const ContainedAttribute = (props: TContentProps): JSX.Element => {
uiAttribute,
uiRecipe,
blueprint,
defaultValue,
} = props
const { watch } = useFormContext()
const { idReference, onOpen } = useRegistryContext()
Expand All @@ -171,7 +180,11 @@ export const ContainedAttribute = (props: TContentProps): JSX.Element => {
(isDefined ? (
<RemoveObject namePath={namePath} />
) : (
<AddObject namePath={namePath} type={type} />
<AddObject
namePath={namePath}
type={type}
defaultValue={defaultValue}
/>
))}
{isDefined &&
(onOpen && !uiAttribute?.showInline ? (
Expand Down Expand Up @@ -277,7 +290,7 @@ export const UncontainedAttribute = (props: TContentProps): JSX.Element => {
}

export const ObjectField = (props: TObjectFieldProps): JSX.Element => {
const { type, namePath, uiAttribute, displayLabel } = props
const { type, namePath, uiAttribute, displayLabel, defaultValue } = props
const { getValues } = useFormContext()

// Be able to override the object field
Expand All @@ -294,16 +307,23 @@ export const ObjectField = (props: TObjectFieldProps): JSX.Element => {
id={namePath}
label={displayLabel}
type={type === 'object' && values ? values.type : type}
defaultValue={defaultValue}
/>
)
}

export const ObjectTypeSelector = (props: TObjectFieldProps): JSX.Element => {
const { type, namePath, displayLabel, optional, contained, uiAttribute } =
props
const {
type,
namePath,
displayLabel,
optional,
contained,
uiAttribute,
defaultValue,
} = props

const { blueprint, uiRecipes, isLoading, error } = useBlueprint(type)

if (isLoading) return <Loading />
if (error) throw new Error(`Failed to fetch blueprint for '${type}'`)
if (blueprint === undefined) return <div>Could not find the blueprint</div>
Expand All @@ -324,6 +344,7 @@ export const ObjectTypeSelector = (props: TObjectFieldProps): JSX.Element => {
blueprint={blueprint}
uiRecipe={uiRecipe}
uiAttribute={uiAttribute}
defaultValue={defaultValue}
/>
)
}
2 changes: 2 additions & 0 deletions packages/dm-core-plugins/src/form/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type TObjectFieldProps = {
optional: boolean
uiAttribute: TAttributeConfig | undefined
readOnly?: boolean
defaultValue?: any
}

export type TContentProps = {
Expand All @@ -33,6 +34,7 @@ export type TContentProps = {
uiRecipe: TUiRecipeForm | undefined
uiAttribute: TAttributeConfig | undefined
readOnly?: boolean
defaultValue?: any
}

export type TArrayFieldProps = {
Expand Down

0 comments on commit c72d62a

Please sign in to comment.