diff --git a/src/Umbraco.Web.UI.Client/src/core/stores/property-editor-config/property-editor-config.store.ts b/src/Umbraco.Web.UI.Client/src/core/stores/property-editor-config/property-editor-config.store.ts index 53932e2819..7bec2f5dd1 100644 --- a/src/Umbraco.Web.UI.Client/src/core/stores/property-editor-config/property-editor-config.store.ts +++ b/src/Umbraco.Web.UI.Client/src/core/stores/property-editor-config/property-editor-config.store.ts @@ -1,24 +1,20 @@ -import { BehaviorSubject, map, Observable } from 'rxjs'; +import { map, Observable } from 'rxjs'; import { getPropertyEditorConfig } from '../../api/fetcher'; import type { PropertyEditorConfig } from '../../models'; +import { UmbDataStoreBase } from '../store'; export interface PropertyEditorConfigRef { propertyEditorAlias: string; config: PropertyEditorConfig; } -export class UmbPropertyEditorConfigStore { - private _items: BehaviorSubject> = new BehaviorSubject( - >[] - ); - public readonly items: Observable> = this._items.asObservable(); - +export class UmbPropertyEditorConfigStore extends UmbDataStoreBase { getByAlias(alias: string): Observable { // TODO: only fetch if the data type is not in the store? getPropertyEditorConfig({ propertyEditorAlias: alias }) .then((res) => { const propertyEditorConfigRef: PropertyEditorConfigRef = { propertyEditorAlias: alias, config: res.data }; - this.update([propertyEditorConfigRef]); + this.update([propertyEditorConfigRef], 'propertyEditorAlias'); }) .catch((err) => { console.log(err); @@ -26,38 +22,4 @@ export class UmbPropertyEditorConfigStore { return this.items.pipe(map((items) => items.find((item) => item.propertyEditorAlias === alias))); } - - public update(updatedItems: Array) { - const storedItems = this._items.getValue(); - const updated: PropertyEditorConfigRef[] = [...storedItems]; - - updatedItems.forEach((updatedItem) => { - const index = storedItems - .map((storedItem) => storedItem.propertyEditorAlias) - .indexOf(updatedItem.propertyEditorAlias); - - if (index !== -1) { - const itemKeys = Object.keys(storedItems[index]); - - const newItem = {}; - - for (const [key] of Object.entries(updatedItem)) { - if (itemKeys.indexOf(key) !== -1) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - newItem[key] = updatedItem[key]; - } - } - - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - updated[index] = newItem; - } else { - // If the node is not in the store, add it - updated.push(updatedItem); - } - }); - - this._items.next([...updated]); - } } diff --git a/src/Umbraco.Web.UI.Client/src/core/stores/property-editor/property-editor.store.ts b/src/Umbraco.Web.UI.Client/src/core/stores/property-editor/property-editor.store.ts index 1603e13a41..bc3942b0b2 100644 --- a/src/Umbraco.Web.UI.Client/src/core/stores/property-editor/property-editor.store.ts +++ b/src/Umbraco.Web.UI.Client/src/core/stores/property-editor/property-editor.store.ts @@ -1,16 +1,14 @@ -import { BehaviorSubject, map, Observable } from 'rxjs'; +import { map, Observable } from 'rxjs'; import type { PropertyEditor } from '../../models'; import { getPropertyEditorsList, getPropertyEditor } from '../../api/fetcher'; +import { UmbDataStoreBase } from '../store'; -export class UmbPropertyEditorStore { - private _items: BehaviorSubject> = new BehaviorSubject(>[]); - public readonly items: Observable> = this._items.asObservable(); - +export class UmbPropertyEditorStore extends UmbDataStoreBase { getAll(): Observable | []> { // TODO: only fetch if the data type is not in the store? getPropertyEditorsList({}) .then((res) => { - this._items.next(res.data.propertyEditors); + this.update(res.data.propertyEditors, 'alias'); }) .catch((err) => { console.log(err); @@ -23,7 +21,7 @@ export class UmbPropertyEditorStore { // TODO: only fetch if the data type is not in the store? getPropertyEditor({ propertyEditorAlias: alias }) .then((res) => { - this.update([res.data]); + this.update([res.data], 'alias'); }) .catch((err) => { console.log(err); @@ -31,36 +29,4 @@ export class UmbPropertyEditorStore { return this.items.pipe(map((items) => items.find((item) => item.alias === alias))); } - - public update(updatedItems: Array) { - const storedItems = this._items.getValue(); - const updated: PropertyEditor[] = [...storedItems]; - - updatedItems.forEach((updatedItem) => { - const index = storedItems.map((storedItem) => storedItem.alias).indexOf(updatedItem.alias); - - if (index !== -1) { - const itemKeys = Object.keys(storedItems[index]); - - const newItem = {}; - - for (const [key] of Object.entries(updatedItem)) { - if (itemKeys.indexOf(key) !== -1) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - newItem[key] = updatedItem[key]; - } - } - - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - updated[index] = newItem; - } else { - // If the node is not in the store, add it - updated.push(updatedItem); - } - }); - - this._items.next([...updated]); - } } diff --git a/src/Umbraco.Web.UI.Client/src/core/stores/store.ts b/src/Umbraco.Web.UI.Client/src/core/stores/store.ts index 10f842496e..bc20672373 100644 --- a/src/Umbraco.Web.UI.Client/src/core/stores/store.ts +++ b/src/Umbraco.Web.UI.Client/src/core/stores/store.ts @@ -1,21 +1,25 @@ import { BehaviorSubject, Observable } from 'rxjs'; -import { Entity } from '../../mocks/data/entities'; + +export interface UmbDataStoreIdentifiers { + key?: string; + [more: string]: any; +} export interface UmbDataStore { readonly items: Observable>; update(items: Array): void; } -export class UmbDataStoreBase implements UmbDataStore { +export class UmbDataStoreBase implements UmbDataStore { private _items: BehaviorSubject> = new BehaviorSubject(>[]); public readonly items: Observable> = this._items.asObservable(); - public update(updatedItems: Array) { + public update(updatedItems: Array, compareKey: keyof T = 'key'): void { const storedItems = this._items.getValue(); const updated: T[] = [...storedItems]; updatedItems.forEach((updatedItem) => { - const index = storedItems.map((storedItem) => storedItem.key).indexOf(updatedItem.key); + const index = storedItems.map((storedItem) => storedItem[compareKey]).indexOf(updatedItem[compareKey]); if (index !== -1) { const itemKeys = Object.keys(storedItems[index]);