Segments: Fix for processing data for Segments-variants (#21018)

* refactor to load segments before processing incoming data

* clean up

* remove unused segment promise
This commit is contained in:
Niels Lyngsø
2025-12-02 15:00:25 +01:00
committed by GitHub
parent 657ccbd104
commit 857f2900bb
4 changed files with 38 additions and 39 deletions

View File

@@ -92,7 +92,6 @@ export abstract class UmbContentTypeWorkspaceContextBase<
let { data } = await request;
if (data) {
data = await this._processIncomingData(data);
data = await this._scaffoldProcessData(data);
if (this.modalContext) {

View File

@@ -368,11 +368,6 @@ export abstract class UmbContentDetailWorkspaceContextBase<
(varies) => {
this._data.setVariesBySegment(varies);
this.#variesBySegment = varies;
if (varies) {
this.loadSegments();
} else {
this._segments.setValue([]);
}
},
null,
);
@@ -393,20 +388,28 @@ export abstract class UmbContentDetailWorkspaceContextBase<
this.#languages.setValue(data?.items ?? []);
}
/**
* @deprecated Call `_loadSegmentsFor` instead. `loadSegments` will be removed in v.18.
* (note this was introduced in v.17, and deprecated in v.17.0.1)
*/
protected async loadSegments() {
console.warn('Stop using loadSegments, call _loadSegmentsFor instead. loadSegments will be removed in v.18.');
const unique = await firstValueFrom(this.unique);
if (!unique) {
this._segments.setValue([]);
return;
}
this._loadSegmentsFor(unique);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async _loadSegmentsFor(unique: string): Promise<void> {
console.warn(
`UmbContentDetailWorkspaceContextBase: Segments are not implemented in the workspace context for "${this.getEntityType()}" types.`,
);
this._segments.setValue([]);
}
/**
* @deprecated Call `_processIncomingData` instead. `_scaffoldProcessData` will be removed in v.18.
*/
protected override _scaffoldProcessData(data: DetailModelType): Promise<DetailModelType> {
return this._processIncomingData(data);
}
protected override async _processIncomingData(data: DetailModelType): Promise<DetailModelType> {
const contentTypeUnique: string | undefined = (data as any)[this.#contentTypePropertyName].unique;
if (!contentTypeUnique) {
@@ -415,24 +418,28 @@ export abstract class UmbContentDetailWorkspaceContextBase<
// Load the content type structure, usually this comes from the data, but in this case we are making the data, and we need this to be able to complete the data. [NL]
await this.structure.loadType(contentTypeUnique);
// Load segments if varying by segment, or reset to empty array:
if (this.#variesBySegment) {
await this._loadSegmentsFor(data.unique);
} else {
this._segments.setValue([]);
}
// Set culture and segment for all values:
const cultures = this.#languages.getValue().map((x) => x.unique);
if (this.structure.variesBySegment) {
// TODO: v.17 Engage please note we have not implemented support for segments yet. [NL]
console.warn('Segments are not yet implemented for preset');
let segments: Array<string> | undefined;
if (this.#variesBySegment) {
segments = this._segments.getValue().map((s) => s.alias);
}
// TODO: Add Segments for Presets:
const segments: Array<string> | undefined = this.structure.variesBySegment ? [] : undefined;
const repo = new UmbDataTypeDetailRepository(this);
const propertyTypes = await this.structure.getContentTypeProperties();
const contentTypeVariesByCulture = this.structure.getVariesByCulture();
const contentTypeVariesBySegment = this.structure.getVariesByCulture();
const contentTypeVariesBySegment = this.structure.getVariesBySegment();
const valueDefinitions = await Promise.all(
propertyTypes.map(async (property) => {
// TODO: Implement caching for data-type requests. [NL]
const dataType = (await repo.requestByUnique(property.dataType.unique)).data;
// This means if its not loaded this will never resolve and the error below will never happen.
if (!dataType) {

View File

@@ -253,7 +253,7 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
}
}
} else if (data) {
const processedData = await this._processIncomingData(data);
const processedData = await this._scaffoldProcessData(data);
this._data.setPersisted(processedData);
this._data.setCurrent(processedData);
@@ -311,7 +311,6 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
let { data } = await request;
if (data) {
data = await this._processIncomingData(data);
data = await this._scaffoldProcessData(data);
if (this.modalContext) {
@@ -336,7 +335,7 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
* @returns {Promise<DetailModelType>} The processed data.
*/
protected async _scaffoldProcessData(data: DetailModelType): Promise<DetailModelType> {
return data;
return await this._processIncomingData(data);
}
protected async _processIncomingData(data: DetailModelType): Promise<DetailModelType> {
return data;

View File

@@ -227,22 +227,16 @@ export class UmbDocumentWorkspaceContext
this.#isTrashedContext.setIsTrashed(false);
}
protected override async loadSegments(): Promise<void> {
this.observe(
this.unique,
async (unique) => {
if (!unique) {
this._segments.setValue([]);
return;
}
const { data } = await this.#documentSegmentRepository.getDocumentByIdSegmentOptions(unique, {
skip: 0,
take: 9999,
});
this._segments.setValue(data?.items ?? []);
},
'_loadSegmentsUnique',
);
protected override async _loadSegmentsFor(unique: string): Promise<void> {
if (!unique) {
this._segments.setValue([]);
return;
}
const { data } = await this.#documentSegmentRepository.getDocumentByIdSegmentOptions(unique, {
skip: 0,
take: 9999,
});
this._segments.setValue(data?.items ?? []);
}
async create(parent: UmbEntityModel, documentTypeUnique: string, blueprintUnique?: string) {