use unique instead of content type id

This commit is contained in:
Mads Rasmussen
2024-01-16 16:45:39 +01:00
parent bca5d6de03
commit 3c04837784

View File

@@ -26,7 +26,7 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
#contentTypeRepository: UmbDetailRepository<T>;
#ownerContentTypeId?: string;
#ownerContentTypeUnique?: string;
#contentTypeObservers = new Array<UmbController>();
#contentTypes = new UmbArrayState<T>([], (x) => x.unique);
readonly contentTypes = this.#contentTypes.asObservable();
@@ -55,29 +55,30 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
* loadType will load the node type and all inherited and composed types.
* This will give us all the structure for properties and containers.
*/
public async loadType(id?: string) {
public async loadType(unique?: string) {
this._reset();
this.#ownerContentTypeId = id;
this.#ownerContentTypeUnique = unique;
const promiseResult = this._loadType(id);
const promiseResult = this._loadType(unique);
this.#init = promiseResult;
await this.#init;
return promiseResult;
}
public async createScaffold(parentId: string | null) {
public async createScaffold(parentUnique: string | null) {
this._reset();
if (parentId === undefined) return {};
if (parentUnique === undefined) return {};
const { data } = await this.#contentTypeRepository.createScaffold(parentId);
const { data } = await this.#contentTypeRepository.createScaffold(parentUnique);
if (!data) return {};
this.#ownerContentTypeId = data.unique;
this.#ownerContentTypeUnique = data.unique;
this.#init = this._observeContentType(data);
await this.#init;
debugger;
return { data };
}
@@ -146,15 +147,15 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
/** Public methods for consuming structure: */
ownerContentType() {
return this.#contentTypes.asObservablePart((x) => x.find((y) => y.unique === this.#ownerContentTypeId));
return this.#contentTypes.asObservablePart((x) => x.find((y) => y.unique === this.#ownerContentTypeUnique));
}
getOwnerContentType() {
return this.#contentTypes.getValue().find((y) => y.unique === this.#ownerContentTypeId);
return this.#contentTypes.getValue().find((y) => y.unique === this.#ownerContentTypeUnique);
}
updateOwnerContentType(entry: Partial<T>) {
this.#contentTypes.updateOne(this.#ownerContentTypeId, entry);
this.#contentTypes.updateOne(this.#ownerContentTypeUnique, entry);
}
// We could move the actions to another class?
@@ -166,7 +167,7 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
sortOrder?: number,
) {
await this.#init;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const container: PropertyTypeContainerModelBaseModel = {
id: UmbId.new(),
@@ -189,11 +190,12 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
return container;
}
async insertContainer(contentTypeId: string | null, container: PropertyTypeContainerModelBaseModel) {
async insertContainer(contentTypeUnique: string | null, container: PropertyTypeContainerModelBaseModel) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenContainers = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.containers ?? [];
const frozenContainers =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.containers ?? [];
const containers = appendToFrozenArray(frozenContainers, container, (x) => x.id === container.id);
@@ -201,7 +203,7 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { containers });
this.#contentTypes.updateOne(contentTypeUnique, { containers });
}
makeContainerNameUniqueForOwnerContentType(
@@ -223,34 +225,36 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
}
async updateContainer(
contentTypeId: string | null,
contentTypeUnique: string | null,
containerId: string,
partialUpdate: Partial<PropertyTypeContainerModelBaseModel>,
) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenContainers = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.containers ?? [];
const frozenContainers =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.containers ?? [];
const containers = partialUpdateFrozenArray(frozenContainers, partialUpdate, (x) => x.id === containerId);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { containers });
this.#contentTypes.updateOne(contentTypeUnique, { containers });
}
async removeContainer(contentTypeId: string | null, containerId: string | null = null) {
async removeContainer(contentTypeUnique: string | null, containerId: string | null = null) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenContainers = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.containers ?? [];
const frozenContainers =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.containers ?? [];
const containers = frozenContainers.filter((x) => x.id !== containerId);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { containers });
this.#contentTypes.updateOne(contentTypeUnique, { containers });
}
createPropertyScaffold(containerId: string | null = null, sortOrder?: number) {
@@ -278,67 +282,72 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
return property;
}
async createProperty(contentTypeId: string | null, containerId: string | null = null, sortOrder?: number) {
async createProperty(contentTypeUnique: string | null, containerId: string | null = null, sortOrder?: number) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const property: PropertyTypeModelBaseModel = this.createPropertyScaffold(containerId, sortOrder);
const properties = [...(this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.properties ?? [])];
const properties = [
...(this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.properties ?? []),
];
properties.push(property);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { properties });
this.#contentTypes.updateOne(contentTypeUnique, { properties });
return property;
}
async insertProperty(contentTypeId: string | null, property: PropertyTypeModelBaseModel) {
async insertProperty(contentTypeUnique: string | null, property: PropertyTypeModelBaseModel) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenProperties = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.properties ?? [];
const frozenProperties =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.properties ?? [];
const properties = appendToFrozenArray(frozenProperties, property, (x) => x.id === property.id);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { properties });
this.#contentTypes.updateOne(contentTypeUnique, { properties });
}
async removeProperty(contentTypeId: string | null, propertyId: string) {
async removeProperty(contentTypeUnique: string | null, propertyId: string) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenProperties = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.properties ?? [];
const frozenProperties =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.properties ?? [];
const properties = filterFrozenArray(frozenProperties, (x) => x.id !== propertyId);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { properties });
this.#contentTypes.updateOne(contentTypeUnique, { properties });
}
async updateProperty(
contentTypeId: string | null,
contentTypeUnique: string | null,
propertyId: string,
partialUpdate: Partial<PropertyTypeModelBaseModel>,
) {
await this.#init;
contentTypeId = contentTypeId ?? this.#ownerContentTypeId!;
contentTypeUnique = contentTypeUnique ?? this.#ownerContentTypeUnique!;
const frozenProperties = this.#contentTypes.getValue().find((x) => x.unique === contentTypeId)?.properties ?? [];
const frozenProperties =
this.#contentTypes.getValue().find((x) => x.unique === contentTypeUnique)?.properties ?? [];
const properties = partialUpdateFrozenArray(frozenProperties, partialUpdate, (x) => x.id === propertyId);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// TODO: fix TS partial complaint
this.#contentTypes.updateOne(contentTypeId, { properties });
this.#contentTypes.updateOne(contentTypeUnique, { properties });
}
// TODO: Refactor: These property methods, should maybe be named without structure in their name.
@@ -391,7 +400,7 @@ export class UmbContentTypePropertyStructureManager<T extends UmbContentTypeMode
ownerContentTypeObservablePart<PartResult>(mappingFunction: MappingFunction<T, PartResult>) {
return this.#contentTypes.asObservablePart((docTypes) => {
const docType = docTypes.find((x) => x.unique === this.#ownerContentTypeId);
const docType = docTypes.find((x) => x.unique === this.#ownerContentTypeUnique);
return docType ? mappingFunction(docType) : undefined;
});
}