nodes.store

This commit is contained in:
Niels Lyngsø
2022-05-31 12:01:48 +02:00
parent cf12e684a9
commit 41d5549005
3 changed files with 10 additions and 9 deletions

View File

@@ -2,7 +2,7 @@ import { css, html, LitElement } from 'lit';
import { UUITextStyles } from '@umbraco-ui/uui-css/lib';
import { customElement, property, state } from 'lit/decorators.js';
import { UmbContextConsumerMixin } from '../core/context';
import { UmbContentService } from './content.service';
import { UmbNodesStore } from '../core/stores/nodes.store';
import { Subscription } from 'rxjs';
import { DocumentNode } from '../mocks/data/content.data';
@@ -51,13 +51,13 @@ class UmbContentEditor extends UmbContextConsumerMixin(LitElement) {
@state()
_node?: DocumentNode;
private _contentService?: UmbContentService;
private _contentService?: UmbNodesStore;
private _nodeSubscription?: Subscription;
constructor () {
super();
this.consumeContext('umbContentService', (contentService: UmbContentService) => {
this.consumeContext('umbContentService', (contentService: UmbNodesStore) => {
this._contentService = contentService;
this._useNode();
});
@@ -75,7 +75,7 @@ class UmbContentEditor extends UmbContextConsumerMixin(LitElement) {
this._nodeSubscription?.unsubscribe();
this._nodeSubscription = this._contentService?.getById(parseInt(this.id)).subscribe(node => {
if (!node) return;
if (!node) return; // TODO: Handle nicely if there is no node.
this._node = node;
});
}

View File

@@ -3,7 +3,7 @@ import { css, html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import { UmbContextConsumerMixin, UmbContextProviderMixin } from '../core/context';
import { UmbRouteLocation, UmbRouter } from '../core/router';
import { UmbContentService } from './content.service';
import { UmbNodesStore } from '../core/stores/nodes.store';
import { Subscription } from 'rxjs';
import './content-tree.element';
@@ -30,7 +30,7 @@ export class UmbContentSection extends UmbContextProviderMixin(UmbContextConsume
constructor () {
super();
this.provideContext('umbContentService', new UmbContentService());
this.provideContext('umbContentService', new UmbNodesStore());
this.consumeContext('umbRouter', (_instance: UmbRouter) => {
this._router = _instance;

View File

@@ -1,7 +1,8 @@
import { BehaviorSubject, map, Observable } from 'rxjs';
import { DocumentNode } from '../mocks/data/content.data';
import { DocumentNode } from '../../mocks/data/content.data';
export class UmbNodesStore {
export class UmbContentService {
private _nodes: BehaviorSubject<Array<DocumentNode>> = new BehaviorSubject(<Array<DocumentNode>>[]);
public readonly nodes: Observable<Array<DocumentNode>> = this._nodes.asObservable();
@@ -16,7 +17,7 @@ export class UmbContentService {
return this.nodes.pipe(map(((nodes: Array<DocumentNode>) => nodes.find((node: DocumentNode) => node.id === id) || null)));
}
_updateStore (fetchedNodes: Array<any>) {
private _updateStore (fetchedNodes: Array<any>) {
const storedNodes = this._nodes.getValue();
let updated: any = [...storedNodes];