featured-review-selector.component.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
  2. import { FormControl } from '@angular/forms';
  3. import { ActivatedRoute } from '@angular/router';
  4. import { CustomFieldControl, DataService, SharedModule } from '@vendure/admin-ui/core';
  5. import { Observable } from 'rxjs';
  6. import { switchMap } from 'rxjs/operators';
  7. import { GetReviewForProductDocument, ProductReviewFragment } from '../../generated-types';
  8. @Component({
  9. selector: 'kb-relation-review-input',
  10. template: `
  11. <div *ngIf="formControl.value as review">
  12. <vdr-chip>{{ review.rating }} / 5</vdr-chip>
  13. {{ review.summary }}
  14. <a [routerLink]="['/extensions', 'product-reviews', review.id]">
  15. <clr-icon shape="link"></clr-icon>
  16. </a>
  17. </div>
  18. <select class="mt-1" [formControl]="formControl">
  19. <option [ngValue]="null">Select a review...</option>
  20. <option *ngFor="let item of reviews$ | async" [ngValue]="item">
  21. <b>{{ item.summary }}</b>
  22. {{ item.rating }} / 5
  23. </option>
  24. </select>
  25. `,
  26. changeDetection: ChangeDetectionStrategy.OnPush,
  27. standalone: true,
  28. imports: [SharedModule],
  29. })
  30. export class RelationReviewInputComponent implements OnInit, CustomFieldControl {
  31. @Input() readonly: boolean;
  32. @Input() formControl: FormControl;
  33. @Input() config: any;
  34. reviews$: Observable<ProductReviewFragment[]>;
  35. constructor(private dataService: DataService, private route: ActivatedRoute) {}
  36. ngOnInit() {
  37. this.reviews$ = this.route.data.pipe(
  38. switchMap(data => data.detail.entity),
  39. switchMap((product: any) => {
  40. return this.dataService
  41. .query(GetReviewForProductDocument, {
  42. productId: product.id,
  43. })
  44. .mapSingle(({ product }) => product?.reviews.items ?? []);
  45. }),
  46. );
  47. }
  48. }