DEV: Ember upgrade (#155)

This commit is contained in:
Keegan George
2023-01-23 10:30:48 -08:00
committed by GitHub
parent 91d2cfb952
commit bdc2f27a2a
33 changed files with 772 additions and 757 deletions
@@ -1,12 +1,20 @@
import Controller from "@ember/controller";
import showModal from "discourse/lib/show-modal";
import computed from "discourse-common/utils/decorators";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
export default Controller.extend({
modalShowing: false,
const MODALS = {
editChannel: "admin-plugins-chat-integration-edit-channel",
testChannel: "admin-plugins-chat-integration-test",
editRule: "admin-plugins-chat-integration-edit-rule",
channelError: "admin-plugins-chat-integration-channel-error",
};
@computed("model.channels")
anyErrors(channels) {
export default class AdminPluginsChatIntegrationEditRule extends Controller {
@tracked modalShowing = false;
get anyErrors() {
const channels = this.model.channels;
let anyErrors = false;
channels.forEach((channel) => {
@@ -16,90 +24,83 @@ export default Controller.extend({
});
return anyErrors;
},
}
actions: {
createChannel() {
this.set("modalShowing", true);
triggerModal(model, modal) {
this.modalShowing = true;
const model = {
showModal(modal, {
model,
admin: true,
});
}
@action
createChannel() {
return this.triggerModal(
{
channel: this.store.createRecord("channel", {
provider: this.get("model.provider.id"),
provider: this.model.provider.id,
data: {},
}),
provider: this.get("model.provider"),
};
provider: this.model.provider,
},
MODALS.editChannel
);
}
showModal("admin-plugins-chat-integration-edit-channel", {
model,
admin: true,
});
},
editChannel(channel) {
this.set("modalShowing", true);
const model = {
@action
editChannel(channel) {
return this.triggerModal(
{
channel,
provider: this.get("model.provider"),
};
provider: this.model.provider,
},
MODALS.editChannel
);
}
showModal("admin-plugins-chat-integration-edit-channel", {
model,
admin: true,
});
},
@action
testChannel(channel) {
return this.triggerModal({ channel }, MODALS.testChannel);
}
testChannel(channel) {
this.set("modalShowing", true);
showModal("admin-plugins-chat-integration-test", {
model: { channel },
admin: true,
});
},
createRule(channel) {
this.set("modalShowing", true);
const model = {
@action
createRule(channel) {
return this.triggerModal(
{
rule: this.store.createRecord("rule", {
channel_id: channel.id,
channel,
}),
channel,
provider: this.get("model.provider"),
groups: this.get("model.groups"),
};
provider: this.model.provider,
groups: this.model.groups,
},
MODALS.editRule
);
}
showModal("admin-plugins-chat-integration-edit-rule", {
model,
admin: true,
});
},
editRuleWithChannel(rule, channel) {
this.set("modalShowing", true);
const model = {
@action
editRuleWithChannel(rule, channel) {
return this.triggerModal(
{
rule,
channel,
provider: this.get("model.provider"),
groups: this.get("model.groups"),
};
provider: this.model.provider,
groups: this.model.groups,
},
MODALS.editRule
);
}
showModal("admin-plugins-chat-integration-edit-rule", {
model,
admin: true,
});
},
@action
showError(channel) {
return this.triggerModal({ channel }, MODALS.channelError);
}
showError(channel) {
this.set("modalShowing", true);
showModal("admin-plugins-chat-integration-channel-error", {
model: channel,
admin: true,
});
},
},
});
@action
refresh() {
this.send("refreshProvider");
}
}
@@ -1,3 +1,3 @@
import Controller from "@ember/controller";
export default Controller.extend({});
export default class AdminPluginsChatIntegration extends Controller {}
@@ -1,131 +1,42 @@
import Controller from "@ember/controller";
import I18n from "I18n";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { popupAjaxError } from "discourse/lib/ajax-error";
import EmberObject, {
defineProperty,
computed as emberComputed,
} from "@ember/object";
import computed, { observes, on } from "discourse-common/utils/decorators";
import { schedule } from "@ember/runloop";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
export default Controller.extend(ModalFunctionality, {
@on("init")
setupKeydown() {
schedule("afterRender", () => {
$("#chat-integration-edit-channel-modal").keydown((e) => {
if (e.keyCode === 13) {
this.send("save");
}
});
});
},
export default class AdminPluginsChatIntegrationEditChannel extends Controller.extend(
ModalFunctionality
) {
@tracked validParams = false;
// The validation property must be defined at runtime since the possible parameters vary by provider
@observes("model")
setupValidations() {
if (this.get("model.provider")) {
const theKeys = this.get("model.provider.channel_parameters").map(
(param) => param["key"]
);
defineProperty(
this,
"paramValidation",
emberComputed(
`model.channel.data.{${theKeys.join(",")}}`,
this._paramValidation
)
);
this.notifyPropertyChange("paramValidation");
@action
isValidParams(validity) {
return (this.validParams = validity);
}
@action
handleKeyUp(e) {
if (e.code === "Enter" && this.validParams) {
this.save();
}
},
}
validate(parameter) {
const regString = parameter.regex;
const regex = new RegExp(regString);
let val = this.get(`model.channel.data.${parameter.key}`);
@action
cancel() {
this.send("closeModal");
}
if (val === undefined) {
val = "";
@action
save() {
if (!this.validParams) {
return;
}
if (val === "") {
// Fail silently if field blank
return EmberObject.create({
failed: true,
});
} else if (!regString) {
// Pass silently if no regex available for provider
return EmberObject.create({
ok: true,
});
} else if (regex.test(val)) {
// Test against regex
return EmberObject.create({
ok: true,
reason: I18n.t(
"chat_integration.edit_channel_modal.channel_validation.ok"
),
});
} else {
// Failed regex
return EmberObject.create({
failed: true,
reason: I18n.t(
"chat_integration.edit_channel_modal.channel_validation.fail"
),
});
}
},
_paramValidation() {
const response = {};
const parameters = this.get("model.provider.channel_parameters");
parameters.forEach((parameter) => {
response[parameter.key] = this.validate(parameter);
});
return response;
},
@computed("paramValidation")
saveDisabled(paramValidation) {
if (!paramValidation) {
return true;
}
let invalid = false;
Object.keys(paramValidation).forEach((key) => {
if (!paramValidation[key]) {
invalid = true;
}
if (!paramValidation[key]["ok"]) {
invalid = true;
}
});
return invalid;
},
actions: {
cancel() {
this.send("closeModal");
},
save() {
if (this.get("saveDisabled")) {
return;
}
this.get("model.channel")
.save()
.then(() => {
this.send("closeModal");
})
.catch(popupAjaxError);
},
},
});
this.model.channel
.save()
.then(() => {
this.send("closeModal");
})
.catch(popupAjaxError);
}
}
@@ -1,38 +1,51 @@
import Controller from "@ember/controller";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { popupAjaxError } from "discourse/lib/ajax-error";
import computed, { on } from "discourse-common/utils/decorators";
import { schedule } from "@ember/runloop";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
export default Controller.extend(ModalFunctionality, {
saveDisabled: false,
export default class AdminPluginsChatIntegrationEditRule extends Controller.extend(
ModalFunctionality
) {
@service siteSettings;
@tracked saveDisabled = false;
@on("init")
setupKeydown() {
schedule("afterRender", () => {
$("#chat-integration-edit-channel-modal").keydown((e) => {
if (e.keyCode === 13) {
this.send("save");
}
});
});
},
get showCategory() {
return this.model.rule.type === "normal";
}
@computed("model.rule.type")
showCategory(type) {
return type === "normal";
},
get currentRuleType() {
return this.model.rule.type;
}
actions: {
save(rule) {
if (this.get("saveDisabled")) {
return;
}
@action
save(rule) {
if (this.saveDisabled) {
return;
}
rule
.save()
.then(() => this.send("closeModal"))
.catch(popupAjaxError);
},
},
});
rule
.save()
.then(() => this.send("closeModal"))
.catch(popupAjaxError);
}
@action
handleKeyUp(e) {
if (e.code === "Enter") {
this.save();
}
}
@action
onChangeRuleType(type) {
this.model.rule.type = type;
this.currentRuleType = type;
if (type !== "normal") {
this.showCategory = false;
} else {
this.showCategory = true;
}
}
}
@@ -1,47 +1,43 @@
import Controller from "@ember/controller";
import { not } from "@ember/object/computed";
import I18n from "I18n";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import computed, { on } from "discourse-common/utils/decorators";
import { schedule } from "@ember/runloop";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
export default Controller.extend(ModalFunctionality, {
@on("init")
setupKeydown() {
schedule("afterRender", () => {
$("#chat_integration_test_modal").keydown((e) => {
if (e.keyCode === 13) {
this.send("send");
}
});
});
},
export default class AdminPluginsChatIntegrationTest extends Controller.extend(
ModalFunctionality
) {
@tracked loading = false;
@not("model.topic_id") sendDisabled;
@computed("model.topic_id")
sendDisabled(topicId) {
return !topicId;
},
@action
handleKeyUp(e) {
if (e.code === "Enter" && !this.sendDisabled) {
this.send();
}
}
actions: {
send() {
if (this.get("sendDisabled")) {
return;
}
this.set("loading", true);
@action
send() {
if (this.sendDisabled) {
return;
}
this.loading = true;
ajax("/admin/plugins/chat-integration/test", {
data: {
channel_id: this.get("model.channel.id"),
topic_id: this.get("model.topic_id"),
},
type: "POST",
ajax("/admin/plugins/chat-integration/test", {
data: {
channel_id: this.model.channel.id,
topic_id: this.model.topic_id,
},
type: "POST",
})
.then(() => {
this.loading = false;
this.flash(I18n.t("chat_integration.test_modal.success"), "success");
})
.then(() => {
this.set("loading", false);
this.flash(I18n.t("chat_integration.test_modal.success"), "success");
})
.catch(popupAjaxError);
},
},
});
.catch(popupAjaxError);
}
}