Skip to content

Commit

Permalink
prevent block grid from setting and initial empty block object value
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Dec 11, 2024
1 parent d514454 commit 8ccc394
Showing 1 changed file with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ export class UmbPropertyEditorUIBlockGridElement
#settingsDataPathTranslator?: UmbBlockElementDataValidationPathTranslator;
#managerContext = new UmbBlockGridManagerContext(this);
//
private _value: UmbBlockGridValueModel = {
layout: {},
contentData: [],
settingsData: [],
expose: [],
};
private _value: UmbBlockGridValueModel | undefined = undefined;

#lastValue: UmbBlockGridValueModel | undefined = undefined;

public set config(config: UmbPropertyEditorConfigCollection | undefined) {
if (!config) return;
Expand All @@ -68,6 +65,13 @@ export class UmbPropertyEditorUIBlockGridElement

@property({ attribute: false })
public override set value(value: UmbBlockGridValueModel | undefined) {
this.#lastValue = value;

if (!value) {
this._value = undefined;
return;
}

const buildUpValue: Partial<UmbBlockGridValueModel> = value ? { ...value } : {};
buildUpValue.layout ??= {};
buildUpValue.contentData ??= [];
Expand All @@ -80,7 +84,7 @@ export class UmbPropertyEditorUIBlockGridElement
this.#managerContext.setSettings(this._value.settingsData);
this.#managerContext.setExposes(this._value.expose);
}
public override get value(): UmbBlockGridValueModel {
public override get value(): UmbBlockGridValueModel | undefined {
return this._value;
}

Expand Down Expand Up @@ -116,13 +120,24 @@ export class UmbPropertyEditorUIBlockGridElement
this.#managerContext.exposes,
]).pipe(debounceTime(20)),
([layouts, contents, settings, exposes]) => {
this._value = {
...this._value,
layout: { [UMB_BLOCK_GRID_PROPERTY_EDITOR_SCHEMA_ALIAS]: layouts },
contentData: contents,
settingsData: settings,
expose: exposes,
};
if (layouts.length === 0) {
this._value = undefined;
} else {
this._value = {
...this._value,
layout: { [UMB_BLOCK_GRID_PROPERTY_EDITOR_SCHEMA_ALIAS]: layouts },
contentData: contents,
settingsData: settings,
expose: exposes,
};
}

// If we don't have a value set from the outside or an internal value, we don't want to set the value.
// This is added to prevent the block grid from setting an empty value on startup.
if (this.#lastValue === undefined && this._value === undefined) {
return;
}

Check warning on line 140 in src/Umbraco.Web.UI.Client/src/packages/block/block-grid/property-editors/block-grid-editor/property-editor-ui-block-grid.element.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/15.1)

❌ New issue: Complex Method

UmbPropertyEditorUIBlockGridElement.constructor has a cyclomatic complexity of 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
propertyContext.setValue(this._value);
},
'motherObserver',
Expand Down

0 comments on commit 8ccc394

Please sign in to comment.