This commit is contained in:
Mads Rasmussen
2023-12-07 21:29:47 +01:00
parent 7bb4739c24
commit c7f5fa6ff8
2 changed files with 58 additions and 104 deletions

View File

@@ -1,75 +1,48 @@
import { UmbMediaTypeTreeItemModel } from './types.js';
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { MediaTypeResource } from '@umbraco-cms/backoffice/backend-api';
import { MediaTypeResource, MediaTypeTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
/**
* A data source for the Media Type tree that fetches data from the server
* @export
* @class UmbMediaTypeTreeServerDataSource
* @implements {UmbTreeDataSource}
* @extends {UmbTreeServerDataSourceBase}
*/
export class UmbMediaTypeTreeServerDataSource implements UmbTreeDataSource<UmbMediaTypeTreeItemModel> {
#host: UmbControllerHost;
export class UmbMediaTypeTreeServerDataSource extends UmbTreeServerDataSourceBase<
MediaTypeTreeItemResponseModel,
UmbMediaTypeTreeItemModel
> {
/**
* Creates an instance of UmbMediaTypeTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbMediaTypeTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbMediaTypeTreeServerDataSource
*/
async getRootItems() {
return tryExecuteAndNotify(this.#host, MediaTypeResource.getTreeMediaTypeRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string | null)} parentId
* @return {*}
* @memberof UmbMediaTypeTreeServerDataSource
*/
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,
MediaTypeResource.getTreeMediaTypeChildren({
parentId,
}),
);
}
}
/**
* Fetches the items for the given ids from the server
* @param {Array<string>} ids
* @return {*}
* @memberof UmbMediaTypeTreeServerDataSource
*/
async getItems(ids: Array<string>) {
if (ids) {
throw new Error('Ids are missing');
}
return tryExecuteAndNotify(
this.#host,
MediaTypeResource.getMediaTypeItem({
id: ids,
}),
);
super(host, {
getChildrenOf,
mapper,
});
}
}
const getChildrenOf = (parentUnique: string | null) => {
if (parentUnique === null) {
return MediaTypeResource.getTreeMediaTypeRoot({});
} else {
return MediaTypeResource.getTreeMediaTypeChildren({
parentId: parentUnique,
});
}
};
const mapper = (item: MediaTypeTreeItemResponseModel): UmbMediaTypeTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId!,
name: item.name!,
type: 'media-type',
hasChildren: item.hasChildren!,
isContainer: item.isContainer!,
};
};

View File

@@ -1,61 +1,42 @@
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { UmbMemberTreeItemModel } from './types.js';
import { EntityTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
/**
* A data source for the Member tree that fetches data from the server
* @export
* @class UmbMemberTreeServerDataSource
* @implements {UmbTreeDataSource}
* @extends {UmbTreeServerDataSourceBase}
*/
export class UmbMemberTreeServerDataSource implements UmbTreeDataSource<EntityTreeItemResponseModel> {
#host: UmbControllerHost;
export class UmbMemberTreeServerDataSource extends UmbTreeServerDataSourceBase<
EntityTreeItemResponseModel,
UmbMemberTreeItemModel
> {
/**
* Creates an instance of UmbMemberTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbMemberTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbMemberTreeServerDataSource
*/
async getRootItems() {
alert('not implemented');
//return tryExecuteAndNotify(this.#host, MemberResource.getTreeMemberRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string)} parentId
* @return {*}
* @memberof UmbMemberTreeServerDataSource
*/
async getChildrenOf(parentId: string | null) {
alert('not implemented');
/* 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,
MemberResource.getTreeMemberChildren({
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): any => {
alert('not implemented');
};
const mapper = (item: EntityTreeItemResponseModel): UmbMemberTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId!,
name: item.name!,
type: 'member',
hasChildren: item.hasChildren!,
isContainer: item.isContainer!,
};
};