This commit is contained in:
Mads Rasmussen
2023-12-07 21:15:53 +01:00
parent 7bfb85884c
commit f05cae60ba

View File

@@ -1,7 +1,7 @@
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { UmbMemberTypeTreeItemModel } from './types.js';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
import { EntityTreeItemResponseModel, MemberTypeResource } 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 MemberType tree that fetches data from the server
@@ -9,52 +9,38 @@ import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
* @class UmbMemberTypeTreeServerDataSource
* @implements {UmbTreeDataSource}
*/
export class UmbMemberTypeTreeServerDataSource implements UmbTreeDataSource<EntityTreeItemResponseModel> {
#host: UmbControllerHost;
export class UmbMemberTypeTreeServerDataSource extends UmbTreeServerDataSourceBase<
EntityTreeItemResponseModel,
UmbMemberTypeTreeItemModel
> {
/**
* Creates an instance of UmbMemberTypeTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbMemberTypeTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbMemberTypeTreeServerDataSource
*/
async getRootItems() {
return tryExecuteAndNotify(this.#host, MemberTypeResource.getTreeMemberTypeRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string)} parentId
* @return {*}
* @memberof UmbMemberTypeTreeServerDataSource
*/
async getChildrenOf(parentId: string | null) {
/* 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,
MemberTypeResource.getTreeMemberTypeChildren({
parentId,
}),
);
*/
}
}
// TODO: remove when interface is cleaned up
async getItems(unique: Array<string>): Promise<any> {
throw new Error('Dot not use this method. Use the item source instead');
super(host, {
getChildrenOf,
mapper,
});
}
}
const getChildrenOf = (parentUnique: string | null) => {
if (parentUnique === null) {
return MemberTypeResource.getTreeMemberTypeRoot({});
} else {
throw new Error('Not supported for the member type tree');
}
};
const mapper = (item: EntityTreeItemResponseModel): UmbMemberTypeTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId!,
name: item.name!,
type: 'member-type',
hasChildren: item.hasChildren!,
isContainer: item.isContainer!,
};
};