use base for dictionary tree data source

This commit is contained in:
Mads Rasmussen
2023-12-07 19:56:19 +01:00
parent 0d7e543f06
commit 739f28b277
2 changed files with 34 additions and 55 deletions

View File

@@ -2,6 +2,7 @@ export interface UmbTreeItemModelBase {
type: string;
name: string;
hasChildren: boolean;
isContainer: boolean;
icon?: string | null;
}

View File

@@ -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<EntityTreeItemResponseModel> {
#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<string>} ids
* @return {*}
* @memberof UmbDictionaryTreeServerDataSource
*/
async getItems(ids: Array<string>) {
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!,
};
};