import { APP_INITIALIZER, ChangeDetectorRef, Component, NgModule, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import {
AssetPickerDialogComponent,
CustomFieldComponentService,
CustomFieldConfig,
CustomFieldControl,
DataService,
ModalService,
NavBuilderService,
SharedModule,
} from '@vendure/admin-ui/src';
import { unique } from '@vendure/common/lib/unique';
import gql from 'graphql-tag';
import { Observable, of } from 'rxjs';
import { startWith, switchMap } from 'rxjs/operators';
@Component({
template: `
{{ formControl.value }}
`,
})
export class SliderControl implements CustomFieldControl {
customFieldConfig: CustomFieldConfig;
formControl: FormControl;
}
@Component({
template: `
{{ 'catalog.no-featured-asset' | translate }}
`,
})
export class AssetPickerControl implements CustomFieldControl, OnInit {
customFieldConfig: CustomFieldConfig;
formControl: FormControl;
currentAsset$: Observable;
constructor(
private changeDetectorRef: ChangeDetectorRef,
private modalService: ModalService,
private dataService: DataService,
) {}
ngOnInit(): void {
this.currentAsset$ = this.formControl.valueChanges.pipe(
startWith(this.formControl.value),
switchMap(assetId => {
if (!assetId) {
return of(null);
}
return this.dataService
.query(
gql`
query($id: ID!) {
asset(id: $id) {
id
name
preview
width
height
}
}
`,
{ id: assetId },
)
.mapStream((data: any) => data.asset);
}),
);
}
selectAssets() {
this.modalService
.fromComponent(AssetPickerDialogComponent, {
size: 'xl',
})
.subscribe((result: any) => {
if (result && result.length) {
this.formControl.setValue(result[0].id);
this.formControl.markAsDirty();
// this.changeDetectorRef.markForCheck();
}
});
}
}
@NgModule({
imports: [SharedModule],
declarations: [SliderControl, AssetPickerControl],
entryComponents: [SliderControl, AssetPickerControl],
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useFactory: addNavItems,
deps: [NavBuilderService],
},
{
provide: APP_INITIALIZER,
multi: true,
useFactory: registerCustomFieldComponents,
deps: [CustomFieldComponentService],
},
],
})
export class TestSharedModule {}
export function registerCustomFieldComponents(customFieldComponentService: CustomFieldComponentService) {
return () => {
customFieldComponentService.registerCustomFieldComponent('Product', 'length', SliderControl);
customFieldComponentService.registerCustomFieldComponent('ProductVariant', 'length', SliderControl);
customFieldComponentService.registerCustomFieldComponent(
'Product',
'offerImageId',
AssetPickerControl,
);
};
}
export function addNavItems(navBuilder: NavBuilderService) {
return () => {
navBuilder.addNavMenuSection(
{
id: 'test-plugin',
label: 'Test Plugin',
items: [
{
id: 'stats',
label: 'Test',
routerLink: ['/extensions/test'],
icon: 'line-chart',
},
],
},
'settings',
);
};
}