2018-10-23 00:10:28 -04:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { FE_CustomSimpleBot } from "../../shared/models/admin-responses";
|
|
|
|
import { AdminCustomSimpleBotsApiService } from "../../shared/services/admin/admin-custom-simple-bots-api.service";
|
|
|
|
import { AddCustomBotDialogContext, AdminAddCustomBotComponent } from "./add/add.component";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2021-08-16 18:00:59 -04:00
|
|
|
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
|
2018-10-23 00:10:28 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "./custom-bots.component.html",
|
|
|
|
styleUrls: ["./custom-bots.component.scss"],
|
|
|
|
})
|
|
|
|
export class AdminCustomBotsComponent {
|
|
|
|
|
|
|
|
public isLoading = true;
|
|
|
|
public bots: FE_CustomSimpleBot[];
|
|
|
|
public isUpdating = false;
|
|
|
|
|
|
|
|
constructor(private botApi: AdminCustomSimpleBotsApiService,
|
2021-09-01 19:01:01 -04:00
|
|
|
private toaster: ToasterService,
|
|
|
|
private modal: NgbModal,
|
|
|
|
public translate: TranslateService) {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate = translate;
|
2018-10-23 00:10:28 -04:00
|
|
|
|
|
|
|
this.reload().then(() => this.isLoading = false).catch(error => {
|
|
|
|
console.error(error);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Error loading go-neb configuration').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private reload(): Promise<any> {
|
|
|
|
return this.botApi.getBots().then(bots => {
|
|
|
|
this.bots = bots;
|
|
|
|
this.isLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public addBot() {
|
2021-08-16 18:00:59 -04:00
|
|
|
const selfhostedRef = this.modal.open(AdminAddCustomBotComponent, {
|
|
|
|
backdrop: 'static',
|
2018-10-23 00:10:28 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
|
|
|
selfhostedRef.result.then(() => {
|
|
|
|
try {
|
|
|
|
this.reload()
|
|
|
|
} catch (err) {
|
2018-10-23 00:10:28 -04:00
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Failed to get an updated bot list').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
}
|
2018-10-23 00:10:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public editBot(bot: FE_CustomSimpleBot) {
|
2021-08-16 18:00:59 -04:00
|
|
|
const selfhostedRef = this.modal.open(AdminAddCustomBotComponent, {
|
|
|
|
backdrop: 'static',
|
2018-10-23 00:10:28 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
|
|
|
selfhostedRef.result.then(() => {
|
|
|
|
try {
|
|
|
|
this.reload()
|
|
|
|
} catch (err) {
|
2018-10-23 00:10:28 -04:00
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Failed to get an updated bot list').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
}
|
2018-10-23 00:10:28 -04:00
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
const selfhostedInstance = selfhostedRef.componentInstance as AddCustomBotDialogContext;
|
|
|
|
selfhostedInstance.bot = bot;
|
|
|
|
selfhostedInstance.isAdding = !bot;
|
2018-10-23 00:10:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public toggleBot(bot: FE_CustomSimpleBot) {
|
|
|
|
this.isUpdating = true;
|
|
|
|
bot.isEnabled = !bot.isEnabled;
|
|
|
|
this.botApi.updateBot(bot.id, bot).then(() => {
|
|
|
|
this.isUpdating = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get(['Enabled', 'disabled']).subscribe((res: string) => {
|
|
|
|
this.toaster.pop("success", "Bot " + (bot.isEnabled ? res[0] : res[1]));
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
}).catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
bot.isEnabled = !bot.isEnabled;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Error updating bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|