-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Enable assigning Products to Channels
Relates to #12
- Loading branch information
1 parent
3a6c277
commit 59b9c91
Showing
32 changed files
with
707 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...onents/assign-products-to-channel-dialog/assign-products-to-channel-dialog.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<ng-template vdrDialogTitle>{{ 'catalog.assign-products-to-channel' | translate }}</ng-template> | ||
|
||
<div class="flex"> | ||
<clr-input-container> | ||
<label>{{ 'common.channel' | translate }}</label> | ||
<vdr-channel-assignment-control | ||
clrInput | ||
[multiple]="false" | ||
[includeDefaultChannel]="false" | ||
[formControl]="selectedChannelIdControl" | ||
></vdr-channel-assignment-control> | ||
</clr-input-container> | ||
<div class="flex-spacer"></div> | ||
<clr-input-container> | ||
<label>{{ 'catalog.price-conversion-factor' | translate }}</label> | ||
<input clrInput type="number" min="0" max="99999" [formControl]="priceFactorControl" /> | ||
</clr-input-container> | ||
</div> | ||
|
||
<div class="channel-price-preview"> | ||
<label class="clr-control-label">{{ 'catalog.channel-price-preview' | translate }}</label> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>{{ 'common.name' | translate }}</th> | ||
<th> | ||
{{ | ||
'catalog.price-in-channel' | ||
| translate: { channel: currentChannel?.code | channelCodeToLabel | translate } | ||
}} | ||
</th> | ||
<th> | ||
<ng-template [ngIf]="selectedChannel" [ngIfElse]="noSelection"> | ||
{{ 'catalog.price-in-channel' | translate: { channel: selectedChannel?.code } }} | ||
</ng-template> | ||
<ng-template #noSelection> | ||
{{ 'catalog.no-channel-selected' | translate }} | ||
</ng-template> | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr *ngFor="let row of variantsPreview$ | async"> | ||
<td>{{ row.name }}</td> | ||
<td>{{ row.price / 100 | currency: currentChannel?.currencyCode }}</td> | ||
<td> | ||
<ng-template [ngIf]="selectedChannel" [ngIfElse]="noChannelSelected"> | ||
{{ row.pricePreview / 100 | currency: selectedChannel?.currencyCode }} | ||
</ng-template> | ||
<ng-template #noChannelSelected> | ||
- | ||
</ng-template> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<ng-template vdrDialogButtons> | ||
<button type="button" class="btn" (click)="cancel()">{{ 'common.cancel' | translate }}</button> | ||
<button type="submit" (click)="assign()" [disabled]="!selectedChannel" class="btn btn-primary"> | ||
<ng-template [ngIf]="selectedChannel" [ngIfElse]="noSelection"> | ||
{{ 'catalog.assign-to-named-channel' | translate: { channelCode: selectedChannel?.code } }} | ||
</ng-template> | ||
<ng-template #noSelection> | ||
{{ 'catalog.no-channel-selected' | translate }} | ||
</ng-template> | ||
</button> | ||
</ng-template> |
12 changes: 12 additions & 0 deletions
12
...onents/assign-products-to-channel-dialog/assign-products-to-channel-dialog.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@import "variables"; | ||
|
||
vdr-channel-assignment-control { | ||
min-width: 200px; | ||
} | ||
|
||
.channel-price-preview { | ||
margin-top: 24px; | ||
table.table { | ||
margin-top: 6px; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...mponents/assign-products-to-channel-dialog/assign-products-to-channel-dialog.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { NotificationService } from '@vendure/admin-ui/src/app/core/providers/notification/notification.service'; | ||
import { combineLatest, from, Observable } from 'rxjs'; | ||
import { map, startWith, switchMap } from 'rxjs/operators'; | ||
|
||
import { GetChannels, ProductVariantFragment } from '../../../common/generated-types'; | ||
import { _ } from '../../../core/providers/i18n/mark-for-extraction'; | ||
import { DataService } from '../../../data/providers/data.service'; | ||
import { Dialog } from '../../../shared/providers/modal/modal.service'; | ||
|
||
@Component({ | ||
selector: 'vdr-assign-products-to-channel-dialog', | ||
templateUrl: './assign-products-to-channel-dialog.component.html', | ||
styleUrls: ['./assign-products-to-channel-dialog.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class AssignProductsToChannelDialogComponent implements OnInit, Dialog<any> { | ||
selectedChannel: GetChannels.Channels | null | undefined; | ||
currentChannel: GetChannels.Channels; | ||
availableChannels: GetChannels.Channels[]; | ||
resolveWith: (result?: any) => void; | ||
variantsPreview$: Observable<Array<{ id: string; name: string; price: number; pricePreview: number }>>; | ||
priceFactorControl = new FormControl(1); | ||
selectedChannelIdControl = new FormControl(); | ||
|
||
// assigned by ModalService.fromComponent() call | ||
productIds: string[]; | ||
|
||
constructor(private dataService: DataService, private notificationService: NotificationService) {} | ||
|
||
ngOnInit() { | ||
const activeChannelId$ = this.dataService.client | ||
.userStatus() | ||
.mapSingle(({ userStatus }) => userStatus.activeChannelId); | ||
const allChannels$ = this.dataService.settings.getChannels().mapSingle(data => data.channels); | ||
|
||
combineLatest(activeChannelId$, allChannels$).subscribe(([activeChannelId, channels]) => { | ||
// tslint:disable-next-line:no-non-null-assertion | ||
this.currentChannel = channels.find(c => c.id === activeChannelId)!; | ||
this.availableChannels = channels; | ||
}); | ||
|
||
this.selectedChannelIdControl.valueChanges.subscribe(ids => { | ||
this.selectChannel(ids); | ||
}); | ||
|
||
this.variantsPreview$ = combineLatest( | ||
from(this.getTopVariants(10)), | ||
this.priceFactorControl.valueChanges.pipe(startWith(1)), | ||
).pipe( | ||
map(([variants, factor]) => { | ||
return variants.map(v => ({ | ||
id: v.id, | ||
name: v.name, | ||
price: v.price, | ||
pricePreview: v.price * +factor, | ||
})); | ||
}), | ||
); | ||
} | ||
|
||
selectChannel(channelIds: string[]) { | ||
this.selectedChannel = this.availableChannels.find(c => c.id === channelIds[0]); | ||
} | ||
|
||
assign() { | ||
const selectedChannel = this.selectedChannel; | ||
if (selectedChannel) { | ||
this.dataService.product | ||
.assignProductsToChannel({ | ||
channelId: selectedChannel.id, | ||
productIds: this.productIds, | ||
priceFactor: +this.priceFactorControl.value, | ||
}) | ||
.subscribe(() => { | ||
this.notificationService.success(_('catalog.assign-product-to-channel-success'), { | ||
channel: selectedChannel.code, | ||
}); | ||
this.resolveWith(true); | ||
}); | ||
} | ||
} | ||
|
||
cancel() { | ||
this.resolveWith(); | ||
} | ||
|
||
private async getTopVariants(take: number): Promise<ProductVariantFragment[]> { | ||
const variants: ProductVariantFragment[] = []; | ||
|
||
for (let i = 0; i < this.productIds.length && variants.length < take; i++) { | ||
const productVariants = await this.dataService.product | ||
.getProduct(this.productIds[i]) | ||
.mapSingle(({ product }) => product && product.variants) | ||
.toPromise(); | ||
variants.push(...(productVariants || [])); | ||
} | ||
return variants.slice(0, take); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.