Browse Source

fix(admin-ui): Clear data table selection on channel change

Michael Bromley 2 năm trước cách đây
mục cha
commit
34c69ba073

+ 1 - 5
packages/admin-ui/src/lib/catalog/src/components/collection-data-table/collection-data-table.component.ts

@@ -39,7 +39,7 @@ export type CollectionOrderEvent = {
 })
 export class CollectionDataTableComponent
     extends DataTable2Component<CollectionTableItem>
-    implements OnInit, OnChanges, AfterViewInit
+    implements OnChanges, AfterViewInit
 {
     @Input() subCollections: CollectionTableItem[];
     @Output() changeOrder = new EventEmitter<CollectionOrderEvent>();
@@ -59,10 +59,6 @@ export class CollectionDataTableComponent
         super(changeDetectorRef, localStorageService, dataService);
     }
 
-    ngOnInit() {
-        super.ngOnInit();
-    }
-
     ngOnChanges(changes: SimpleChanges) {
         super.ngOnChanges(changes);
         if (changes.subCollections || changes.items) {

+ 17 - 9
packages/admin-ui/src/lib/core/src/shared/components/data-table-2/data-table2.component.ts

@@ -9,7 +9,6 @@ import {
     Input,
     OnChanges,
     OnDestroy,
-    OnInit,
     Output,
     QueryList,
     SimpleChanges,
@@ -17,7 +16,7 @@ import {
 } from '@angular/core';
 import { PaginationService } from 'ngx-pagination';
 import { Observable, Subscription } from 'rxjs';
-import { map } from 'rxjs/operators';
+import { distinctUntilChanged, map } from 'rxjs/operators';
 import { LanguageCode } from '../../../common/generated-types';
 import { DataService } from '../../../data/providers/data.service';
 import { DataTableFilterCollection } from '../../../providers/data-table/data-table-filter-collection';
@@ -94,7 +93,7 @@ import { DataTable2SearchComponent } from './data-table-search.component';
     changeDetection: ChangeDetectionStrategy.OnPush,
     providers: [PaginationService],
 })
-export class DataTable2Component<T> implements AfterContentInit, OnChanges, OnInit, OnDestroy {
+export class DataTable2Component<T> implements AfterContentInit, OnChanges, OnDestroy {
     @Input() id: string;
     @Input() items: T[];
     @Input() itemsPerPage: number;
@@ -173,12 +172,6 @@ export class DataTable2Component<T> implements AfterContentInit, OnChanges, OnIn
         }
     };
 
-    ngOnInit() {
-        this.subscription = this.selectionManager?.selectionChanges$.subscribe(() =>
-            this.changeDetectorRef.markForCheck(),
-        );
-    }
-
     ngOnChanges(changes: SimpleChanges) {
         if (changes.items) {
             this.currentStart = this.itemsPerPage * (this.currentPage - 1);
@@ -231,6 +224,21 @@ export class DataTable2Component<T> implements AfterContentInit, OnChanges, OnIn
         this.columns.changes.subscribe(() => {
             this.changeDetectorRef.markForCheck();
         });
+
+        this.subscription = this.selectionManager?.selectionChanges$.subscribe(() =>
+            this.changeDetectorRef.markForCheck(),
+        );
+
+        if (this.selectionManager && this.subscription) {
+            const channelSub = this.dataService.client
+                .userStatus()
+                .mapStream(({ userStatus }) => userStatus.activeChannelId)
+                .pipe(distinctUntilChanged())
+                .subscribe(activeChannelId => {
+                    this.selectionManager?.clearSelection();
+                });
+            this.subscription?.add(channelSub);
+        }
     }
 
     onColumnReorder(event: { column: DataTable2ColumnComponent<any>; newIndex: number }) {