split code into document workspace context

This commit is contained in:
Niels Lyngsø
2023-01-26 10:39:39 +01:00
parent d9d255904d
commit 8b124b3e4c
4 changed files with 131 additions and 15 deletions

View File

@@ -1,10 +1,14 @@
import { UmbWorkspaceContentContext } from '../../../shared/components/workspace/workspace-content/workspace-content.context';
import { UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN } from '../document.detail.store';
import type { UmbDocumentDetailStore } from '../document.detail.store';
import type { UmbControllerHostInterface } from '@umbraco-cms/controller';
import { v4 as uuidv4 } from 'uuid';
import { UmbDocumentDetailStore, UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN } from '../document.detail.store';
import type { UmbWorkspaceEntityContextInterface } from '../../../shared/components/workspace/workspace-context/workspace-entity-context.interface';
import { UmbWorkspaceContext } from '../../../shared/components/workspace/workspace-context/workspace-context';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
import type { DocumentDetails } from '@umbraco-cms/models';
import { appendToFrozenArray } from '@umbraco-cms/observable-api';
import { appendToFrozenArray, createObservablePart, ObjectState, UmbObserverController } from '@umbraco-cms/observable-api';
import { UmbContextConsumerController } from '@umbraco-cms/context-api';
import { UmbNotificationDefaultData, UmbNotificationService, UMB_NOTIFICATION_SERVICE_CONTEXT_TOKEN } from '@umbraco-cms/notification';
/*
const DefaultDocumentData = {
key: '',
name: '',
@@ -33,14 +37,111 @@ const DefaultDocumentData = {
},
],
} as DocumentDetails;
*/
export class UmbDocumentWorkspaceContext extends UmbWorkspaceContext implements UmbWorkspaceEntityContextInterface<DocumentDetails | undefined> {
#data = new ObjectState<DocumentDetails | undefined>(undefined);
public readonly data = this.#data.asObservable();
public readonly name = this.#data.getObservablePart((data) => data?.name);
#isNew = false;
private _entityKey?: string;
protected _storeSubscription?: UmbObserverController;
private _notificationService?: UmbNotificationService;
private _store?: UmbDocumentDetailStore;
export class UmbWorkspaceDocumentContext extends UmbWorkspaceContentContext<DocumentDetails, UmbDocumentDetailStore> {
constructor(host: UmbControllerHostInterface) {
super(host, DefaultDocumentData, UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN.toString(), 'document');
super(host);
new UmbContextConsumerController(this._host, UMB_NOTIFICATION_SERVICE_CONTEXT_TOKEN, (_instance) => {
this._notificationService = _instance;
});
new UmbContextConsumerController(this._host, UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN, (_instance) => {
this._store = _instance;
this._observeStore();
});
console.log("UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN", UMB_DOCUMENT_DETAIL_STORE_CONTEXT_TOKEN.toString())
}
private _observeStore() {
if (!this._store || !this._entityKey) {
return;
}
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.#data.next(content as any);
}
);
}
}
public getStore() {
return this._store;
}
load(entityKey: string) {
this.#isNew = false;
this._entityKey = entityKey;
this._observeStore();
}
create(parentKey: string | null) {
this.#isNew = true;
this._entityKey = uuidv4();
console.log("I'm new, and I will be created under ", parentKey);
}
getData() {
return this.#data.getValue();
}
save(): Promise<void> {
if (!this._store) {
// TODO: add a more beautiful error:
console.error('Could not save cause workspace context has no store.');
return Promise.resolve();
}
const documentData = this.getData();
if(!documentData) {
console.error('Could not save cause workspace context has no data.');
return Promise.resolve();
}
return this._store
.save([documentData])
.then(() => {
const data: UmbNotificationDefaultData = { message: 'Document Saved' };
this._notificationService?.peek('positive', { data });
})
.catch(() => {
const data: UmbNotificationDefaultData = { message: 'Failed to save Document' };
this._notificationService?.peek('danger', { data });
});
}
// TODO: how can we make sure to call this, we might need to turn this thing into a ContextProvider(extending) for it to call destroy?
public destroy(): void {
this.#data.unsubscribe();
}
public setPropertyValue(alias: string, value: unknown) {
// TODO: make sure to check that we have a details model? otherwise fail? 8This can be relevant if we use the same context for tree actions?
@@ -48,7 +149,7 @@ export class UmbWorkspaceDocumentContext extends UmbWorkspaceContentContext<Docu
const newDataSet = appendToFrozenArray(this._data.getValue().data, entry, x => x.alias);
this._data.update({data: newDataSet});
this.#data.update({data: newDataSet});
}
/*

View File

@@ -1,7 +1,7 @@
import { UUITextStyles } from '@umbraco-ui/uui-css/lib';
import { css, html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { UmbWorkspaceDocumentContext } from './document-workspace.context';
import { UmbDocumentWorkspaceContext } from './document-workspace.context';
import { UmbLitElement } from '@umbraco-cms/element';
import type { UmbWorkspaceEntityElement } from 'src/backoffice/shared/components/workspace/workspace-entity-element.interface';
@@ -35,7 +35,7 @@ export class UmbDocumentWorkspaceElement extends UmbLitElement implements UmbWor
this._workspaceContext.create(parentKey);
}
private _workspaceContext: UmbWorkspaceDocumentContext = new UmbWorkspaceDocumentContext(this);
private _workspaceContext: UmbDocumentWorkspaceContext = new UmbDocumentWorkspaceContext(this);
render() {
return html`<umb-workspace-content alias="Umb.Workspace.Document"></umb-workspace-content>`;

View File

@@ -0,0 +1,14 @@
import { UmbContextProviderController } from '@umbraco-cms/context-api';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
export abstract class UmbWorkspaceContext {
protected _host: UmbControllerHostInterface;
constructor(host: UmbControllerHostInterface) {
this._host = host;
new UmbContextProviderController(host, 'UmbWorkspaceContext', this);
}
}

View File

@@ -2,11 +2,12 @@ import { Observable } from "rxjs";
export interface UmbWorkspaceEntityContextInterface<T> {
readonly data:Observable<T>;
readonly name:Observable<string>;
entityKey?: string;
entityType: string;
readonly data: Observable<T>;
readonly name: Observable<string|undefined>;
//entityKey?: string;
//entityType: string;
getData(): T;