Add a dialog for configuring simple bots

This commit is contained in:
Travis Ralston 2018-03-25 19:17:09 -06:00
parent 87121150cc
commit b5aec06c04
5 changed files with 86 additions and 30 deletions

View file

@ -58,6 +58,7 @@ import { AdminNebGiphyConfigComponent } from "./admin/neb/config/giphy/giphy.com
import { AdminNebGuggyConfigComponent } from "./admin/neb/config/guggy/guggy.component";
import { AdminNebGoogleConfigComponent } from "./admin/neb/config/google/google.component";
import { AdminNebImgurConfigComponent } from "./admin/neb/config/imgur/imgur.component";
import { ConfigSimpleBotComponent } from "./configs/simple-bot/simple-bot.component";
@NgModule({
imports: [
@ -110,6 +111,7 @@ import { AdminNebImgurConfigComponent } from "./admin/neb/config/imgur/imgur.com
AdminNebGuggyConfigComponent,
AdminNebGoogleConfigComponent,
AdminNebImgurConfigComponent,
ConfigSimpleBotComponent,
// Vendor
],
@ -137,6 +139,7 @@ import { AdminNebImgurConfigComponent } from "./admin/neb/config/imgur/imgur.com
AdminNebGuggyConfigComponent,
AdminNebGoogleConfigComponent,
AdminNebImgurConfigComponent,
ConfigSimpleBotComponent,
]
})
export class AppModule {

View file

@ -0,0 +1,14 @@
<div class="dialog">
<div class="dialog-header">
<h4>{{ bot.displayName }}</h4>
</div>
<div class="dialog-content" style="text-align: center;">
<p>{{ bot.description }}</p>
<ui-switch [checked]="bot._inRoom" [disabled]="bot._isUpdating" (change)="toggle()"></ui-switch>
</div>
<div class="dialog-footer">
<button type="button" (click)="dialog.close()" title="close" class="btn btn-secondary btn-sm">
<i class="far fa-times-circle"></i> Close
</button>
</div>
</div>

View file

@ -0,0 +1,59 @@
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";
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,
private integrationsApi: IntegrationsApiService) {
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;
if (this.bot._inRoom) this.toaster.pop("success", this.bot.displayName + " was invited to the room");
else this.toaster.pop("success", this.bot.displayName + " was removed from the room");
}).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;
if (!errorMessage) errorMessage = "Could not update integration status";
this.toaster.pop("error", errorMessage);
});
}
}

View file

@ -1,5 +1,4 @@
import { Component } from "@angular/core";
import { ToasterService } from "angular2-toaster";
import { ActivatedRoute, Router } from "@angular/router";
import { ScalarClientApiService } from "../../shared/services/scalar/scalar-client-api.service";
import * as _ from "lodash";
@ -9,6 +8,8 @@ import { IntegrationsRegistry } from "../../shared/registry/integrations.registr
import { SessionStorage } from "../../shared/SessionStorage";
import { AdminApiService } from "../../shared/services/admin/admin-api.service";
import { IntegrationsApiService } from "../../shared/services/integrations/integrations-api.service";
import { Modal, overlayConfigFactory } from "ngx-modialog";
import { ConfigSimpleBotComponent, SimpleBotConfigDialogContext } from "../../configs/simple-bot/simple-bot.component";
const CATEGORY_MAP = {
"Widgets": ["widget"],
@ -39,8 +40,8 @@ export class RiotHomeComponent {
private scalar: ScalarClientApiService,
private integrationsApi: IntegrationsApiService,
private adminApi: AdminApiService,
private toaster: ToasterService,
private router: Router) {
private router: Router,
private modal: Modal) {
let params: any = this.activatedRoute.snapshot.queryParams;
this.requestedScreen = params.screen;
@ -128,34 +129,13 @@ export class RiotHomeComponent {
console.log(this.userId + " is trying to modify " + integration.displayName);
if (integration.category === "bot") {
// It's a bot
const bot = <FE_SimpleBot>integration;
// TODO: "Are you sure?" dialog
this.modal.open(ConfigSimpleBotComponent, overlayConfigFactory({
bot: <FE_SimpleBot>integration,
roomId: this.roomId,
let promise: Promise<any> = Promise.resolve();
if (!integration._inRoom) {
promise = this.scalar.inviteUser(this.roomId, bot.userId);
} else promise = this.integrationsApi.removeIntegration(integration.category, integration.type, this.roomId);
// We set this ahead of the promise for debouncing
integration._inRoom = !integration._inRoom;
integration._isUpdating = true;
promise.then(() => {
integration._isUpdating = false;
if (integration._inRoom) this.toaster.pop("success", integration.displayName + " was invited to the room");
else this.toaster.pop("success", integration.displayName + " was removed from the room");
}).catch(err => {
integration._inRoom = !integration._inRoom; // revert the status change
integration._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;
if (!errorMessage) errorMessage = "Could not update integration status";
this.toaster.pop("error", errorMessage);
});
isBlocking: true,
size: 'lg',
}, SimpleBotConfigDialogContext));
} else {
console.log("Navigating to edit screen for " + integration.category + " " + integration.type);
this.router.navigate(['riot-app', integration.category, integration.type]);