UmbContentStoreBase

This commit is contained in:
Niels Lyngsø
2022-12-20 13:34:18 +01:00
parent f909cbd9f1
commit 0cf00634a0
4 changed files with 63 additions and 7 deletions

View File

@@ -0,0 +1,36 @@
import { UmbWorkspaceWithStoreContext } from "../workspace-context/workspace-with-store.context";
import type { DocumentDetails } from "@umbraco-cms/models";
import { UmbContentStoreBase } from "@umbraco-cms/stores/store";
import { UmbNotificationDefaultData } from "@umbraco-cms/services";
export class UmbWorkspaceContentContext<ContentTypeType extends DocumentDetails, StoreType extends UmbContentStoreBase<ContentTypeType>> extends UmbWorkspaceWithStoreContext<ContentTypeType, StoreType> {
constructor(target:HTMLElement, defaultData:ContentTypeType, storeAlias:string, entityType: string, entityKey: string) {
super(target, defaultData, storeAlias, entityType, entityKey);
}
protected _observeStore(): void {
this._dataObserver = this._store.getByKey(this.entityKey).subscribe((content) => {
if (!content) return; // TODO: Handle nicely if there is no content data.
this.update(content as any);
});
}
public save() {
this._store.save([this.getData()]).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: trash?
}

View File

@@ -5,14 +5,14 @@ import type { DocumentDetails } from "@umbraco-cms/models";
import { UmbNotificationService } from "@umbraco-cms/services";
import { UmbDataStoreBase } from "@umbraco-cms/stores/store";
export abstract class UmbWorkspaceWithStoreContext<T extends DocumentDetails> extends UmbWorkspaceContext<T> {
export abstract class UmbWorkspaceWithStoreContext<DataType extends DocumentDetails, StoreType extends UmbDataStoreBase<DataType>> extends UmbWorkspaceContext<DataType> {
protected _notificationConsumer!:UmbContextConsumer;
protected _notificationService?: UmbNotificationService;
protected _storeConsumer!:UmbContextConsumer;
protected _store!: UmbDataStoreBase<T>; // TODO: Double check its right to assume it here, at least from a type perspective?
protected _store!: StoreType; // TODO: Double check its right to assume it here, at least from a type perspective?
protected _dataObserver?:Subscription;
@@ -20,7 +20,7 @@ export abstract class UmbWorkspaceWithStoreContext<T extends DocumentDetails> ex
public entityKey:string;
constructor(target:HTMLElement, defaultData:T, storeAlias:string, entityType: string, entityKey: string) {
constructor(target:HTMLElement, defaultData:DataType, storeAlias:string, entityType: string, entityKey: string) {
super(target, defaultData)
this.entityType = entityType;
this.entityKey = entityKey;
@@ -30,7 +30,7 @@ export abstract class UmbWorkspaceWithStoreContext<T extends DocumentDetails> ex
});
// TODO: consider if store alias should be configurable of manifest:
this._storeConsumer = new UmbContextConsumer(this._target, storeAlias, (_instance: UmbDataStoreBase<T>) => {
this._storeConsumer = new UmbContextConsumer(this._target, storeAlias, (_instance: UmbDataStoreBase<DataType>) => {
this._store = _instance;
if(!this._store) {
// TODO: if we keep the type assumption of _store existing, then we should here make sure to break the application in a good way.
@@ -59,7 +59,7 @@ export abstract class UmbWorkspaceWithStoreContext<T extends DocumentDetails> ex
}*/
public getStore():UmbDataStoreBase<T> {
public getStore():UmbDataStoreBase<DataType> {
return this._store;
}

View File

@@ -1,5 +1,5 @@
import { map, Observable } from 'rxjs';
import { UmbDataStoreBase } from '../store';
import { UmbContentStoreBase } from '../store';
import type { DocumentDetails } from '@umbraco-cms/models';
import { ApiError, DocumentResource, DocumentTreeItem, FolderTreeItem, ProblemDetails } from '@umbraco-cms/backend-api';
@@ -13,7 +13,7 @@ const isDocumentDetails = (document: DocumentDetails | DocumentTreeItem): docume
* @extends {UmbDocumentStoreBase<DocumentDetails | DocumentTreeItem>}
* @description - Data Store for Documents
*/
export class UmbDocumentStore extends UmbDataStoreBase<DocumentDetails | DocumentTreeItem> {
export class UmbDocumentStore extends UmbContentStoreBase<DocumentDetails | DocumentTreeItem> {
getByKey(key: string): Observable<DocumentDetails | null> {
// TODO: use backend cli when available.
fetch(`/umbraco/management/api/v1/document/details/${key}`)

View File

@@ -73,3 +73,23 @@ export abstract class UmbDataStoreBase<T extends UmbDataStoreIdentifiers> implem
this._items.next([...storedItems]);
}
}
/**
* @export
* @class UmbContentStoreBase
* @implements {UmbDataStore<T>}
* @template T
* @description - Base class for Data Stores
*/
export abstract class UmbContentStoreBase<T extends UmbDataStoreIdentifiers> extends UmbDataStoreBase<T> {
/**
* @description - Save data.
* @param {object} data
* @return {*} {(Promise<void>)}
* @memberof UmbContentStoreBase
*/
abstract save(data: T[]): Promise<void>
}