|
|
@@ -1,11 +1,16 @@
|
|
|
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
|
|
|
-import { FormControl } from '@angular/forms';
|
|
|
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
|
|
|
import { PaginationInstance } from 'ngx-pagination';
|
|
|
-import { Observable, Subject } from 'rxjs';
|
|
|
+import { BehaviorSubject, Observable, Subject } from 'rxjs';
|
|
|
import { debounceTime, finalize, map, takeUntil, tap } from 'rxjs/operators';
|
|
|
|
|
|
-import { Asset, GetAssetList, SortOrder } from '../../../common/generated-types';
|
|
|
+import {
|
|
|
+ Asset,
|
|
|
+ GetAssetList,
|
|
|
+ LogicalOperator,
|
|
|
+ SortOrder,
|
|
|
+ TagFragment,
|
|
|
+} from '../../../common/generated-types';
|
|
|
import { DataService } from '../../../data/providers/data.service';
|
|
|
import { QueryResult } from '../../../data/query-result';
|
|
|
import { Dialog } from '../../../providers/modal/modal.service';
|
|
|
@@ -22,6 +27,7 @@ import { NotificationService } from '../../../providers/notification/notificatio
|
|
|
})
|
|
|
export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Asset[]> {
|
|
|
assets$: Observable<GetAssetList.Items[]>;
|
|
|
+ allTags$: Observable<TagFragment[]>;
|
|
|
paginationConfig: PaginationInstance = {
|
|
|
currentPage: 1,
|
|
|
itemsPerPage: 25,
|
|
|
@@ -30,7 +36,8 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
|
|
|
resolveWith: (result?: Asset[]) => void;
|
|
|
selection: Asset[] = [];
|
|
|
- searchTerm = new FormControl('');
|
|
|
+ searchTerm$ = new BehaviorSubject<string | undefined>(undefined);
|
|
|
+ filterByTags$ = new BehaviorSubject<TagFragment[] | undefined>(undefined);
|
|
|
uploading = false;
|
|
|
private listQuery: QueryResult<GetAssetList.Query, GetAssetList.Variables>;
|
|
|
private destroy$ = new Subject<void>();
|
|
|
@@ -39,19 +46,17 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.listQuery = this.dataService.product.getAssetList(this.paginationConfig.itemsPerPage, 0);
|
|
|
+ this.allTags$ = this.dataService.product.getTagList().mapSingle(data => data.tags.items);
|
|
|
this.assets$ = this.listQuery.stream$.pipe(
|
|
|
- tap((result) => (this.paginationConfig.totalItems = result.assets.totalItems)),
|
|
|
- map((result) => result.assets.items),
|
|
|
+ tap(result => (this.paginationConfig.totalItems = result.assets.totalItems)),
|
|
|
+ map(result => result.assets.items),
|
|
|
);
|
|
|
- this.searchTerm.valueChanges
|
|
|
- .pipe(debounceTime(250), takeUntil(this.destroy$))
|
|
|
- .subscribe((searchTerm) => {
|
|
|
- this.fetchPage(
|
|
|
- this.paginationConfig.currentPage,
|
|
|
- this.paginationConfig.itemsPerPage,
|
|
|
- searchTerm,
|
|
|
- );
|
|
|
- });
|
|
|
+ this.searchTerm$.pipe(debounceTime(250), takeUntil(this.destroy$)).subscribe(() => {
|
|
|
+ this.fetchPage(this.paginationConfig.currentPage, this.paginationConfig.itemsPerPage);
|
|
|
+ });
|
|
|
+ this.filterByTags$.pipe(takeUntil(this.destroy$)).subscribe(() => {
|
|
|
+ this.fetchPage(this.paginationConfig.currentPage, this.paginationConfig.itemsPerPage);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
@@ -61,20 +66,12 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
|
|
|
pageChange(page: number) {
|
|
|
this.paginationConfig.currentPage = page;
|
|
|
- this.fetchPage(
|
|
|
- this.paginationConfig.currentPage,
|
|
|
- this.paginationConfig.itemsPerPage,
|
|
|
- this.searchTerm.value,
|
|
|
- );
|
|
|
+ this.fetchPage(this.paginationConfig.currentPage, this.paginationConfig.itemsPerPage);
|
|
|
}
|
|
|
|
|
|
itemsPerPageChange(itemsPerPage: number) {
|
|
|
this.paginationConfig.itemsPerPage = itemsPerPage;
|
|
|
- this.fetchPage(
|
|
|
- this.paginationConfig.currentPage,
|
|
|
- this.paginationConfig.itemsPerPage,
|
|
|
- this.searchTerm.value,
|
|
|
- );
|
|
|
+ this.fetchPage(this.paginationConfig.currentPage, this.paginationConfig.itemsPerPage);
|
|
|
}
|
|
|
|
|
|
cancel() {
|
|
|
@@ -91,12 +88,8 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
this.dataService.product
|
|
|
.createAssets(files)
|
|
|
.pipe(finalize(() => (this.uploading = false)))
|
|
|
- .subscribe((res) => {
|
|
|
- this.fetchPage(
|
|
|
- this.paginationConfig.currentPage,
|
|
|
- this.paginationConfig.itemsPerPage,
|
|
|
- this.searchTerm.value,
|
|
|
- );
|
|
|
+ .subscribe(res => {
|
|
|
+ this.fetchPage(this.paginationConfig.currentPage, this.paginationConfig.itemsPerPage);
|
|
|
this.notificationService.success(_('asset.notify-create-assets-success'), {
|
|
|
count: files.length,
|
|
|
});
|
|
|
@@ -104,9 +97,11 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fetchPage(currentPage: number, itemsPerPage: number, searchTerm?: string) {
|
|
|
+ private fetchPage(currentPage: number, itemsPerPage: number) {
|
|
|
const take = +itemsPerPage;
|
|
|
const skip = (currentPage - 1) * +itemsPerPage;
|
|
|
+ const searchTerm = this.searchTerm$.value;
|
|
|
+ const tags = this.filterByTags$.value?.map(t => t.value);
|
|
|
this.listQuery.ref.refetch({
|
|
|
options: {
|
|
|
skip,
|
|
|
@@ -119,6 +114,8 @@ export class AssetPickerDialogComponent implements OnInit, OnDestroy, Dialog<Ass
|
|
|
sort: {
|
|
|
createdAt: SortOrder.DESC,
|
|
|
},
|
|
|
+ tags,
|
|
|
+ tagsOperator: LogicalOperator.AND,
|
|
|
},
|
|
|
});
|
|
|
}
|