@WidgetComponent

nm-synchronize-data

File

src/app/shared/widgets/buy/synchronization/synchronization.component.ts

Implements

OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector nm-synchronize-data
styleUrls synchronization.component.scss
templateUrl ./synchronization.component.html

Index

Widget inputs
Widget outputs
Properties
Methods

Constructor

constructor(_widgetframeService: WidgetframeService, notificationService: NotificationsService, translateService: TranslateService, halService: HalService)
Parameters :
Name Type Optional
_widgetframeService WidgetframeService no
notificationService NotificationsService no
translateService TranslateService no
halService HalService no

Methods

checkSynchronizationFinished
checkSynchronizationFinished()
Returns : void
Protected configureWidget
configureWidget(configuration: WidgetConfig)
Decorators : WidgetConfigure
Parameters :
Name Type Optional
configuration WidgetConfig no
Returns : void
getProcessStatus
getProcessStatus()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
startSynchronization
startSynchronization()
Returns : void
Private stopPolling
stopPolling()
Returns : void

Properties

Public _id
_id: string
Type : string
Decorators : WidgetId
Private action
action: Action
Type : Action
Public configuration
configuration: WidgetConfig
Type : WidgetConfig
Public isDisabled
isDisabled: boolean
Type : boolean
Public lastSynchronization
lastSynchronization: Observable<Date>
Type : Observable<Date>
Public masterdataLabel
masterdataLabel: string
Type : string
Private refresh
refresh: number
Type : number
Public synchronizeDataButton
synchronizeDataButton: string
Type : string
Private timerSubscription
timerSubscription: Subscription
Type : Subscription
Default value : null
Public title
title: string
Type : string
Private trigger
trigger: string
Type : string
Private unsubscribe
unsubscribe:
Default value : NgUnsubscribe.create()
import { Subscription, timer as observableTimer, timer } from "rxjs";
import { ChangeDetectionStrategy, Component, OnDestroy } from "@angular/core";
import { WidgetframeService } from "../../widgetframe/widgetframe.service";
import { WidgetConfig } from "../../widget.configuration";
import {
  WidgetComponent,
  WidgetConfigure,
  WidgetId,
} from "../../widget.metadata";
import { TranslateService } from "@ngx-translate/core";
import { NotificationsService } from "../../../components/notifications/services/notifications.service";
import { HalService } from "../../../components/hal/hal.service";
import { Action } from "../../../components/hal/action";
import { Link } from "../../../components/hal/hal";
import { NgUnsubscribe } from "../../../ng-unsubscribe";
import { Observable } from "rxjs";
import { flatMap, map } from "rxjs/operators";
import { takeUntil } from "rxjs/operators";

const TEN_SECONDS = 10 * 1000;

@WidgetComponent("nm-synchronize-data")
@Component({
  selector: "nm-synchronize-data",
  templateUrl: "./synchronization.component.html",
  styleUrls: ["./synchronization.component.scss"],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SynchronizeDataWidgetComponent implements OnDestroy {
  @WidgetId()
  public _id: string;

  public title: string;
  public synchronizeDataButton: string;
  public masterdataLabel: string;
  private trigger: string;
  public isDisabled: boolean;
  private refresh: number;
  private action: Action;

  private timerSubscription: Subscription = null;
  public configuration: WidgetConfig;

  private unsubscribe = NgUnsubscribe.create();

  public lastSynchronization: Observable<Date>;

  constructor(
    private _widgetframeService: WidgetframeService,
    private notificationService: NotificationsService,
    private translateService: TranslateService,
    private halService: HalService
  ) {}

  @WidgetConfigure()
  protected configureWidget(configuration: WidgetConfig) {
    this.isDisabled = false;
    this.title = configuration.configuration["title"];
    this.synchronizeDataButton =
      configuration.configuration["synchronizeData-Button"];
    this.masterdataLabel = configuration.configuration["masterdata-label"];

    const uri = (<Link>configuration._links["trigger"]).href;
    this.trigger = uri;
    this.refresh = configuration.configuration["refresh"];

    this.lastSynchronization = timer(1000, TEN_SECONDS).pipe(
      takeUntil(this.unsubscribe),
      flatMap(() => this._widgetframeService.getData(uri)),
      map((result) => (result?.timestamp ? new Date(result.timestamp) : null))
    );

    this.configuration = configuration;
  }

  startSynchronization(): void {
    this.isDisabled = true;
    this._widgetframeService.putData(this.trigger, null).subscribe(
      (data) => {
        this.action = <Action>data._actions["poll"];
        this.notificationService.success(
          this.translateService.instant("message.success.title"),
          this.translateService.instant(data["message"])
        );
        this.checkSynchronizationFinished();
      },
      (error) => {
        var err = JSON.parse(error.message);
        this.notificationService.error(err.title, err.message);
        this.isDisabled = false;
      }
    );
  }

  checkSynchronizationFinished(): void {
    if (this.refresh) {
      this.timerSubscription = observableTimer(
        this.refresh,
        this.refresh
      ).subscribe((t) => this.getProcessStatus());
    }
  }

  getProcessStatus(): void {
    this.halService.execute("synchronization-polling", this.action).subscribe(
      (data) => {
        if (data.response.body.status == "FINISHED") {
          this.notificationService.success(
            this.translateService.instant("message.success.title"),
            this.translateService.instant("message.success.body")
          );
          this.isDisabled = false;

          this.stopPolling();
        }
      },
      (error) => {
        var err = JSON.parse(error.message);
        this.notificationService.error(err.title, err.message);
        this.isDisabled = false;
        this.stopPolling();
      }
    );
  }

  private stopPolling() {
    if (this.timerSubscription) {
      this.timerSubscription.unsubscribe();
    }
  }

  ngOnDestroy(): void {
    this.stopPolling();
    this.unsubscribe.destroy();
  }
}
<nm-widgetframe
  [header]="configuration.configuration['header']"
  style="max-width: 50%; min-width: 700px"
  widgetId="{{ _id }}"
>
  <div slot="title" class="nm-widgetframe__title">{{ title | translate }}</div>

  <div slot="content" class="nm-widgetframe__content">
    <div>
      <label class="nm-label-font"> {{ masterdataLabel | translate }}</label>
      <button
        mat-raised-button
        color="primary"
        [disabled]="isDisabled"
        (click)="startSynchronization()"
      >
        <i class="material-icons">import_export</i>
        {{ synchronizeDataButton | translate }}
      </button>
    </div>
    <div *ngIf="lastSynchronization | async as timestamp; else elseBlock">
      {{ "web.label.last-synchronization" | translate }}:
      {{ timestamp | amTimeAgo }}
    </div>
    <ng-template #elseBlock>
      {{ "web.label.last-synchronization" | translate }}:
      {{ "web.label.never-synchronized" | translate }}
    </ng-template>
  </div>
</nm-widgetframe>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""