This commit is contained in:
Mads Rasmussen
2023-12-07 21:22:50 +01:00
parent f05cae60ba
commit 7bb4739c24
2 changed files with 40 additions and 44 deletions

View File

@@ -1,58 +1,50 @@
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { EntityTreeItemResponseModel, MediaResource } from '@umbraco-cms/backoffice/backend-api';
import { UmbMediaTreeItemModel } from './types.js';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
import { MediaResource, MediaTreeItemResponseModel } 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 Media tree that fetches data from the server
* @export
* @class UmbMediaTreeServerDataSource
* @implements {UmbTreeDataSource}
* @extends {UmbTreeServerDataSourceBase}
*/
export class UmbMediaTreeServerDataSource implements UmbTreeDataSource<EntityTreeItemResponseModel> {
#host: UmbControllerHost;
export class UmbMediaTreeServerDataSource extends UmbTreeServerDataSourceBase<
MediaTreeItemResponseModel,
UmbMediaTreeItemModel
> {
/**
* Creates an instance of UmbMediaTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbMediaTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbMediaTreeServerDataSource
*/
async getRootItems() {
return tryExecuteAndNotify(this.#host, MediaResource.getTreeMediaRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string)} parentId
* @return {*}
* @memberof UmbMediaTreeServerDataSource
*/
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,
MediaResource.getTreeMediaChildren({
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 MediaResource.getTreeMediaRoot({});
} else {
return MediaResource.getTreeMediaChildren({
parentId: parentUnique,
});
}
};
const mapper = (item: MediaTreeItemResponseModel): UmbMediaTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId!,
name: item.name!,
type: 'media',
hasChildren: item.hasChildren!,
isContainer: item.isContainer!,
noAccess: item.noAccess!,
isTrashed: item.isTrashed!,
};
};

View File

@@ -1,5 +1,9 @@
import type { UmbEntityTreeItemModel, UmbEntityTreeRootModel } from '@umbraco-cms/backoffice/tree';
export interface UmbMediaTreeItemModel extends UmbEntityTreeItemModel {}
export interface UmbMediaTreeItemModel extends UmbEntityTreeItemModel {
noAccess: boolean;
isTrashed: boolean;
}
// TODO: TREE STORE TYPE PROBLEM:
export interface UmbMediaTreeRootModel extends UmbEntityTreeRootModel {}