2018-03-25 21:17:09 -04:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { FE_SimpleBot } from "../../shared/models/integration";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { ScalarClientApiService } from "../../shared/services/scalar/scalar-client-api.service";
|
|
|
|
import { IntegrationsApiService } from "../../shared/services/integrations/integrations-api.service";
|
|
|
|
import { BSModalContext } from "ngx-modialog/plugins/bootstrap";
|
|
|
|
import { DialogRef } from "ngx-modialog";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2018-03-25 21:17:09 -04:00
|
|
|
|
|
|
|
export class SimpleBotConfigDialogContext extends BSModalContext {
|
|
|
|
public bot: FE_SimpleBot;
|
|
|
|
public roomId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "simple-bot.component.html",
|
|
|
|
styleUrls: ["simple-bot.component.scss"],
|
|
|
|
})
|
|
|
|
export class ConfigSimpleBotComponent {
|
|
|
|
|
|
|
|
public bot: FE_SimpleBot;
|
|
|
|
|
|
|
|
private roomId: string;
|
|
|
|
|
|
|
|
constructor(public dialog: DialogRef<SimpleBotConfigDialogContext>,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private scalar: ScalarClientApiService,
|
2020-10-23 07:30:20 -04:00
|
|
|
private integrationsApi: IntegrationsApiService,
|
|
|
|
public translate: TranslateService) {
|
|
|
|
this.translate = translate;
|
2018-03-25 21:17:09 -04:00
|
|
|
this.bot = dialog.context.bot;
|
|
|
|
this.roomId = dialog.context.roomId;
|
|
|
|
|
|
|
|
this.bot._isUpdating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggle() {
|
|
|
|
let promise: Promise<any> = Promise.resolve();
|
|
|
|
if (!this.bot._inRoom) {
|
|
|
|
promise = this.scalar.inviteUser(this.roomId, this.bot.userId);
|
|
|
|
} else promise = this.integrationsApi.removeIntegration(this.bot.category, this.bot.type, this.roomId);
|
|
|
|
|
|
|
|
this.bot._inRoom = !this.bot._inRoom;
|
|
|
|
this.bot._isUpdating = true;
|
|
|
|
promise.then(() => {
|
|
|
|
this.bot._isUpdating = false;
|
2020-10-23 07:30:20 -04:00
|
|
|
if (this.bot._inRoom) {
|
|
|
|
this.translate.get('was invited to the room').subscribe((res: string) => {this.toaster.pop("success", this.bot.displayName + res); });
|
|
|
|
} else {
|
|
|
|
this.translate.get('was removed from the room').subscribe((res: string) => {this.toaster.pop("success", this.bot.displayName + res); });
|
|
|
|
}
|
2018-03-25 21:17:09 -04:00
|
|
|
}).catch(err => {
|
|
|
|
this.bot._inRoom = !this.bot._inRoom; // revert the status change
|
|
|
|
this.bot._isUpdating = false;
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
let errorMessage = null;
|
|
|
|
if (err.json) errorMessage = err.json().error;
|
|
|
|
if (err.response && err.response.error) errorMessage = err.response.error.message;
|
2020-10-23 07:30:20 -04:00
|
|
|
if (!errorMessage) errorMessage = "";
|
|
|
|
this.translate.get('Could not update integration status').subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-03-25 21:17:09 -04:00
|
|
|
});
|
|
|
|
}
|
2020-10-23 07:30:20 -04:00
|
|
|
}
|