Browse Source

feat(admin-ui): Implement content preview when creating collection

Relates to #1530
Michael Bromley 3 years ago
parent
commit
1e4f07226b

+ 30 - 29
packages/admin-ui/src/lib/catalog/src/components/collection-contents/collection-contents.component.ts

@@ -16,9 +16,8 @@ import {
     ConfigurableOperationInput,
     DataService,
     GetCollectionContents,
-    LocalStorageService,
 } from '@vendure/admin-ui/core';
-import { BehaviorSubject, combineLatest, Observable, of, onErrorResumeNext, Subject } from 'rxjs';
+import { BehaviorSubject, combineLatest, Observable, of, Subject } from 'rxjs';
 import {
     catchError,
     debounceTime,
@@ -26,8 +25,6 @@ import {
     filter,
     finalize,
     map,
-    retry,
-    retryWhen,
     startWith,
     switchMap,
     takeUntil,
@@ -42,6 +39,7 @@ import {
 })
 export class CollectionContentsComponent implements OnInit, OnChanges, OnDestroy {
     @Input() collectionId: string;
+    @Input() parentId: string;
     @Input() updatedFilters: ConfigurableOperationInput[] | undefined;
     @Input() previewUpdatedFilters = false;
     @ContentChild(TemplateRef, { static: true }) headerTemplate: TemplateRef<any>;
@@ -53,6 +51,7 @@ export class CollectionContentsComponent implements OnInit, OnChanges, OnDestroy
     filterTermControl = new FormControl('');
     isLoading = false;
     private collectionIdChange$ = new BehaviorSubject<string>('');
+    private parentIdChange$ = new BehaviorSubject<string>('');
     private filterChanges$ = new BehaviorSubject<ConfigurableOperationInput[]>([]);
     private refresh$ = new BehaviorSubject<boolean>(true);
     private destroy$ = new Subject<void>();
@@ -88,6 +87,7 @@ export class CollectionContentsComponent implements OnInit, OnChanges, OnDestroy
 
         const fetchUpdate$ = combineLatest(
             this.collectionIdChange$,
+            this.parentIdChange$,
             this.contentsCurrentPage$,
             this.contentsItemsPerPage$,
             filterTerm$,
@@ -99,33 +99,31 @@ export class CollectionContentsComponent implements OnInit, OnChanges, OnDestroy
             takeUntil(this.destroy$),
             tap(() => (this.isLoading = true)),
             debounceTime(50),
-            switchMap(([id, currentPage, itemsPerPage, filterTerm, filters]) => {
+            switchMap(([id, parentId, currentPage, itemsPerPage, filterTerm, filters]) => {
                 const take = itemsPerPage;
                 const skip = (currentPage - 1) * itemsPerPage;
-                if (id) {
-                    if (filters.length && this.previewUpdatedFilters) {
-                        const filterClause = filterTerm
-                            ? ({ name: { contains: filterTerm } } as CollectionFilterParameter)
-                            : undefined;
-                        return this.dataService.collection
-                            .previewCollectionVariants(
-                                {
-                                    collectionId: id,
-                                    filters,
-                                },
-                                {
-                                    take,
-                                    skip,
-                                    filter: filterClause,
-                                },
-                            )
-                            .mapSingle(data => data.previewCollectionVariants)
-                            .pipe(catchError(() => of({ items: [], totalItems: 0 })));
-                    } else {
-                        return this.dataService.collection
-                            .getCollectionContents(id, take, skip, filterTerm)
-                            .mapSingle(data => data.collection?.productVariants);
-                    }
+                if (filters.length && this.previewUpdatedFilters) {
+                    const filterClause = filterTerm
+                        ? ({ name: { contains: filterTerm } } as CollectionFilterParameter)
+                        : undefined;
+                    return this.dataService.collection
+                        .previewCollectionVariants(
+                            {
+                                parentId,
+                                filters,
+                            },
+                            {
+                                take,
+                                skip,
+                                filter: filterClause,
+                            },
+                        )
+                        .mapSingle(data => data.previewCollectionVariants)
+                        .pipe(catchError(() => of({ items: [], totalItems: 0 })));
+                } else if (id) {
+                    return this.dataService.collection
+                        .getCollectionContents(id, take, skip, filterTerm)
+                        .mapSingle(data => data.collection?.productVariants);
                 } else {
                     return of(null);
                 }
@@ -142,6 +140,9 @@ export class CollectionContentsComponent implements OnInit, OnChanges, OnDestroy
         if ('collectionId' in changes) {
             this.collectionIdChange$.next(changes.collectionId.currentValue);
         }
+        if ('parentId' in changes) {
+            this.parentIdChange$.next(changes.parentId.currentValue);
+        }
         if ('updatedFilters' in changes) {
             if (this.updatedFilters) {
                 this.filterChanges$.next(this.updatedFilters);

+ 3 - 2
packages/admin-ui/src/lib/catalog/src/components/collection-detail/collection-detail.component.html

@@ -106,9 +106,9 @@
     <div class="clr-row" formArrayName="filters">
         <div class="clr-col">
             <label>{{ 'catalog.filters' | translate }}</label>
-            <ng-container *ngFor="let filter of filters; index as i">
+            <ng-container *ngFor="let filter of filters; index as i; trackBy:trackByFn">
                 <vdr-configurable-input
-                    (remove)="removeFilter($event)"
+                    (remove)="removeFilter(i)"
                     [operation]="filter"
                     [operationDefinition]="getFilterDefinition(filter)"
                     [formControlName]="i"
@@ -138,6 +138,7 @@
         <div class="clr-col">
             <vdr-collection-contents
                 [collectionId]="id"
+                [parentId]="parentId$ | async"
                 [updatedFilters]="updatedFilters$ | async"
                 [previewUpdatedFilters]="livePreview"
                 #collectionContents

+ 46 - 28
packages/admin-ui/src/lib/catalog/src/components/collection-detail/collection-detail.component.ts

@@ -33,8 +33,8 @@ import {
     UpdateCollectionInput,
 } from '@vendure/admin-ui/core';
 import { normalizeString } from '@vendure/common/lib/normalize-string';
-import { combineLatest, Observable } from 'rxjs';
-import { debounceTime, filter, map, mergeMap, take } from 'rxjs/operators';
+import { combineLatest, Observable, of } from 'rxjs';
+import { debounceTime, filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
 
 import { CollectionContentsComponent } from '../collection-contents/collection-contents.component';
 
@@ -55,6 +55,7 @@ export class CollectionDetailComponent
     allFilters: ConfigurableOperationDefinition[] = [];
     updatedFilters$: Observable<ConfigurableOperationInput[]>;
     livePreview = false;
+    parentId$: Observable<string | undefined>;
     readonly updatePermission = [Permission.UpdateCatalog, Permission.UpdateCollection];
     @ViewChild('collectionContents') contentsComponent: CollectionContentsComponent;
 
@@ -95,6 +96,16 @@ export class CollectionDetailComponent
             filter(() => filtersFormArray.touched),
             map(status => this.mapOperationsToInputs(this.filters, filtersFormArray.value)),
         );
+        this.parentId$ = this.route.paramMap.pipe(
+            map(pm => pm.get('parentId') || undefined),
+            switchMap(parentId => {
+                if (parentId) {
+                    return of(parentId);
+                } else {
+                    return this.entity$.pipe(map(collection => collection.parent?.id));
+                }
+            }),
+        );
     }
 
     ngOnDestroy() {
@@ -127,31 +138,27 @@ export class CollectionDetailComponent
 
     addFilter(collectionFilter: ConfigurableOperation) {
         const filtersArray = this.detailForm.get('filters') as FormArray;
-        const index = filtersArray.value.findIndex(o => o.code === collectionFilter.code);
-        if (index === -1) {
-            const argsHash = collectionFilter.args.reduce(
-                (output, arg) => ({
-                    ...output,
-                    [arg.name]: getConfigArgValue(arg.value),
-                }),
-                {},
-            );
-            filtersArray.push(
-                this.formBuilder.control({
-                    code: collectionFilter.code,
-                    args: argsHash,
-                }),
-            );
-            this.filters.push({
+        const argsHash = collectionFilter.args.reduce(
+            (output, arg) => ({
+                ...output,
+                [arg.name]: getConfigArgValue(arg.value),
+            }),
+            {},
+        );
+        filtersArray.push(
+            this.formBuilder.control({
                 code: collectionFilter.code,
-                args: collectionFilter.args.map(a => ({ name: a.name, value: getConfigArgValue(a.value) })),
-            });
-        }
+                args: argsHash,
+            }),
+        );
+        this.filters.push({
+            code: collectionFilter.code,
+            args: collectionFilter.args.map(a => ({ name: a.name, value: getConfigArgValue(a.value) })),
+        });
     }
 
-    removeFilter(collectionFilter: ConfigurableOperation) {
+    removeFilter(index: number) {
         const filtersArray = this.detailForm.get('filters') as FormArray;
-        const index = filtersArray.value.findIndex(o => o.code === collectionFilter.code);
         if (index !== -1) {
             filtersArray.removeAt(index);
             this.filters.splice(index, 1);
@@ -236,6 +243,10 @@ export class CollectionDetailComponent
         this.localStorageService.set('livePreviewCollectionContents', this.livePreview);
     }
 
+    trackByFn(index: number, item: ConfigurableOperation) {
+        return JSON.stringify(item);
+    }
+
     /**
      * Sets the values of the form on changes to the category or current language.
      */
@@ -249,7 +260,12 @@ export class CollectionDetailComponent
             visible: !entity.isPrivate,
         });
 
-        entity.filters.forEach(f => this.addFilter(f));
+        const formArray = this.detailForm.get('filters') as FormArray;
+        if (formArray.length !== entity.filters.length) {
+            formArray.clear();
+            this.filters = [];
+            entity.filters.forEach(f => this.addFilter(f));
+        }
 
         if (this.customFields.length) {
             this.setCustomFieldFormValues(
@@ -301,10 +317,12 @@ export class CollectionDetailComponent
         return operations.map((o, i) => {
             return {
                 code: o.code,
-                arguments: Object.values(formValueOperations[i].args).map((value: any, j) => ({
-                    name: o.args[j].name,
-                    value: encodeConfigArgValue(value),
-                })),
+                arguments: Object.entries(formValueOperations[i].args).map(([name, value], j) => {
+                    return {
+                        name,
+                        value: encodeConfigArgValue(value),
+                    };
+                }),
             };
         });
     }

+ 1 - 1
packages/admin-ui/src/lib/core/src/common/generated-types.ts

@@ -3791,7 +3791,7 @@ export type PermissionDefinition = {
 };
 
 export type PreviewCollectionVariantsInput = {
-  collectionId: Scalars['ID'];
+  parentId?: Maybe<Scalars['ID']>;
   filters: Array<ConfigurableOperationInput>;
 };