Skip to content

Commit

Permalink
refactor(admin-ui): Change the output of ProductAssetComponent
Browse files Browse the repository at this point in the history
Output the entire Asset object rather than just the ID
  • Loading branch information
michaelbromley committed Jan 15, 2021
1 parent 43bd770 commit 90a1176
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
Asset,
BaseDetailComponent,
Collection,
ConfigurableOperation,
Expand Down Expand Up @@ -46,7 +47,7 @@ export class CollectionDetailComponent
implements OnInit, OnDestroy {
customFields: CustomFieldConfig[];
detailForm: FormGroup;
assetChanges: { assetIds?: string[]; featuredAssetId?: string } = {};
assetChanges: { assets?: Asset[]; featuredAsset?: Asset } = {};
filters: ConfigurableOperation[] = [];
allFilters: ConfigurableOperationDefinition[] = [];
@ViewChild('collectionContents') contentsComponent: CollectionContentsComponent;
Expand Down Expand Up @@ -217,7 +218,7 @@ export class CollectionDetailComponent
}

canDeactivate(): boolean {
return super.canDeactivate() && !this.assetChanges.assetIds && !this.assetChanges.featuredAssetId;
return super.canDeactivate() && !this.assetChanges.assets && !this.assetChanges.featuredAsset;
}

/**
Expand Down Expand Up @@ -275,7 +276,8 @@ export class CollectionDetailComponent
});
return {
...updatedCategory,
...this.assetChanges,
assetIds: this.assetChanges.assets?.map(a => a.id),
featuredAssetId: this.assetChanges.featuredAsset?.id,
isPrivate: !form.value.visible,
filters: this.mapOperationsToInputs(this.filters, this.detailForm.value.filters),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
import { unique } from '@vendure/common/lib/unique';

export interface AssetChange {
assetIds: string[];
featuredAssetId: string | undefined;
assets: Asset[];
featuredAsset: Asset | undefined;
}

/**
Expand All @@ -38,7 +38,10 @@ export interface AssetChange {
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductAssetsComponent implements AfterViewInit {
@Input() assets: Asset[] = [];
@Input('assets') set assetsSetter(val: Asset[]) {
// create a new non-readonly array of assets
this.assets = val.slice();
}
@Input() featuredAsset: Asset | undefined;
@HostBinding('class.compact')
@Input()
Expand All @@ -53,6 +56,7 @@ export class ProductAssetsComponent implements AfterViewInit {
public sourceIndex: number;
public dragIndex: number;
public activeContainer;
public assets: Asset[] = [];

constructor(
private modalService: ModalService,
Expand Down Expand Up @@ -115,8 +119,8 @@ export class ProductAssetsComponent implements AfterViewInit {

private emitChangeEvent(assets: Asset[], featuredAsset: Asset | undefined) {
this.change.emit({
assetIds: assets.map(a => a.id),
featuredAssetId: featuredAsset && featuredAsset.id,
assets,
featuredAsset,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@ang
import { ActivatedRoute, Router } from '@angular/router';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
Asset,
BaseDetailComponent,
CreateProductInput,
createUpdatedTranslatable,
Expand Down Expand Up @@ -68,8 +69,8 @@ export interface VariantFormValue {
}

export interface SelectedAssets {
assetIds?: string[];
featuredAssetId?: string;
assets?: Asset[];
featuredAsset?: Asset;
}

@Component({
Expand Down Expand Up @@ -524,7 +525,7 @@ export class ProductDetailComponent
}

canDeactivate(): boolean {
return super.canDeactivate() && !this.assetChanges.assetIds && !this.assetChanges.featuredAssetId;
return super.canDeactivate() && !this.assetChanges.assets && !this.assetChanges.featuredAsset;
}

/**
Expand Down Expand Up @@ -635,7 +636,8 @@ export class ProductDetailComponent
});
return {
...updatedProduct,
...this.assetChanges,
assetIds: this.assetChanges.assets?.map(a => a.id),
featuredAssetId: this.assetChanges.featuredAsset?.id,
facetValueIds: productFormGroup.value.facetValueIds,
} as UpdateProductInput | CreateProductInput;
}
Expand Down Expand Up @@ -677,8 +679,8 @@ export class ProductDetailComponent
result.price = priceIncludesTax ? formValue.priceWithTax : formValue.price;
const assetChanges = this.variantAssetChanges[variant.id];
if (assetChanges) {
result.featuredAssetId = assetChanges.featuredAssetId;
result.assetIds = assetChanges.assetIds;
result.featuredAssetId = assetChanges.featuredAsset?.id;
result.assetIds = assetChanges.assets?.map(a => a.id);
}
return result;
})
Expand Down

0 comments on commit 90a1176

Please sign in to comment.