From 739f28b277f7bdb22ff8a0e9b10d4bf2fb865603 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Thu, 7 Dec 2023 19:56:19 +0100 Subject: [PATCH] use base for dictionary tree data source --- .../src/packages/core/tree/types.ts | 1 + .../dictionary-tree.server.data-source.ts | 88 +++++++------------ 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/tree/types.ts b/src/Umbraco.Web.UI.Client/src/packages/core/tree/types.ts index ff4572d7b9..e5618a2351 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/tree/types.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/tree/types.ts @@ -2,6 +2,7 @@ export interface UmbTreeItemModelBase { type: string; name: string; hasChildren: boolean; + isContainer: boolean; icon?: string | null; } diff --git a/src/Umbraco.Web.UI.Client/src/packages/dictionary/dictionary/tree/dictionary-tree.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/dictionary/dictionary/tree/dictionary-tree.server.data-source.ts index bdcfb4721a..1784f4513e 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/dictionary/dictionary/tree/dictionary-tree.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/dictionary/dictionary/tree/dictionary-tree.server.data-source.ts @@ -1,7 +1,7 @@ -import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree'; -import { DictionaryResource, EntityTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api'; +import { UmbDictionaryTreeItemModel } from './types.js'; +import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; -import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources'; +import { DictionaryResource, EntityTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api'; /** * A data source for the Dictionary tree that fetches data from the server @@ -9,63 +9,41 @@ import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources'; * @class UmbDictionaryTreeServerDataSource * @implements {UmbTreeDataSource} */ -export class UmbDictionaryTreeServerDataSource implements UmbTreeDataSource { - #host: UmbControllerHost; - +export class UmbDictionaryTreeServerDataSource extends UmbTreeServerDataSourceBase< + EntityTreeItemResponseModel, + UmbDictionaryTreeItemModel +> { /** * Creates an instance of UmbDictionaryTreeServerDataSource. * @param {UmbControllerHost} host * @memberof UmbDictionaryTreeServerDataSource */ constructor(host: UmbControllerHost) { - this.#host = host; - } - - /** - * Fetches the root items for the tree from the server - * @return {*} - * @memberof UmbDictionaryTreeServerDataSource - */ - async getRootItems() { - return tryExecuteAndNotify(this.#host, DictionaryResource.getTreeDictionaryRoot({})); - } - - /** - * Fetches the children of a given parent id from the server - * @param {(string)} parentId - * @return {*} - * @memberof UmbDictionaryTreeServerDataSource - */ - 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, - DictionaryResource.getTreeDictionaryChildren({ - parentId, - }), - ); - } - } - - /** - * Fetches the items for the given ids from the server - * @param {Array} ids - * @return {*} - * @memberof UmbDictionaryTreeServerDataSource - */ - async getItems(ids: Array) { - if (!ids) throw new Error('Ids are missing'); - return tryExecuteAndNotify( - this.#host, - DictionaryResource.getDictionaryItem({ - 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 DictionaryResource.getTreeDictionaryRoot({}); + } else { + // eslint-disable-next-line local-rules/no-direct-api-import + return DictionaryResource.getTreeDictionaryChildren({ + parentId: parentUnique, + }); + } +}; + +const mapper = (item: EntityTreeItemResponseModel): UmbDictionaryTreeItemModel => { + return { + id: item.id!, + parentId: item.parentId || null, + name: item.name!, + type: 'dictionary-item', + hasChildren: item.hasChildren!, + }; +};