2021-08-11 17:41:29 -04:00
|
|
|
import { Inject, Injectable, OnDestroy, OnInit } from "@angular/core";
|
2018-03-25 23:01:05 -04:00
|
|
|
import { FE_ComplexBot } from "../../shared/models/integration";
|
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { Subscription } from "rxjs/Subscription";
|
|
|
|
import { IntegrationsApiService } from "../../shared/services/integrations/integrations-api.service";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { ServiceLocator } from "../../shared/registry/locator.service";
|
2018-03-26 23:48:44 -04:00
|
|
|
import { ScalarClientApiService } from "../../shared/services/scalar/scalar-client-api.service";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2018-03-25 23:01:05 -04:00
|
|
|
|
2021-08-11 17:41:29 -04:00
|
|
|
@Injectable()
|
2018-03-25 23:01:05 -04:00
|
|
|
export class ComplexBotComponent<T> implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
public isLoading = true;
|
|
|
|
public isUpdating = false;
|
|
|
|
public bot: FE_ComplexBot<T>;
|
|
|
|
public newConfig: T;
|
2018-03-26 23:48:44 -04:00
|
|
|
public roomId: string;
|
2018-03-25 23:01:05 -04:00
|
|
|
|
|
|
|
private routeQuerySubscription: Subscription;
|
|
|
|
|
|
|
|
protected toaster = ServiceLocator.injector.get(ToasterService);
|
|
|
|
protected integrationsApi = ServiceLocator.injector.get(IntegrationsApiService);
|
|
|
|
protected route = ServiceLocator.injector.get(ActivatedRoute);
|
2018-03-26 23:48:44 -04:00
|
|
|
protected scalarClientApi = ServiceLocator.injector.get(ScalarClientApiService);
|
2018-03-25 23:01:05 -04:00
|
|
|
|
2021-08-11 17:41:29 -04:00
|
|
|
constructor(@Inject(String) private integrationType: string, public translate: TranslateService) {
|
|
|
|
this.translate = translate;
|
2018-03-25 23:01:05 -04:00
|
|
|
this.isLoading = true;
|
|
|
|
this.isUpdating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit(): void {
|
|
|
|
this.routeQuerySubscription = this.route.queryParams.subscribe(params => {
|
|
|
|
this.roomId = params['roomId'];
|
|
|
|
this.loadBot();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy(): void {
|
|
|
|
if (this.routeQuerySubscription) this.routeQuerySubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadBot() {
|
|
|
|
this.isLoading = true;
|
|
|
|
this.isUpdating = false;
|
|
|
|
|
|
|
|
this.newConfig = <T>{};
|
|
|
|
|
|
|
|
this.integrationsApi.getIntegrationInRoom("complex-bot", this.integrationType, this.roomId).then(i => {
|
|
|
|
this.bot = <FE_ComplexBot<T>>i;
|
2018-03-26 23:48:44 -04:00
|
|
|
this.newConfig = JSON.parse(JSON.stringify(this.bot.config));
|
2018-03-25 23:01:05 -04:00
|
|
|
this.isLoading = false;
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get("Failed to load configuration").subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-03-25 23:01:05 -04:00
|
|
|
});
|
|
|
|
}
|
2018-03-26 23:48:44 -04:00
|
|
|
|
|
|
|
public save(): void {
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.integrationsApi.setIntegrationConfiguration("complex-bot", this.integrationType, this.roomId, this.newConfig).then(() => {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get("Configuration updated").subscribe((res: string) => {this.toaster.pop("success", res); });
|
2018-03-26 23:48:44 -04:00
|
|
|
this.bot.config = this.newConfig;
|
|
|
|
this.newConfig = JSON.parse(JSON.stringify(this.bot.config));
|
|
|
|
this.isUpdating = false;
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get("Error updating configuration").subscribe((res: string) => {this.toaster.pop("error", res); });
|
2018-03-26 23:48:44 -04:00
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
2020-10-23 07:30:20 -04:00
|
|
|
}
|