From dc6ec721187196612f1cf6701d51b3dba0c64841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Mon, 9 Jan 2023 20:47:29 +0100 Subject: [PATCH] Simplify update Items --- .../src/core/stores/store.ts | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) 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 b77b702aef..da1a8c5ec1 100644 --- a/src/Umbraco.Web.UI.Client/src/core/stores/store.ts +++ b/src/Umbraco.Web.UI.Client/src/core/stores/store.ts @@ -40,6 +40,8 @@ export abstract class UmbDataStoreBase implem this._items.next(remainingItems); } + + /** * @description - Update the store with new items. Existing items are updated, new items are added. Existing items are matched by the compareKey. * @param {Array} items @@ -47,27 +49,17 @@ export abstract class UmbDataStoreBase implem * @memberof UmbDataStoreBase */ public updateItems(items: Array, compareKey: keyof T = 'key'): void { - const storedItems = [...this._items.getValue()]; - - items.forEach((item) => { - const index = storedItems.map((storedItem) => storedItem[compareKey]).indexOf(item[compareKey]); - - // If the node is in the store, update it - if (index !== -1) { - const storedItem = storedItems[index]; - - for (const [key] of Object.entries(item)) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - //@ts-ignore - storedItem[key] = item[key]; - } + const newData = [...this._items.getValue()]; + items.forEach((newItem) => { + const storedItemIndex = newData.findIndex((item) => item[compareKey] === newItem[compareKey]); + if(storedItemIndex !== -1) { + newData[storedItemIndex] = newItem; } else { - // If the node is not in the store, add it - storedItems.push(item); + newData.push(newItem); } }); - this._items.next([...storedItems]); + this._items.next(newData); } }