From 05e631c87e43aa6f8f5a76868f1c0d2de3bb54d6 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 21 Jul 2020 10:59:17 +0300 Subject: [PATCH] feat: create new NavItem instance on add and patch --- .../src/lib/services/nav-items.service.ts | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts index 92b7c26059..d79f293032 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts @@ -14,37 +14,31 @@ export class NavItemsService { return this._items$.asObservable(); } - addItems(items: NavItem[]) { - this._items$.next( - [ - ...this.items, - ...items.map(item => - typeof item.visible === 'undefined' ? { ...item, visible: () => true } : item, - ), - ].sort(sortItems), - ); + addItems(newItems: NavItem[]) { + const items = [...this.items]; + newItems.forEach(item => items.push(new NavItem(item))); + items.sort(sortItems); + this._items$.next(items); } removeItem(id: string | number) { const index = this.items.findIndex(item => item.id === id); - if (index > -1) { - this._items$.next([...this.items.slice(0, index), ...this.items.slice(index + 1)]); - } + if (index < -1) return; + + const items = [...this.items.slice(0, index), ...this.items.slice(index + 1)]; + this._items$.next(items); } patchItem(id: string | number, item: Partial>) { const index = this.items.findIndex(i => i.id === id); - if (index > -1) { - this._items$.next( - [ - ...this.items.slice(0, index), - { ...this.items[index], ...item }, - ...this.items.slice(index + 1), - ].sort(sortItems), - ); - } + if (index < -1) return; + + const items = [...this.items]; + items[index] = new NavItem({ ...items[index], ...item }); + items.sort(sortItems); + this._items$.next(items); } }