From a8d04c439a8db1eb768c0cc192ecb7dce68b0b47 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Thu, 7 Dec 2023 20:02:42 +0100 Subject: [PATCH] use tree server data source base --- .../document-type.tree.server.data-source.ts | 93 +++++++------------ 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/tree/document-type.tree.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/tree/document-type.tree.server.data-source.ts index 4d131e4fa5..12006428e6 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/tree/document-type.tree.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/tree/document-type.tree.server.data-source.ts @@ -1,75 +1,50 @@ import { UmbDocumentTypeTreeItemModel } from './types.js'; -import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree'; -import { DocumentTypeResource } from '@umbraco-cms/backoffice/backend-api'; +import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree'; +import { DocumentTypeResource, DocumentTypeTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; -import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources'; /** * A data source for the Document Type tree that fetches data from the server * @export * @class UmbDocumentTypeTreeServerDataSource - * @implements {UmbTreeDataSource} + * @extends {UmbTreeServerDataSourceBase} */ -export class UmbDocumentTypeTreeServerDataSource implements UmbTreeDataSource { - #host: UmbControllerHost; - +export class UmbDocumentTypeTreeServerDataSource extends UmbTreeServerDataSourceBase< + DocumentTypeTreeItemResponseModel, + UmbDocumentTypeTreeItemModel +> { /** * Creates an instance of UmbDocumentTypeTreeServerDataSource. * @param {UmbControllerHost} host * @memberof UmbDocumentTypeTreeServerDataSource */ constructor(host: UmbControllerHost) { - this.#host = host; - } - - /** - * Fetches the root items for the tree from the server - * @return {*} - * @memberof UmbDocumentTypeTreeServerDataSource - */ - async getRootItems() { - return tryExecuteAndNotify(this.#host, DocumentTypeResource.getTreeDocumentTypeRoot({})); - } - - /** - * Fetches the children of a given parent id from the server - * @param {(string | null)} parentId - * @return {*} - * @memberof UmbDocumentTypeTreeServerDataSource - */ - async getChildrenOf(parentId: string | null) { - if (parentId === undefined) throw new Error('Parent id is missing'); - - /* TODO: should we make getRootItems() internal - so it only is a server concern that there are two endpoints? */ - if (parentId === null) { - return this.getRootItems(); - } else { - return tryExecuteAndNotify( - this.#host, - DocumentTypeResource.getTreeDocumentTypeChildren({ - parentId, - }), - ); - } - } - - /** - * Fetches the items for the given ids from the server - * @param {Array} ids - * @return {*} - * @memberof UmbDocumentTypeTreeServerDataSource - */ - async getItems(ids: Array) { - if (ids) { - throw new Error('Ids are missing'); - } - - return tryExecuteAndNotify( - this.#host, - DocumentTypeResource.getDocumentTypeItem({ - id: ids, - }), - ); + super(host, { + getChildrenOf, + mapper, + }); } } + +const getChildrenOf = (parentUnique: string | null) => { + if (parentUnique === null) { + // eslint-disable-next-line local-rules/no-direct-api-import + return DocumentTypeResource.getTreeDocumentTypeRoot({}); + } else { + // eslint-disable-next-line local-rules/no-direct-api-import + return DocumentTypeResource.getTreeDocumentTypeChildren({ + parentId: parentUnique, + }); + } +}; + +const mapper = (item: DocumentTypeTreeItemResponseModel): UmbDocumentTypeTreeItemModel => { + return { + id: item.id!, + parentId: item.parentId || null, + name: item.name!, + type: 'document-type', + isContainer: item.isContainer!, + hasChildren: item.hasChildren!, + }; +};