UmbContentStoreBase
This commit is contained in:
@@ -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?
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,14 +5,14 @@ import type { DocumentDetails } from "@umbraco-cms/models";
|
|||||||
import { UmbNotificationService } from "@umbraco-cms/services";
|
import { UmbNotificationService } from "@umbraco-cms/services";
|
||||||
import { UmbDataStoreBase } from "@umbraco-cms/stores/store";
|
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 _notificationConsumer!:UmbContextConsumer;
|
||||||
protected _notificationService?: UmbNotificationService;
|
protected _notificationService?: UmbNotificationService;
|
||||||
|
|
||||||
protected _storeConsumer!:UmbContextConsumer;
|
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;
|
protected _dataObserver?:Subscription;
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ export abstract class UmbWorkspaceWithStoreContext<T extends DocumentDetails> ex
|
|||||||
public entityKey:string;
|
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)
|
super(target, defaultData)
|
||||||
this.entityType = entityType;
|
this.entityType = entityType;
|
||||||
this.entityKey = entityKey;
|
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:
|
// 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;
|
this._store = _instance;
|
||||||
if(!this._store) {
|
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.
|
// 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;
|
return this._store;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { map, Observable } from 'rxjs';
|
import { map, Observable } from 'rxjs';
|
||||||
import { UmbDataStoreBase } from '../store';
|
import { UmbContentStoreBase } from '../store';
|
||||||
import type { DocumentDetails } from '@umbraco-cms/models';
|
import type { DocumentDetails } from '@umbraco-cms/models';
|
||||||
import { ApiError, DocumentResource, DocumentTreeItem, FolderTreeItem, ProblemDetails } from '@umbraco-cms/backend-api';
|
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>}
|
* @extends {UmbDocumentStoreBase<DocumentDetails | DocumentTreeItem>}
|
||||||
* @description - Data Store for Documents
|
* @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> {
|
getByKey(key: string): Observable<DocumentDetails | null> {
|
||||||
// TODO: use backend cli when available.
|
// TODO: use backend cli when available.
|
||||||
fetch(`/umbraco/management/api/v1/document/details/${key}`)
|
fetch(`/umbraco/management/api/v1/document/details/${key}`)
|
||||||
|
|||||||
@@ -73,3 +73,23 @@ export abstract class UmbDataStoreBase<T extends UmbDataStoreIdentifiers> implem
|
|||||||
this._items.next([...storedItems]);
|
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>
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user