From 0e159152aa920a77700290d16d4467cf0ef92942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 1 Oct 2024 10:26:23 +0200 Subject: [PATCH] blueprint save alignment --- .../document-blueprint-workspace.context.ts | 48 ++++++++++++------- .../workspace/document-workspace.context.ts | 12 +---- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts index c3acbd81d2..a54759a714 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts @@ -396,7 +396,7 @@ export class UmbDocumentBlueprintWorkspaceContext this.#data.finishPropertyValueChange(); }; - async #createOrSave() { + async #handleSave() { const current = this.#data.getCurrent(); if (!current?.unique) throw new Error('Unique is missing'); @@ -404,34 +404,49 @@ export class UmbDocumentBlueprintWorkspaceContext const parent = this.#parent.getValue(); if (!parent) throw new Error('Parent is not set'); - if ((await this.repository.create(current, parent.unique)).data !== undefined) { - this.setIsNew(false); - - // TODO: this might not be the right place to alert the tree, but it works for now - const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); - const event = new UmbRequestReloadChildrenOfEntityEvent({ - entityType: parent.entityType, - unique: parent.unique, - }); - eventContext.dispatchEvent(event); + const { data, error } = await this.repository.create(current, parent.unique); + if (!data || error) { + console.error('Error creating document', error); + throw new Error('Error creating document'); } - } else { - await this.repository.save(current); - const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); + this.setIsNew(false); + this.#data.setPersisted(data); + // We do not know about the variant IDs, so lets update everything. + this.#data.setCurrent(data); + + const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); + const event = new UmbRequestReloadChildrenOfEntityEvent({ + entityType: parent.entityType, + unique: parent.unique, + }); + eventContext.dispatchEvent(event); + } else { + // Save: + const { data, error } = await this.repository.save(current); + if (!data || error) { + console.error('Error saving document', error); + throw new Error('Error saving document'); + } + + this.#data.setPersisted(data); + // We do not know about the variant IDs, so lets update everything. + this.#data.setCurrent(data); + + const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); const event = new UmbRequestReloadStructureForEntityEvent({ unique: this.getUnique()!, entityType: this.getEntityType(), }); - actionEventContext.dispatchEvent(event); + eventContext.dispatchEvent(event); } } async submit() { const data = this.getData(); if (!data) throw new Error('Data is missing'); - await this.#createOrSave(); + await this.#handleSave(); } async delete() { @@ -451,6 +466,7 @@ export class UmbDocumentBlueprintWorkspaceContext public override destroy(): void { this.#data.destroy(); this.structure.destroy(); + this.#languageRepository.destroy(); super.destroy(); } } diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts index 58be94458c..f1cfb421d7 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts @@ -433,17 +433,6 @@ export class UmbDocumentWorkspaceContext ?.value as PropertyValueType, ); } - // TODO: Re-evaluate if this is begin used, i wrote this as part of a POC... [NL] - /* - async propertyIndexByAlias( - propertyAlias: string, - variantId?: UmbVariantId, - ): Promise | undefined> { - return this.#data.current.asObservablePart((data) => - data?.values?.findIndex((x) => x?.alias === propertyAlias && (variantId ? variantId.compare(x) : true)), - ); - } - */ /** * Get the current value of the property with the given alias and variantId. @@ -870,6 +859,7 @@ export class UmbDocumentWorkspaceContext } public override destroy(): void { + this.#data.destroy(); this.structure.destroy(); this.#languageRepository.destroy(); super.destroy();