use tree server data source base

This commit is contained in:
Mads Rasmussen
2023-12-07 20:38:43 +01:00
parent a8d04c439a
commit c052bd6ded
3 changed files with 87 additions and 88 deletions

View File

@@ -1,58 +1,61 @@
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { EntityTreeItemResponseModel, DocumentResource } from '@umbraco-cms/backoffice/backend-api';
import { UmbDocumentTreeItemModel } from './types.js';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
import { DocumentResource, DocumentTreeItemResponseModel } 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 Document tree that fetches data from the server
* @export
* @class UmbDocumentTreeServerDataSource
* @implements {UmbTreeDataSource}
* @extends {UmbTreeServerDataSourceBase}
*/
export class UmbDocumentTreeServerDataSource implements UmbTreeDataSource<EntityTreeItemResponseModel> {
#host: UmbControllerHost;
export class UmbDocumentTreeServerDataSource extends UmbTreeServerDataSourceBase<
DocumentTreeItemResponseModel,
UmbDocumentTreeItemModel
> {
/**
* Creates an instance of UmbDocumentTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbDocumentTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbDocumentTreeServerDataSource
*/
async getRootItems() {
return tryExecuteAndNotify(this.#host, DocumentResource.getTreeDocumentRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string)} parentId
* @return {*}
* @memberof UmbDocumentTreeServerDataSource
*/
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,
DocumentResource.getTreeDocumentChildren({
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) {
// eslint-disable-next-line local-rules/no-direct-api-import
return DocumentResource.getTreeDocumentRoot({});
} else {
// eslint-disable-next-line local-rules/no-direct-api-import
return DocumentResource.getTreeDocumentChildren({
parentId: parentUnique,
});
}
};
const mapper = (item: DocumentTreeItemResponseModel): UmbDocumentTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId || null,
name: item.name!,
type: 'document',
isContainer: item.isContainer!,
hasChildren: item.hasChildren!,
isProtected: item.isProtected!,
isPublished: item.isPublished!,
isEdited: item.isEdited!,
contentTypeId: item.contentTypeId!,
variants:
item.variants?.map((variant) => ({
name: variant.name!,
culture: variant.culture!,
state: variant.state!,
})) || [],
icon: item.icon!,
};
};

View File

@@ -1,5 +1,20 @@
import { PublishedStateModel } from '@umbraco-cms/backoffice/backend-api';
import type { UmbEntityTreeItemModel, UmbEntityTreeRootModel } from '@umbraco-cms/backoffice/tree';
export interface UmbDocumentTreeItemModel extends UmbEntityTreeItemModel {}
export interface UmbDocumentTreeItemModel extends UmbEntityTreeItemModel {
isProtected: boolean;
isPublished: boolean;
isEdited: boolean;
contentTypeId: string;
variants: Array<UmbDocumentVariantTreeItemModel>;
icon: string;
}
export interface UmbDocumentVariantTreeItemModel {
name: string;
culture: string | null;
state: PublishedStateModel; // TODO: make our own enum for this. We might have states for "unsaved changes" etc.
}
// TODO: TREE STORE TYPE PROBLEM:
export interface UmbDocumentTreeRootModel extends UmbEntityTreeRootModel {}

View File

@@ -1,7 +1,7 @@
import type { UmbTreeDataSource } from '@umbraco-cms/backoffice/tree';
import { UmbMemberGroupTreeItemModel } from './types.js';
import { UmbTreeServerDataSourceBase } from '@umbraco-cms/backoffice/tree';
import { EntityTreeItemResponseModel, MemberGroupResource } 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 MemberGroup tree that fetches data from the server
@@ -9,53 +9,34 @@ import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
* @class UmbMemberGroupTreeServerDataSource
* @implements {UmbTreeDataSource}
*/
export class UmbMemberGroupTreeServerDataSource implements UmbTreeDataSource<EntityTreeItemResponseModel> {
#host: UmbControllerHost;
export class UmbMemberGroupTreeServerDataSource extends UmbTreeServerDataSourceBase<
EntityTreeItemResponseModel,
UmbMemberGroupTreeItemModel
> {
/**
* Creates an instance of UmbMemberGroupTreeServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbMemberGroupTreeServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the root items for the tree from the server
* @return {*}
* @memberof UmbMemberGroupTreeServerDataSource
*/
async getRootItems() {
return tryExecuteAndNotify(this.#host, MemberGroupResource.getTreeMemberGroupRoot({}));
}
/**
* Fetches the children of a given parent id from the server
* @param {(string)} parentId
* @return {*}
* @memberof UmbMemberGroupTreeServerDataSource
*/
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 {
alert('not implemented');
/*
return tryExecuteAndNotify(
this.#host,
MemberGroupResource.getTreeMemberGroupChildren({
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 = () => {
return MemberGroupResource.getTreeMemberGroupRoot({});
};
const mapper = (item: EntityTreeItemResponseModel): UmbMemberGroupTreeItemModel => {
return {
id: item.id!,
parentId: item.parentId || null,
name: item.name!,
type: 'member-group',
isContainer: item.isContainer!,
hasChildren: item.hasChildren!,
};
};