|
|
@@ -1,17 +1,8 @@
|
|
|
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
|
|
-import { FormControl } from '@angular/forms';
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
-import { combineLatest, Observable, of } from 'rxjs';
|
|
|
-import {
|
|
|
- debounceTime,
|
|
|
- distinctUntilChanged,
|
|
|
- map,
|
|
|
- startWith,
|
|
|
- switchMap,
|
|
|
- takeUntil,
|
|
|
- tap,
|
|
|
-} from 'rxjs/operators';
|
|
|
-import { GetCollectionContents, GetCollectionList } from 'shared/generated-types';
|
|
|
+import { combineLatest, Observable } from 'rxjs';
|
|
|
+import { distinctUntilChanged, map } from 'rxjs/operators';
|
|
|
+import { GetCollectionList } from 'shared/generated-types';
|
|
|
|
|
|
import { BaseListComponent } from '../../../common/base-list.component';
|
|
|
import { _ } from '../../../core/providers/i18n/mark-for-extraction';
|
|
|
@@ -28,13 +19,8 @@ import { RearrangeEvent } from '../collection-tree/collection-tree.component';
|
|
|
export class CollectionListComponent
|
|
|
extends BaseListComponent<GetCollectionList.Query, GetCollectionList.Items>
|
|
|
implements OnInit {
|
|
|
- contents$: Observable<GetCollectionContents.Items[]>;
|
|
|
- contentsTotalItems$: Observable<number>;
|
|
|
- contentsItemsPerPage$: Observable<number>;
|
|
|
- contentsCurrentPage$: Observable<number>;
|
|
|
activeCollectionId$: Observable<string | null>;
|
|
|
activeCollectionTitle$: Observable<string>;
|
|
|
- filterTermControl = new FormControl('');
|
|
|
|
|
|
constructor(
|
|
|
private dataService: DataService,
|
|
|
@@ -56,50 +42,16 @@ export class CollectionListComponent
|
|
|
map(pm => pm.get('contents')),
|
|
|
distinctUntilChanged(),
|
|
|
);
|
|
|
- this.contentsCurrentPage$ = this.route.paramMap.pipe(
|
|
|
- map(qpm => qpm.get('contentsPage')),
|
|
|
- map(page => (!page ? 1 : +page)),
|
|
|
- startWith(1),
|
|
|
- distinctUntilChanged(),
|
|
|
- );
|
|
|
- this.contentsItemsPerPage$ = this.route.paramMap.pipe(
|
|
|
- map(qpm => qpm.get('contentsPerPage')),
|
|
|
- map(perPage => (!perPage ? 10 : +perPage)),
|
|
|
- startWith(10),
|
|
|
- distinctUntilChanged(),
|
|
|
- );
|
|
|
|
|
|
- const filterTerm$ = this.filterTermControl.valueChanges.pipe(
|
|
|
- debounceTime(250),
|
|
|
- tap(() => this.setContentsPageNumber(1)),
|
|
|
- startWith(''),
|
|
|
- );
|
|
|
-
|
|
|
- const collection$ = combineLatest(
|
|
|
- this.activeCollectionId$,
|
|
|
- this.contentsCurrentPage$,
|
|
|
- this.contentsItemsPerPage$,
|
|
|
- filterTerm$,
|
|
|
- ).pipe(
|
|
|
- takeUntil(this.destroy$),
|
|
|
- switchMap(([id, currentPage, itemsPerPage, filterTerm]) => {
|
|
|
+ this.activeCollectionTitle$ = combineLatest(this.activeCollectionId$, this.items$).pipe(
|
|
|
+ map(([id, collections]) => {
|
|
|
if (id) {
|
|
|
- const take = itemsPerPage;
|
|
|
- const skip = (currentPage - 1) * itemsPerPage;
|
|
|
- return this.dataService.collection
|
|
|
- .getCollectionContents(id, take, skip, filterTerm)
|
|
|
- .mapSingle(data => data.collection);
|
|
|
- } else {
|
|
|
- return of(null);
|
|
|
+ const match = collections.find(c => c.id === id);
|
|
|
+ return match ? match.name : '';
|
|
|
}
|
|
|
+ return '';
|
|
|
}),
|
|
|
);
|
|
|
-
|
|
|
- this.contents$ = collection$.pipe(map(result => (result ? result.productVariants.items : [])));
|
|
|
- this.contentsTotalItems$ = collection$.pipe(
|
|
|
- map(result => (result ? result.productVariants.totalItems : 0)),
|
|
|
- );
|
|
|
- this.activeCollectionTitle$ = collection$.pipe(map(result => (result ? result.name : '')));
|
|
|
}
|
|
|
|
|
|
onRearrange(event: RearrangeEvent) {
|
|
|
@@ -114,22 +66,9 @@ export class CollectionListComponent
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- setContentsPageNumber(page: number) {
|
|
|
- this.setParam('contentsPage', page);
|
|
|
- }
|
|
|
-
|
|
|
- setContentsItemsPerPage(perPage: number) {
|
|
|
- this.setParam('contentsPerPage', perPage);
|
|
|
- }
|
|
|
-
|
|
|
closeContents() {
|
|
|
- this.setParam('contents', null);
|
|
|
- }
|
|
|
-
|
|
|
- private setParam(key: string, value: any) {
|
|
|
- this.router.navigate(['./', { ...this.route.snapshot.params, [key]: value }], {
|
|
|
- relativeTo: this.route,
|
|
|
- queryParamsHandling: 'merge',
|
|
|
- });
|
|
|
+ const params = { ...this.route.snapshot.params };
|
|
|
+ delete params.contents;
|
|
|
+ this.router.navigate(['./', params], { relativeTo: this.route });
|
|
|
}
|
|
|
}
|