src/app/shared/widgets/apps/category-feed/datafeed-download/datafeed-download.component.ts
Properties |
|
datafeedsUrl |
datafeedsUrl:
|
Type : string
|
Optional |
Datafeeds url. |
popup-title |
popup-title:
|
Type : string
|
Optional |
Popup title. |
import { Component, TemplateRef, ViewChild } from "@angular/core";
import { Subject } from "rxjs";
import { getOrDefault, WidgetConfig } from "../../../widget.configuration";
import {
WidgetComponent,
WidgetId,
WidgetConfiguration,
WidgetConfigure,
WidgetInput,
} from "../../../widget.metadata";
import { MatDialog, MatDialogRef } from "@angular/material/dialog";
import { WidgetframeService } from "../../../widgetframe/widgetframe.service";
import { Action } from "../../../../components/hal/action";
import { HalService } from "../../../../components/hal/index";
import * as uriTemplates_ from "uri-templates";
const uriTemplates = uriTemplates_;
import { TranslateService } from "@ngx-translate/core";
import { NotificationsService } from "../../../../components/notifications/services/notifications.service";
export interface DatafeedDownload {
/**
* Datafeeds url.
*/
datafeedsUrl?: string;
/**
* Popup title.
*/
"popup-title"?: string;
}
/**
* Used in datafeeddownload app.
*
* Displays a button. Receives categoryIdentifier and displays popup for datafeed download.
*/
@WidgetComponent("nm-datafeeddownload")
@Component({
selector: "nm-datafeeddownload",
templateUrl: "./datafeed-download.component.html",
styleUrls: ["./datafeed-download.component.scss"],
})
export class DatafeeddownloadWidgetComponent {
@WidgetConfiguration()
public configuration: WidgetConfig;
/**
* Sets category identifier.
*/
@WidgetInput("categoryIdentifier")
private categoryIdentifier: Subject<any> = new Subject<any>();
/**
* Sets datafeed identifier.
*/
@WidgetInput("datafeed")
private datafeed: Subject<any> = new Subject<any>();
@WidgetId()
private _id: string;
@ViewChild(TemplateRef, { static: true }) template: TemplateRef<any>;
public isDownloadButtonNeeded: boolean = false;
public downloadAction: Action;
public categoryIdentifiers = <any>[];
public datafeedIdentifierString: string;
public dialogRef: MatDialogRef<any>;
public dataFeeds: any;
constructor(
private notificationService: NotificationsService,
public translateService: TranslateService,
public dialog: MatDialog,
public _halService: HalService,
public widgetframeService: WidgetframeService
) {}
@WidgetConfigure()
protected configureWidget(configuration: WidgetConfig<DatafeedDownload>) {
this.categoryIdentifier.subscribe((data) => {
this.categoryIdentifiers = data;
this.isDownloadButtonNeeded = this.categoryIdentifiers.length > 0;
});
this.datafeed.subscribe((data) => {
if (data && data.length > 0) {
this.datafeedIdentifierString = data[0].identifier;
this.downloadAction = data[0]._actions["download"];
}
});
const datafeedsUrl = (configuration._links.datafeeds as any).href;
if (!datafeedsUrl) {
throw new Error("link 'datafeeds' needs to be configured");
}
this.widgetframeService.getData(datafeedsUrl).subscribe((data) => {
this.dataFeeds = data._embedded.datafeeds;
});
}
onValueChange(id) {
const selectedDdataFeed = this.dataFeeds.filter((item) => {
return item.id === id;
})[0];
if (selectedDdataFeed) {
this.datafeedIdentifierString = selectedDdataFeed.identifier;
this.downloadAction = selectedDdataFeed._actions["download"];
}
}
openPopup() {
this.dialogRef = this.dialog.open(this.template);
}
closePopup() {
this.dialogRef.close();
let template = uriTemplates(this.downloadAction.href);
let href = template.fill({
"category-identifiers": this.categoryIdentifiers,
});
// fill removes the token, add it here manually
href = href + "{&token}";
this.downloadAction.href = href;
this._halService
.execute("download-category-datafeed", this.downloadAction)
.subscribe((resp) => {
this.notificationService.success(
this.translateService.instant(
"app.ipim-category-feed.message.success.title"
),
this.translateService.instant(
"app.ipim-category-feed.message.success.body"
)
);
});
}
}