Browse Source

fix(admin-ui): Refresh Collection List on deletion

Relates to #295
Michael Bromley 5 years ago
parent
commit
42023980c3

+ 18 - 11
packages/admin-ui/src/lib/catalog/src/components/collection-list/collection-list.component.ts

@@ -1,10 +1,15 @@
 import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
 import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
 import { ActivatedRoute, Router } from '@angular/router';
 import { ActivatedRoute, Router } from '@angular/router';
 import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
 import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
-import { GetCollectionList } from '@vendure/admin-ui/core';
-import { DataService, ModalService, NotificationService, QueryResult } from '@vendure/admin-ui/core';
+import {
+    DataService,
+    GetCollectionList,
+    ModalService,
+    NotificationService,
+    QueryResult,
+} from '@vendure/admin-ui/core';
 import { combineLatest, EMPTY, Observable } from 'rxjs';
 import { combineLatest, EMPTY, Observable } from 'rxjs';
-import { distinctUntilChanged, map, switchMap, take } from 'rxjs/operators';
+import { distinctUntilChanged, map, shareReplay, switchMap, take } from 'rxjs/operators';
 
 
 import { RearrangeEvent } from '../collection-tree/collection-tree.component';
 import { RearrangeEvent } from '../collection-tree/collection-tree.component';
 
 
@@ -31,16 +36,16 @@ export class CollectionListComponent implements OnInit {
 
 
     ngOnInit() {
     ngOnInit() {
         this.queryResult = this.dataService.collection.getCollections(99999, 0).refetchOnChannelChange();
         this.queryResult = this.dataService.collection.getCollections(99999, 0).refetchOnChannelChange();
-        this.items$ = this.queryResult.mapStream(data => data.collections.items);
+        this.items$ = this.queryResult.mapStream((data) => data.collections.items).pipe(shareReplay(1));
         this.activeCollectionId$ = this.route.paramMap.pipe(
         this.activeCollectionId$ = this.route.paramMap.pipe(
-            map(pm => pm.get('contents')),
+            map((pm) => pm.get('contents')),
             distinctUntilChanged(),
             distinctUntilChanged(),
         );
         );
 
 
         this.activeCollectionTitle$ = combineLatest(this.activeCollectionId$, this.items$).pipe(
         this.activeCollectionTitle$ = combineLatest(this.activeCollectionId$, this.items$).pipe(
             map(([id, collections]) => {
             map(([id, collections]) => {
                 if (id) {
                 if (id) {
-                    const match = collections.find(c => c.id === id);
+                    const match = collections.find((c) => c.id === id);
                     return match ? match.name : '';
                     return match ? match.name : '';
                 }
                 }
                 return '';
                 return '';
@@ -54,7 +59,7 @@ export class CollectionListComponent implements OnInit {
                 this.notificationService.success(_('common.notify-saved-changes'));
                 this.notificationService.success(_('common.notify-saved-changes'));
                 this.refresh();
                 this.refresh();
             },
             },
-            error: err => {
+            error: (err) => {
                 this.notificationService.error(_('common.notify-save-changes-error'));
                 this.notificationService.error(_('common.notify-save-changes-error'));
             },
             },
         });
         });
@@ -64,8 +69,8 @@ export class CollectionListComponent implements OnInit {
         this.items$
         this.items$
             .pipe(
             .pipe(
                 take(1),
                 take(1),
-                map(items => -1 < items.findIndex(i => i.parent && i.parent.id === id)),
-                switchMap(hasChildren => {
+                map((items) => -1 < items.findIndex((i) => i.parent && i.parent.id === id)),
+                switchMap((hasChildren) => {
                     return this.modalService.dialog({
                     return this.modalService.dialog({
                         title: _('catalog.confirm-delete-collection'),
                         title: _('catalog.confirm-delete-collection'),
                         body: hasChildren
                         body: hasChildren
@@ -77,7 +82,9 @@ export class CollectionListComponent implements OnInit {
                         ],
                         ],
                     });
                     });
                 }),
                 }),
-                switchMap(response => (response ? this.dataService.collection.deleteCollection(id) : EMPTY)),
+                switchMap((response) =>
+                    response ? this.dataService.collection.deleteCollection(id) : EMPTY,
+                ),
             )
             )
             .subscribe(
             .subscribe(
                 () => {
                 () => {
@@ -86,7 +93,7 @@ export class CollectionListComponent implements OnInit {
                     });
                     });
                     this.refresh();
                     this.refresh();
                 },
                 },
-                err => {
+                (err) => {
                     this.notificationService.error(_('common.notify-delete-error'), {
                     this.notificationService.error(_('common.notify-delete-error'), {
                         entity: 'Collection',
                         entity: 'Collection',
                     });
                     });