2018-10-23 00:10:28 -04:00
|
|
|
import { Component } from "@angular/core";
|
2021-08-16 18:00:59 -04:00
|
|
|
import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
|
2018-10-23 00:10:28 -04:00
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { FE_CustomSimpleBot, FE_UserProfile } from "../../../shared/models/admin-responses";
|
|
|
|
import { AdminCustomSimpleBotsApiService } from "../../../shared/services/admin/admin-custom-simple-bots-api.service";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2018-10-23 00:10:28 -04:00
|
|
|
|
2021-08-16 18:00:59 -04:00
|
|
|
export interface AddCustomBotDialogContext {
|
2018-10-23 00:10:28 -04:00
|
|
|
bot: FE_CustomSimpleBot;
|
2021-08-16 18:00:59 -04:00
|
|
|
isAdding: boolean;
|
2018-10-23 00:10:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "./add.component.html",
|
|
|
|
styleUrls: ["./add.component.scss"],
|
|
|
|
})
|
2021-08-16 18:00:59 -04:00
|
|
|
export class AdminAddCustomBotComponent {
|
2018-10-23 00:10:28 -04:00
|
|
|
|
2021-08-16 18:00:59 -04:00
|
|
|
public bot = <FE_CustomSimpleBot>{};
|
|
|
|
public isAdding = true;
|
2018-10-23 00:10:28 -04:00
|
|
|
public isSaving = false;
|
|
|
|
|
|
|
|
private lastProfile: FE_UserProfile;
|
|
|
|
|
2021-08-16 18:00:59 -04:00
|
|
|
constructor(public modal: NgbActiveModal,
|
2021-09-01 19:01:01 -04:00
|
|
|
private botApi: AdminCustomSimpleBotsApiService,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
public translate: TranslateService) {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate = translate;
|
2018-10-23 00:10:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public loadProfile() {
|
|
|
|
this.botApi.getProfile(this.bot.userId).then(profile => {
|
|
|
|
if (!this.lastProfile || this.lastProfile.name === this.bot.name) {
|
|
|
|
this.bot.name = profile.name;
|
|
|
|
}
|
|
|
|
if (!this.lastProfile || this.lastProfile.avatarUrl === this.bot.avatarUrl) {
|
|
|
|
this.bot.avatarUrl = profile.avatarUrl;
|
|
|
|
}
|
|
|
|
this.lastProfile = profile;
|
|
|
|
}).catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
// We don't need to alert the user - this is non-fatal
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public add() {
|
|
|
|
if (!this.bot.name) {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Please enter a name for the bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("warning", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.bot.avatarUrl) {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Please enter an avatar URL for the bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("warning", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.bot.userId) {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Please enter a user ID for the bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("warning", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.bot.description) {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Please enter a description for the bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("warning", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.bot.accessToken) {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Please enter an access token for the bot').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("warning", res);
|
|
|
|
});
|
2018-10-23 00:10:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isSaving = true;
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
name: this.bot.name,
|
|
|
|
avatarUrl: this.bot.avatarUrl,
|
|
|
|
userId: this.bot.userId,
|
|
|
|
accessToken: this.bot.accessToken,
|
|
|
|
description: this.bot.description,
|
|
|
|
isEnabled: true,
|
|
|
|
isPublic: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
let promise = null;
|
|
|
|
if (this.isAdding) {
|
|
|
|
promise = this.botApi.createBot(config);
|
|
|
|
} else {
|
|
|
|
promise = this.botApi.updateBot(this.bot.id, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.then(() => {
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Bot updated').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("success", res);
|
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
this.modal.close();
|
2018-10-23 00:10:28 -04:00
|
|
|
}).catch(error => {
|
|
|
|
this.isSaving = false;
|
|
|
|
console.error(error);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|