From 83227bf35d344fe25063385f28b715beb1dc0857 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 6 Jan 2023 08:28:31 +0100 Subject: [PATCH 1/2] remove check because the workspace context is always available --- .../data-types/workspace/workspace-data-type.element.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/workspace/workspace-data-type.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/workspace/workspace-data-type.element.ts index 1df735f59f..b03c43dc2d 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/workspace/workspace-data-type.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/workspace/workspace-data-type.element.ts @@ -37,13 +37,13 @@ export class UmbWorkspaceDataTypeElement extends UmbLitElement { public set entityKey(value: string) { this._entityKey = value; if (this._entityKey) { - this._workspaceContext?.load(this._entityKey); + this._workspaceContext.load(this._entityKey); } } @property() public set create(parentKey: string | null) { - this._workspaceContext?.create(parentKey); + this._workspaceContext.create(parentKey); } private _workspaceContext: UmbWorkspaceDataTypeContext = new UmbWorkspaceDataTypeContext(this); @@ -64,7 +64,7 @@ export class UmbWorkspaceDataTypeElement extends UmbLitElement { private _onPropertyValueChange = (e: Event) => { const target = e.composedPath()[0] as any; - this._workspaceContext?.setPropertyValue(target?.alias, target?.value); + this._workspaceContext.setPropertyValue(target?.alias, target?.value); }; // TODO. find a way where we don't have to do this for all Workspaces. @@ -73,7 +73,7 @@ export class UmbWorkspaceDataTypeElement extends UmbLitElement { const target = event.composedPath()[0] as UUIInputElement; if (typeof target?.value === 'string') { - this._workspaceContext?.update({ name: target.value }); + this._workspaceContext.update({ name: target.value }); } } } From 7f205339f33397a1dab13f0280fb9fe605292659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Fri, 6 Jan 2023 11:14:23 +0100 Subject: [PATCH 2/2] refactor workspace context to self provide --- .../document-type-workspace.context.ts | 4 +- .../document-type-workspace.element.ts | 23 ++++------- .../workspace/document-workspace.element.ts | 16 +++----- .../workspace/media-workspace.context.ts | 4 +- .../workspace/media-workspace.element.ts | 18 ++++---- .../settings/data-types/data-type.store.ts | 1 + .../workspace-view-collection.element.ts | 2 +- .../workspace-content.context.ts | 26 ++++++++---- .../workspace/user-group-workspace.context.ts | 4 +- .../workspace/user-group-workspace.element.ts | 30 ++++++-------- .../users/workspace/user-workspace.context.ts | 4 +- .../users/workspace/user-workspace.element.ts | 41 ++++++++----------- 12 files changed, 82 insertions(+), 91 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.context.ts index 0b7e654bda..86d909b900 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.context.ts @@ -20,7 +20,7 @@ export class UmbWorkspaceDocumentTypeContext extends UmbWorkspaceContentContext< UmbDocumentTypeStoreItemType, UmbDocumentTypeStore > { - constructor(host: UmbControllerHostInterface, entityKey: string) { - super(host, DefaultDocumentTypeData, 'umbDocumentTypeStore', entityKey, 'documentType'); + constructor(host: UmbControllerHostInterface) { + super(host, DefaultDocumentTypeData, 'umbDocumentTypeStore', 'documentType'); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.element.ts index 92ff854dd3..4ab87c39f5 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/documents/document-types/workspace/document-type-workspace.element.ts @@ -53,10 +53,17 @@ export class UmbDocumentTypeWorkspaceElement extends UmbLitElement implements Um } public set entityKey(value: string) { this._entityKey = value; - this._provideWorkspace(); + if (this._entityKey) { + this._workspaceContext?.load(this._entityKey); + } } - private _workspaceContext?: UmbWorkspaceDocumentTypeContext; + @property() + public set create(parentKey: string | null) { + this._workspaceContext?.create(parentKey); + } + + private _workspaceContext: UmbWorkspaceDocumentTypeContext = new UmbWorkspaceDocumentTypeContext(this); @state() private _documentType?: DocumentTypeDetails; @@ -69,18 +76,6 @@ export class UmbDocumentTypeWorkspaceElement extends UmbLitElement implements Um this.consumeContext('umbModalService', (instance) => { this._modalService = instance; }); - } - - protected _provideWorkspace() { - if (this._entityKey) { - this._workspaceContext = new UmbWorkspaceDocumentTypeContext(this, this._entityKey); - this.provideContext('umbWorkspaceContext', this._workspaceContext); - this._observeWorkspace(); - } - } - - private async _observeWorkspace() { - if (!this._workspaceContext) return; this.observe(this._workspaceContext.data.pipe(distinctUntilChanged()), (data) => { // TODO: make method to identify if data is of type DocumentTypeDetails diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/workspace/document-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/workspace/document-workspace.element.ts index 0a512316aa..82a847c010 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/workspace/document-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/workspace/document-workspace.element.ts @@ -3,6 +3,7 @@ import { css, html } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { UmbWorkspaceDocumentContext } from './document-workspace.context'; import { UmbLitElement } from '@umbraco-cms/element'; +import type { UmbWorkspaceEntityElement } from 'src/backoffice/shared/components/workspace/workspace-entity-element.interface'; @customElement('umb-document-workspace') export class UmbDocumentWorkspaceElement extends UmbLitElement implements UmbWorkspaceEntityElement { @@ -25,24 +26,17 @@ export class UmbDocumentWorkspaceElement extends UmbLitElement implements UmbWor public set entityKey(value: string) { this._entityKey = value; if (this._entityKey) { - this._workspaceContext?.load(this._entityKey); + this._workspaceContext.load(this._entityKey); } } @property() public set create(parentKey: string | null) { - this._workspaceContext?.create(parentKey); - } - - private _workspaceContext?: UmbWorkspaceDocumentContext; - - - constructor() { - super(); - this._workspaceContext = new UmbWorkspaceDocumentContext(this); - this.provideContext('umbWorkspaceContext', this._workspaceContext); + this._workspaceContext.create(parentKey); } + private _workspaceContext: UmbWorkspaceDocumentContext = new UmbWorkspaceDocumentContext(this); + render() { return html``; } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.context.ts index 91ee8141b2..ca40faedcf 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.context.ts @@ -32,7 +32,7 @@ const DefaultMediaData = { } as UmbMediaStoreItemType; export class UmbWorkspaceMediaContext extends UmbWorkspaceContentContext { - constructor(host: UmbControllerHostInterface, entityKey: string) { - super(host, DefaultMediaData, 'umbMediaStore', entityKey, 'media'); + constructor(host: UmbControllerHostInterface) { + super(host, DefaultMediaData, 'umbMediaStore', 'media'); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.element.ts index 27b0f6339d..5b6dca7d65 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/media/media/workspace/media-workspace.element.ts @@ -24,19 +24,19 @@ export class UmbMediaWorkspaceElement extends UmbLitElement { } public set entityKey(value: string) { this._entityKey = value; - this._provideWorkspace(); - } - - private _workspaceContext?: UmbWorkspaceMediaContext; - - protected _provideWorkspace() { if (this._entityKey) { - this._workspaceContext?.destroy(); - this._workspaceContext = new UmbWorkspaceMediaContext(this, this._entityKey); - this.provideContext('umbWorkspaceContext', this._workspaceContext); + this._workspaceContext?.load(this._entityKey); } } + @property() + public set create(parentKey: string | null) { + this._workspaceContext?.create(parentKey); + } + + private _workspaceContext: UmbWorkspaceMediaContext = new UmbWorkspaceMediaContext(this); + + render() { return html``; } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/data-type.store.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/data-type.store.ts index 8110ce3299..95c9d089f0 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/data-type.store.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/data-types/data-type.store.ts @@ -17,6 +17,7 @@ export type UmbDataTypeStoreItemType = DataTypeDetails | FolderTreeItem; * @description - Data Store for Data Types */ export class UmbDataTypeStore extends UmbDataStoreBase { + public readonly storeAlias = 'umbDataTypeStore'; /** diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/views/collection/workspace-view-collection.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/views/collection/workspace-view-collection.element.ts index 879e2a0683..4d6eac122f 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/views/collection/workspace-view-collection.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/views/collection/workspace-view-collection.element.ts @@ -40,7 +40,7 @@ export class UmbWorkspaceViewCollectionElement extends UmbLitElement { this._collectionContext = new UmbCollectionContext( this, this._workspaceContext.entityKey, - this._workspaceContext.getStore().storeAlias + this._workspaceContext.getStore()?.storeAlias || '' // The store is available when the context is available. ); this.provideContext('umbCollectionContext', this._collectionContext); } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/workspace-content.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/workspace-content.context.ts index 695bc39e0d..9d491762c9 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/workspace-content.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/workspace/workspace-content/workspace-content.context.ts @@ -7,8 +7,10 @@ import { ContentTreeItem } from '@umbraco-cms/backend-api'; import { UmbControllerHostInterface } from 'src/core/controller/controller-host.mixin'; import { UmbContextConsumerController } from 'src/core/context-api/consume/context-consumer.controller'; import { UmbObserverController } from '@umbraco-cms/observable-api'; +import { UmbContextProviderController } from 'src/core/context-api/provide/context-provider.controller'; // TODO: Consider if its right to have this many class-inheritance of WorkspaceContext +// TODO: Could we extract this code into a 'Manager' of its own, which will be instantiated by the concrete Workspace Context. This will be more transparent and 'reuseable' export class UmbWorkspaceContentContext< ContentTypeType extends ContentTreeItem = ContentTreeItem, StoreType extends UmbNodeStoreBase = UmbNodeStoreBase @@ -19,6 +21,8 @@ export class UmbWorkspaceContentContext< protected _store: StoreType|null = null; protected _storeSubscription?: UmbObserverController; + #isNew = true; + public entityKey?: string; public entityType: string; @@ -47,15 +51,20 @@ export class UmbWorkspaceContentContext< return; } this._readyToLoad(); + + // TODO: first provide when we have umbNotificationService as well. + new UmbContextProviderController(this._host, 'umbWorkspaceContext', this); }); } load(entityKey: string) { + this.#isNew = false; this.entityKey = entityKey; this._readyToLoad(); } create(parentKey: string | null) { + this.#isNew = true; this.entityKey = uuidv4(); console.log("I'm new, and I will be created under ", parentKey) } @@ -64,15 +73,18 @@ export class UmbWorkspaceContentContext< if(!this._store || !this.entityKey) { return; } - this._storeSubscription?.destroy(); - this._storeSubscription = new UmbObserverController(this._host, this._store.getByKey(this.entityKey), - (content) => { - if (!content) return; // TODO: Handle nicely if there is no content data. - this.update(content as any); - }); + + if(!this.#isNew) { + this._storeSubscription?.destroy(); + this._storeSubscription = new UmbObserverController(this._host, this._store.getByKey(this.entityKey), + (content) => { + if (!content) return; // TODO: Handle nicely if there is no content data. + this.update(content as any); + }); + } } - public getStore(): StoreType | null { + public getStore() { return this._store; } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.context.ts index 6a796cd2ba..beea232e4b 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.context.ts @@ -18,7 +18,7 @@ export class UmbWorkspaceUserGroupContext extends UmbWorkspaceContentContext< UmbUserGroupStoreItemType, UmbUserGroupStore > { - constructor(host: UmbControllerHostInterface, entityKey: string) { - super(host, DefaultDataTypeData, 'umbUserStore', entityKey, 'userGroup'); + constructor(host: UmbControllerHostInterface) { + super(host, DefaultDataTypeData, 'umbUserStore', 'userGroup'); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.element.ts index c5c1a7fbe7..66409d0a2c 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/users/user-groups/workspace/user-group-workspace.element.ts @@ -194,10 +194,17 @@ export class UmbUserGroupWorkspaceElement extends UmbLitElement implements UmbWo } public set entityKey(value: string) { this._entityKey = value; - this._provideWorkspace(); + if (this._entityKey) { + this._workspaceContext.load(this._entityKey); + } } - private _workspaceContext?: UmbWorkspaceUserGroupContext; + @property() + public set create(parentKey: string | null) { + this._workspaceContext.create(parentKey); + } + + private _workspaceContext: UmbWorkspaceUserGroupContext = new UmbWorkspaceUserGroupContext(this); @state() private _userGroup?: UserGroupDetails | null; @@ -214,15 +221,10 @@ export class UmbUserGroupWorkspaceElement extends UmbLitElement implements UmbWo this._userStore = instance; this._observeUsers(); }); - } - protected _provideWorkspace() { - if (this._entityKey) { - this._workspaceContext = new UmbWorkspaceUserGroupContext(this, this._entityKey); - this.provideContext('umbWorkspaceContext', this._workspaceContext); - - this._observeUserGroup(); - } + this.observe(this._workspaceContext.data.pipe(distinctUntilChanged()), (userGroup) => { + this._userGroup = userGroup; + }); } private _registerWorkspaceActions() { @@ -247,14 +249,6 @@ export class UmbUserGroupWorkspaceElement extends UmbLitElement implements UmbWo }); } - private _observeUserGroup() { - if (!this._workspaceContext) return; - - this.observe(this._workspaceContext.data.pipe(distinctUntilChanged()), (userGroup) => { - this._userGroup = userGroup; - }); - } - private _observeUsers() { if (!this._userStore) return; diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.context.ts index 4d534f72a4..2016e0609b 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.context.ts @@ -21,7 +21,7 @@ const DefaultDataTypeData = { } as UmbUserStoreItemType; export class UmbWorkspaceUserContext extends UmbWorkspaceContentContext { - constructor(host: UmbControllerHostInterface, entityKey: string) { - super(host, DefaultDataTypeData, 'umbUserStore', entityKey, 'user'); + constructor(host: UmbControllerHostInterface) { + super(host, DefaultDataTypeData, 'umbUserStore', 'user'); } } diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.element.ts index 1f1a9b60e4..a74a587a89 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/users/users/workspace/user-workspace.element.ts @@ -96,10 +96,17 @@ export class UmbUserWorkspaceElement extends UmbLitElement implements UmbWorkspa } public set entityKey(value: string) { this._entityKey = value; - this._provideWorkspace(); + if (this._entityKey) { + this._workspaceContext.load(this._entityKey); + } } - private _workspaceContext?: UmbWorkspaceUserContext; + @property() + public set create(parentKey: string | null) { + this._workspaceContext.create(parentKey); + } + + private _workspaceContext: UmbWorkspaceUserContext = new UmbWorkspaceUserContext(this); @state() private _user?: UserDetails; @@ -114,14 +121,13 @@ export class UmbUserWorkspaceElement extends UmbLitElement implements UmbWorkspa this._currentUserStore = store; this._observeCurrentUser(); }); - } - protected _provideWorkspace() { - if (this._entityKey) { - this._workspaceContext = new UmbWorkspaceUserContext(this, this._entityKey); - this.provideContext('umbWorkspaceContext', this._workspaceContext); - this._observeUser(); - } + this.observe(this._workspaceContext.data.pipe(distinctUntilChanged()), (user) => { + this._user = user; + if (user.name !== this._userName) { + this._userName = user.name; + } + }); } private async _observeCurrentUser() { @@ -133,32 +139,21 @@ export class UmbUserWorkspaceElement extends UmbLitElement implements UmbWorkspa }); } - private async _observeUser() { - if (!this._workspaceContext) return; - - this.observe(this._workspaceContext.data.pipe(distinctUntilChanged()), (user) => { - this._user = user; - if (user.name !== this._userName) { - this._userName = user.name; - } - }); - } - private _updateUserStatus() { if (!this._user || !this._workspaceContext) return; const isDisabled = this._user.status === 'disabled'; // TODO: make sure we use store /workspace right, maybe move function to workspace, or store reference to store? isDisabled - ? this._workspaceContext.getStore().enableUsers([this._user.key]) - : this._workspaceContext.getStore().disableUsers([this._user.key]); + ? this._workspaceContext.getStore()?.enableUsers([this._user.key]) + : this._workspaceContext.getStore()?.disableUsers([this._user.key]); } private _deleteUser() { if (!this._user || !this._workspaceContext) return; // TODO: make sure we use store /workspace right, maybe move function to workspace, or store reference to store? - this._workspaceContext.getStore().deleteUsers([this._user.key]); + this._workspaceContext.getStore()?.deleteUsers([this._user.key]); history.pushState(null, '', 'section/users/view/users/overview'); }