2017-05-29 00:51:04 -04:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { RSSIntegration } from "../../shared/models/integration";
|
|
|
|
import { ModalComponent, DialogRef } from "angular2-modal";
|
|
|
|
import { ConfigModalContext } from "../../integration/integration.component";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { ApiService } from "../../shared/api.service";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-rss-config',
|
|
|
|
templateUrl: './rss-config.component.html',
|
|
|
|
styleUrls: ['./rss-config.component.scss', './../config.component.scss'],
|
|
|
|
})
|
|
|
|
export class RssConfigComponent implements ModalComponent<ConfigModalContext> {
|
|
|
|
|
|
|
|
public integration: RSSIntegration;
|
|
|
|
|
|
|
|
public isUpdating = false;
|
|
|
|
public feedUrl = "";
|
|
|
|
|
|
|
|
private roomId: string;
|
|
|
|
private scalarToken: string;
|
|
|
|
|
|
|
|
constructor(public dialog: DialogRef<ConfigModalContext>,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private api: ApiService) {
|
|
|
|
this.integration = <RSSIntegration>dialog.context.integration;
|
|
|
|
this.roomId = dialog.context.roomId;
|
|
|
|
this.scalarToken = dialog.context.scalarToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addFeed() {
|
|
|
|
if (!this.feedUrl || this.feedUrl.trim().length === 0) {
|
|
|
|
this.toaster.pop("warning", "Please enter a feed URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.integration.feeds.indexOf(this.feedUrl) !== -1) {
|
|
|
|
this.toaster.pop("error", "This feed has already been added");
|
2017-05-29 00:58:14 -04:00
|
|
|
return;
|
2017-05-29 00:51:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let feedCopy = JSON.parse(JSON.stringify(this.integration.feeds));
|
|
|
|
feedCopy.push(this.feedUrl);
|
2017-05-29 01:02:44 -04:00
|
|
|
this.updateFeeds(feedCopy).then(() => this.feedUrl = "");
|
2017-05-29 00:51:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeFeed(feedUrl) {
|
|
|
|
let feedCopy = JSON.parse(JSON.stringify(this.integration.feeds));
|
|
|
|
const idx = feedCopy.indexOf(feedUrl);
|
|
|
|
feedCopy.splice(idx, 1);
|
2017-05-29 01:02:44 -04:00
|
|
|
this.updateFeeds(feedCopy);
|
2017-05-29 00:51:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private updateFeeds(newFeeds) {
|
|
|
|
this.isUpdating = true;
|
2017-05-29 00:57:40 -04:00
|
|
|
return this.api.updateIntegrationState(this.roomId, this.integration.type, this.integration.integrationType, this.scalarToken, {
|
2017-05-29 00:51:04 -04:00
|
|
|
feeds: newFeeds
|
|
|
|
}).then(response => {
|
|
|
|
this.integration.feeds = response.feeds;
|
|
|
|
this.integration.immutableFeeds = response.immutableFeeds;
|
|
|
|
this.isUpdating = false;
|
|
|
|
this.toaster.pop("success", "Feeds updated");
|
|
|
|
}).catch(err => {
|
|
|
|
this.toaster.pop("error", err.json().error);
|
|
|
|
console.error(err);
|
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|