From 3137699d9f47388b7d18e24ac27d6f0d4042b21f Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Wed, 1 Feb 2023 21:08:06 +0100 Subject: [PATCH] update method names --- .../libs/models/index.ts | 13 +++-- .../components/tree/tree-item.element.ts | 18 ++---- .../shared/components/tree/tree.context.ts | 23 ++++---- .../shared/components/tree/tree.element.ts | 12 ++-- .../templates/tree/data/sources/index.ts | 4 +- .../data/sources/template.tree.server.data.ts | 4 +- .../tree/data/template.tree.repository.ts | 58 ++++++++++--------- .../tree/data/template.tree.store.ts | 6 +- 8 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/libs/models/index.ts b/src/Umbraco.Web.UI.Client/libs/models/index.ts index e770cd8d6e..9e91bcb3df 100644 --- a/src/Umbraco.Web.UI.Client/libs/models/index.ts +++ b/src/Umbraco.Web.UI.Client/libs/models/index.ts @@ -164,14 +164,19 @@ export interface UmbTreeRepositoryFactory { } export interface UmbTreeRepository { - getRoot: () => Promise<{ + requestRootItems: () => Promise<{ data: PagedEntityTreeItem | undefined; - updates: Observable | undefined; error: ProblemDetails | undefined; }>; - getChildren: (parentKey: string | null) => Promise<{ + requestChildrenOf: (parentKey: string | null) => Promise<{ data: PagedEntityTreeItem | undefined; - updates: Observable | undefined; error: ProblemDetails | undefined; }>; + requestItems: (keys: string[]) => Promise<{ + data: Array | undefined; + error: ProblemDetails | undefined; + }>; + rootItems: () => Promise>; + childrenOf: (parentKey: string | null) => Promise>; + items: (keys: string[]) => Promise>; } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree-item.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree-item.element.ts index 7252b67b87..1f611ae50c 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree-item.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree-item.element.ts @@ -171,20 +171,14 @@ export class UmbTreeItem extends UmbLitElement { } private async _observeRepositoryChildren() { - if (!this._treeContext?.getChildren) return; + if (!this._treeContext?.requestChildrenOf) return; - this._loading = true; + // TODO: add loading state + this._treeContext.requestChildrenOf(this.key); - const { updates } = await this._treeContext.getChildren(this.key); - - this._loading = false; - - if (updates) { - this.observe(updates, (childItems) => { - this._childItems = childItems as Entity[]; - this._loading = false; - }); - } + this.observe(await this._treeContext.childrenOf(this.key), (childItems) => { + this._childItems = childItems as Entity[]; + }); } // TODO: remove when repositories are in place diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.context.ts index db2c0101f1..28b0dda2ba 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.context.ts @@ -22,7 +22,7 @@ export class UmbTreeContextBase implements UmbTreeContext { #selection = new DeepState(>[]); public readonly selection = this.#selection.asObservable(); - repository?: UmbTreeRepository; + repository!: UmbTreeRepository; constructor(host: UmbControllerHostInterface, tree: ManifestTree) { this.#host = host; @@ -55,18 +55,19 @@ export class UmbTreeContextBase implements UmbTreeContext { this.#selection.next(selection.filter((x) => x !== key)); } - public async getRoot() { - if (!this.repository) { - return { data: undefined, updates: undefined, error: undefined }; - } - return this.repository?.getRoot(); + public async requestRootItems() { + return this.repository.requestRootItems(); } - public async getChildren(parentKey: string | null) { - if (!this.repository) { - return { data: undefined, updates: undefined, error: undefined }; - } + public async requestChildrenOf(parentKey: string | null) { + return this.repository.requestChildrenOf(parentKey); + } - return this.repository.getChildren(parentKey); + public async rootItems() { + return this.repository.rootItems(); + } + + public async childrenOf(parentKey: string | null) { + return this.repository.childrenOf(parentKey); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.element.ts index 0dfca907cb..7fba991109 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/tree/tree.element.ts @@ -120,15 +120,13 @@ export class UmbTreeElement extends UmbLitElement { } private async _observeRepositoryTreeRoot() { - if (!this._treeContext?.getRoot) return; + if (!this._treeContext?.requestRootItems) return; - const { updates } = await this._treeContext.getRoot(); + this._treeContext.requestRootItems(); - if (updates) { - this.observe(updates, (rootItems) => { - this._items = rootItems as Entity[]; - }); - } + this.observe(await this._treeContext.rootItems(), (rootItems) => { + this._items = rootItems as Entity[]; + }); } private _observeSelection() { diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/index.ts b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/index.ts index 6fe96ca88d..b1d7c01021 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/index.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/index.ts @@ -2,7 +2,7 @@ import type { DataSourceResponse } from '@umbraco-cms/models'; import { EntityTreeItem, PagedEntityTreeItem } from '@umbraco-cms/backend-api'; export interface TemplateTreeDataSource { - getRoot(): Promise>; - getChildren(parentKey: string): Promise>; + getRootItems(): Promise>; + getChildrenOf(parentKey: string): Promise>; getItems(key: Array): Promise>; } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/template.tree.server.data.ts b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/template.tree.server.data.ts index e55aa41c5d..a5a73e7d27 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/template.tree.server.data.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/sources/template.tree.server.data.ts @@ -9,11 +9,11 @@ export class TemplateTreeServerDataSource implements TemplateTreeDataSource { this.#host = host; } - async getRoot() { + async getRootItems() { return tryExecuteAndNotify(this.#host, TemplateResource.getTreeTemplateRoot({})); } - async getChildren(parentKey: string | null) { + async getChildrenOf(parentKey: string | null) { if (!parentKey) { const error: ProblemDetails = { title: 'Parent key is missing' }; return { error }; diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.repository.ts b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.repository.ts index bc05ab2150..eb786f8f09 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.repository.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.repository.ts @@ -13,7 +13,7 @@ import type { UmbTreeRepository } from '@umbraco-cms/models'; export class UmbTemplateTreeRepository implements UmbTreeRepository { #host: UmbControllerHostInterface; #dataSource: TemplateTreeServerDataSource; - #treeStore!: UmbTemplateTreeStore; + #treeStore?: UmbTemplateTreeStore; #notificationService?: UmbNotificationService; #initResolver?: () => void; #initialized = false; @@ -34,11 +34,9 @@ export class UmbTemplateTreeRepository implements UmbTreeRepository { }); } - #init() { - return new Promise((resolve) => { - this.#initialized ? resolve() : (this.#initResolver = resolve); - }); - } + #init = new Promise((resolve) => { + this.#initialized ? resolve() : (this.#initResolver = resolve); + }); #checkIfInitialized() { if (this.#treeStore && this.#notificationService) { @@ -47,54 +45,60 @@ export class UmbTemplateTreeRepository implements UmbTreeRepository { } } - async getRoot() { - await this.#init(); - let updates = undefined; + async requestRootItems() { + await this.#init; - const { data, error } = await this.#dataSource.getRoot(); + const { data, error } = await this.#dataSource.getRootItems(); if (data) { this.#treeStore?.appendItems(data.items); - updates = this.#treeStore?.rootChanged(); } - return { data, updates, error }; + return { data, error }; } - async getChildren(parentKey: string | null) { - await this.#init(); - let updates = undefined; + async requestChildrenOf(parentKey: string | null) { + await this.#init; if (!parentKey) { const error: ProblemDetails = { title: 'Parent key is missing' }; - return { data: undefined, updates, error }; + return { data: undefined, error }; } - const { data, error } = await this.#dataSource.getChildren(parentKey); + const { data, error } = await this.#dataSource.getChildrenOf(parentKey); if (data) { this.#treeStore?.appendItems(data.items); - updates = this.#treeStore?.childrenChanged(parentKey); } - return { data, updates, error }; + return { data, error }; } - async getItems(keys: Array) { - await this.#init(); - let updates = undefined; + async requestItems(keys: Array) { + await this.#init; if (!keys) { const error: ProblemDetails = { title: 'Keys are missing' }; - return { data: undefined, updates, error }; + return { data: undefined, error }; } const { data, error } = await this.#dataSource.getItems(keys); - if (data) { - updates = this.#treeStore?.itemsChanged(keys); - } + return { data, error }; + } - return { data, updates, error }; + async rootItems() { + await this.#init; + return this.#treeStore!.rootItems(); + } + + async childrenOf(parentKey: string | null) { + await this.#init; + return this.#treeStore!.childrenOf(parentKey); + } + + async items(keys: Array) { + await this.#init; + return this.#treeStore!.items(keys); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.store.ts b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.store.ts index 90aa247821..9558908b2c 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.store.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/templating/templates/tree/data/template.tree.store.ts @@ -39,15 +39,15 @@ export class UmbTemplateTreeStore extends UmbStoreBase { } } - rootChanged() { + rootItems() { return this.#data.getObservablePart((items) => items.filter((item) => item.parentKey === null)); } - childrenChanged(parentKey: string) { + childrenOf(parentKey: string | null) { return this.#data.getObservablePart((items) => items.filter((item) => item.parentKey === parentKey)); } - itemsChanged(keys: Array) { + items(keys: Array) { return this.#data.getObservablePart((items) => items.filter((item) => keys.includes(item.key ?? ''))); } }