Skip to content

Commit bbe8e36

Browse files
committed
blueprint save alignment
1 parent 950ce0f commit bbe8e36

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

src/packages/documents/document-blueprints/workspace/document-blueprint-workspace.context.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -396,42 +396,57 @@ export class UmbDocumentBlueprintWorkspaceContext
396396
this.#data.finishPropertyValueChange();
397397
};
398398

399-
async #createOrSave() {
399+
async #handleSave() {
400400
const current = this.#data.getCurrent();
401401
if (!current?.unique) throw new Error('Unique is missing');
402402

403403
if (this.getIsNew()) {
404404
const parent = this.#parent.getValue();
405405
if (!parent) throw new Error('Parent is not set');
406406

407-
if ((await this.repository.create(current, parent.unique)).data !== undefined) {
408-
this.setIsNew(false);
409-
410-
// TODO: this might not be the right place to alert the tree, but it works for now
411-
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
412-
const event = new UmbRequestReloadChildrenOfEntityEvent({
413-
entityType: parent.entityType,
414-
unique: parent.unique,
415-
});
416-
eventContext.dispatchEvent(event);
407+
const { data, error } = await this.repository.create(current, parent.unique);
408+
if (!data || error) {
409+
console.error('Error creating document', error);
410+
throw new Error('Error creating document');
417411
}
412+
413+
this.setIsNew(false);
414+
this.#data.setPersisted(data);
415+
// We do not know about the variant IDs, so lets update everything.
416+
this.#data.setCurrent(data);
417+
418+
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
419+
const event = new UmbRequestReloadChildrenOfEntityEvent({
420+
entityType: parent.entityType,
421+
unique: parent.unique,
422+
});
423+
eventContext.dispatchEvent(event);
418424
} else {
419-
await this.repository.save(current);
425+
// Save:
426+
const { data, error } = await this.repository.save(current);
427+
if (!data || error) {
428+
console.error('Error saving document', error);
429+
throw new Error('Error saving document');
430+
}
431+
432+
this.#data.setPersisted(data);
433+
// We do not know about the variant IDs, so lets update everything.
434+
this.#data.setCurrent(data);
420435

421-
const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
436+
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
422437
const event = new UmbRequestReloadStructureForEntityEvent({
423438
unique: this.getUnique()!,
424439
entityType: this.getEntityType(),
425440
});
426441

427-
actionEventContext.dispatchEvent(event);
442+
eventContext.dispatchEvent(event);
428443
}
429444
}
430445

431446
async submit() {
432447
const data = this.getData();
433448
if (!data) throw new Error('Data is missing');
434-
await this.#createOrSave();
449+
await this.#handleSave();
435450
}
436451

437452
async delete() {
@@ -451,6 +466,7 @@ export class UmbDocumentBlueprintWorkspaceContext
451466
public override destroy(): void {
452467
this.#data.destroy();
453468
this.structure.destroy();
469+
this.#languageRepository.destroy();
454470
super.destroy();
455471
}
456472
}

src/packages/documents/documents/workspace/document-workspace.context.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,6 @@ export class UmbDocumentWorkspaceContext
433433
?.value as PropertyValueType,
434434
);
435435
}
436-
// TODO: Re-evaluate if this is begin used, i wrote this as part of a POC... [NL]
437-
/*
438-
async propertyIndexByAlias(
439-
propertyAlias: string,
440-
variantId?: UmbVariantId,
441-
): Promise<Observable<number | undefined> | undefined> {
442-
return this.#data.current.asObservablePart((data) =>
443-
data?.values?.findIndex((x) => x?.alias === propertyAlias && (variantId ? variantId.compare(x) : true)),
444-
);
445-
}
446-
*/
447436

448437
/**
449438
* Get the current value of the property with the given alias and variantId.
@@ -870,6 +859,7 @@ export class UmbDocumentWorkspaceContext
870859
}
871860

872861
public override destroy(): void {
862+
this.#data.destroy();
873863
this.structure.destroy();
874864
this.#languageRepository.destroy();
875865
super.destroy();

0 commit comments

Comments
 (0)