DEV: Refactor bootbox dialogs (#137)

This commit is contained in:
Penar Musaraj 2022-10-17 14:42:40 -04:00 committed by GitHub
parent c93be7632c
commit 550e8347e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 96 additions and 107 deletions

1
.discourse-compatibility Normal file
View File

@ -0,0 +1 @@
2.9.0.beta9: 39b7253c082270e33839f4b0821fbf75abfdd826

View File

@ -2,7 +2,7 @@ import discourseComputed from "discourse-common/utils/decorators";
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import { alias } from "@ember/object/computed"; import { alias } from "@ember/object/computed";
import bootbox from "bootbox"; import { popupAjaxError } from "discourse/lib/ajax-error";
const RECURRING = "recurring"; const RECURRING = "recurring";
const ONE_TIME = "one_time"; const ONE_TIME = "one_time";
@ -83,9 +83,7 @@ export default Controller.extend({
this.get("model.plan") this.get("model.plan")
.save() .save()
.then(() => this.redirect(this.productId)) .then(() => this.redirect(this.productId))
.catch((data) => .catch(popupAjaxError);
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
}, },
updatePlan() { updatePlan() {
@ -95,9 +93,7 @@ export default Controller.extend({
this.get("model.plan") this.get("model.plan")
.update() .update()
.then(() => this.redirect(this.productId)) .then(() => this.redirect(this.productId))
.catch((data) => .catch(popupAjaxError);
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
}, },
}, },
}); });

View File

@ -4,10 +4,12 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import I18n from "I18n"; import I18n from "I18n";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
export default Controller.extend({ export default Controller.extend({
loading: false, loading: false,
dialog: service(),
@discourseComputed @discourseComputed
stripeConfigured() { stripeConfigured() {
@ -29,26 +31,20 @@ export default Controller.extend({
ajax(`/s/admin/refresh`, { ajax(`/s/admin/refresh`, {
method: "post", method: "post",
}).then(() => { }).then(() => {
bootbox.alert( this.dialog.alert(
I18n.t("discourse_subscriptions.campaign.refresh_page"), I18n.t("discourse_subscriptions.campaign.refresh_page")
() => {
this.transitionToRoute(
"adminPlugins.discourse-subscriptions.products"
);
}
); );
}); });
}, },
@action @action
createOneClickCampaign() { createOneClickCampaign() {
bootbox.confirm( this.dialog.yesNoConfirm({
I18n.t("discourse_subscriptions.campaign.confirm_creation"), title: I18n.t("discourse_subscriptions.campaign.confirm_creation_title"),
(result) => { message: htmlSafe(
if (!result) { I18n.t("discourse_subscriptions.campaign.confirm_creation")
return; ),
} didConfirm: () => {
this.set("loading", true); this.set("loading", true);
ajax(`/s/admin/create-campaign`, { ajax(`/s/admin/create-campaign`, {
@ -56,15 +52,15 @@ export default Controller.extend({
}) })
.then(() => { .then(() => {
this.set("loading", false); this.set("loading", false);
bootbox.alert( this.dialog.confirm({
I18n.t("discourse_subscriptions.campaign.created"), message: I18n.t("discourse_subscriptions.campaign.created"),
() => { shouldDisplayCancel: false,
this.send("showSettings"); didConfirm: () => this.send("showSettings"),
} didCancel: () => this.send("showSettings"),
); });
}) })
.catch(popupAjaxError); .catch(popupAjaxError);
} },
); });
}, },
}); });

View File

@ -4,9 +4,10 @@ import Transaction from "discourse/plugins/discourse-subscriptions/discourse/mod
import I18n from "I18n"; import I18n from "I18n";
import { not } from "@ember/object/computed"; import { not } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Controller.extend({ export default Controller.extend({
dialog: service(),
selectedPlan: null, selectedPlan: null,
promoCode: null, promoCode: null,
isAnonymous: not("currentUser"), isAnonymous: not("currentUser"),
@ -23,7 +24,7 @@ export default Controller.extend({
}, },
alert(path) { alert(path) {
bootbox.alert(I18n.t(`discourse_subscriptions.${path}`)); this.dialog.alert(I18n.t(`discourse_subscriptions.${path}`));
}, },
@discourseComputed("model.product.repurchaseable", "model.product.subscribed") @discourseComputed("model.product.repurchaseable", "model.product.subscribed")
@ -63,7 +64,7 @@ export default Controller.extend({
return result; return result;
} else { } else {
this.set("loading", false); this.set("loading", false);
bootbox.alert(result.error.message || result.error); this.dialog.alert(result.error.message || result.error);
return result; return result;
} }
}); });
@ -99,7 +100,7 @@ export default Controller.extend({
transaction transaction
.then((result) => { .then((result) => {
if (result.error) { if (result.error) {
bootbox.alert(result.error.message || result.error); this.dialog.alert(result.error.message || result.error);
} else if ( } else if (
result.status === "incomplete" || result.status === "incomplete" ||
result.status === "open" result.status === "open"
@ -122,7 +123,7 @@ export default Controller.extend({
} }
}) })
.catch((result) => { .catch((result) => {
bootbox.alert( this.dialog.alert(
result.jqXHR.responseJSON.errors[0] || result.errorThrown result.jqXHR.responseJSON.errors[0] || result.errorThrown
); );
this.set("loading", false); this.set("loading", false);

View File

@ -3,9 +3,10 @@ import { action } from "@ember/object";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import I18n from "I18n"; import I18n from "I18n";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Controller.extend({ export default Controller.extend({
dialog: service(),
loading: false, loading: false,
saved: false, saved: false,
init() { init() {
@ -29,7 +30,7 @@ export default Controller.extend({
}); });
if (paymentMethodObject.error) { if (paymentMethodObject.error) {
bootbox.alert( this.dialog.alert(
paymentMethodObject.error?.message || I18n.t("generic_error") paymentMethodObject.error?.message || I18n.t("generic_error")
); );
this.set("loading", false); this.set("loading", false);

View File

@ -2,24 +2,23 @@ import Route from "@ember/routing/route";
import AdminProduct from "discourse/plugins/discourse-subscriptions/discourse/models/admin-product"; import AdminProduct from "discourse/plugins/discourse-subscriptions/discourse/models/admin-product";
import I18n from "I18n"; import I18n from "I18n";
import { action } from "@ember/object"; import { action } from "@ember/object";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Route.extend({ export default Route.extend({
dialog: service(),
model() { model() {
return AdminProduct.findAll(); return AdminProduct.findAll();
}, },
@action @action
destroyProduct(product) { destroyProduct(product) {
bootbox.confirm( this.dialog.yesNoConfirm({
I18n.t( message: I18n.t(
"discourse_subscriptions.admin.products.operations.destroy.confirm" "discourse_subscriptions.admin.products.operations.destroy.confirm"
), ),
I18n.t("no_value"), didConfirm: () => {
I18n.t("yes_value"), return product
(confirmed) => {
if (confirmed) {
product
.destroy() .destroy()
.then(() => { .then(() => {
this.controllerFor( this.controllerFor(
@ -29,10 +28,9 @@ export default Route.extend({
.removeObject(product); .removeObject(product);
}) })
.catch((data) => .catch((data) =>
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n")) this.dialog.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
}
}
); );
}, },
});
},
}); });

View File

@ -4,9 +4,10 @@ import AdminPlan from "discourse/plugins/discourse-subscriptions/discourse/model
import I18n from "I18n"; import I18n from "I18n";
import { hash } from "rsvp"; import { hash } from "rsvp";
import { action } from "@ember/object"; import { action } from "@ember/object";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Route.extend({ export default Route.extend({
dialog: service(),
model(params) { model(params) {
const product_id = params["product-id"]; const product_id = params["product-id"];
let product; let product;
@ -24,26 +25,22 @@ export default Route.extend({
@action @action
destroyPlan(plan) { destroyPlan(plan) {
bootbox.confirm( this.dialog.yesNoConfirm({
I18n.t("discourse_subscriptions.admin.plans.operations.destroy.confirm"), message: I18n.t(
I18n.t("no_value"), "discourse_subscriptions.admin.plans.operations.destroy.confirm"
I18n.t("yes_value"), ),
(confirmed) => { didConfirm: () => {
if (confirmed) {
plan plan
.destroy() .destroy()
.then(() => { .then(() => {
this.controllerFor( this.controllerFor("adminPluginsDiscourseSubscriptionsProductsShow")
"adminPluginsDiscourseSubscriptionsProductsShow"
)
.get("model.plans") .get("model.plans")
.removeObject(plan); .removeObject(plan);
}) })
.catch((data) => .catch((data) =>
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n")) this.dialog.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
}
}
); );
}, },
});
},
}); });

View File

@ -2,9 +2,10 @@ import I18n from "I18n";
import Route from "@ember/routing/route"; import Route from "@ember/routing/route";
import AdminSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/admin-subscription"; import AdminSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/admin-subscription";
import { action } from "@ember/object"; import { action } from "@ember/object";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Route.extend({ export default Route.extend({
dialog: service(),
model() { model() {
return AdminSubscription.find(); return AdminSubscription.find();
}, },
@ -19,9 +20,11 @@ export default Route.extend({
.then((result) => { .then((result) => {
subscription.set("status", result.status); subscription.set("status", result.status);
this.send("closeModal"); this.send("closeModal");
bootbox.alert(I18n.t("discourse_subscriptions.admin.canceled")); this.dialog.alert(I18n.t("discourse_subscriptions.admin.canceled"));
}) })
.catch((data) => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))) .catch((data) =>
this.dialog.alert(data.jqXHR.responseJSON.errors.join("\n"))
)
.finally(() => { .finally(() => {
subscription.set("loading", false); subscription.set("loading", false);
this.refresh(); this.refresh();

View File

@ -2,9 +2,10 @@ import Route from "@ember/routing/route";
import UserSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/user-subscription"; import UserSubscription from "discourse/plugins/discourse-subscriptions/discourse/models/user-subscription";
import I18n from "I18n"; import I18n from "I18n";
import { action } from "@ember/object"; import { action } from "@ember/object";
import bootbox from "bootbox"; import { inject as service } from "@ember/service";
export default Route.extend({ export default Route.extend({
dialog: service(),
model() { model() {
return UserSubscription.findAll(); return UserSubscription.findAll();
}, },
@ -15,28 +16,24 @@ export default Route.extend({
}, },
@action @action
cancelSubscription(subscription) { cancelSubscription(subscription) {
bootbox.confirm( this.dialog.yesNoConfirm({
I18n.t( message: I18n.t(
"discourse_subscriptions.user.subscriptions.operations.destroy.confirm" "discourse_subscriptions.user.subscriptions.operations.destroy.confirm"
), ),
I18n.t("no_value"), didConfirm: () => {
I18n.t("yes_value"),
(confirmed) => {
if (confirmed) {
subscription.set("loading", true); subscription.set("loading", true);
subscription subscription
.destroy() .destroy()
.then((result) => subscription.set("status", result.status)) .then((result) => subscription.set("status", result.status))
.catch((data) => .catch((data) =>
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n")) this.dialog.alert(data.jqXHR.responseJSON.errors.join("\n"))
) )
.finally(() => { .finally(() => {
subscription.set("loading", false); subscription.set("loading", false);
this.refresh(); this.refresh();
}); });
} },
} });
);
}, },
}); });

View File

@ -47,9 +47,8 @@ en:
refresh_page: Campaign data is now syncing from Stripe in the background. Refresh the page in a few moments to load the updated info. refresh_page: Campaign data is now syncing from Stripe in the background. Refresh the page in a few moments to load the updated info.
supporter: Supporter supporter: Supporter
supporters: Supporters supporters: Supporters
confirm_creation_title: Do you want to automatically create a campaign for your community?
confirm_creation: |- confirm_creation: |-
<p>Do you want to automatically create a campaign for your community?</p>
<p>Continuing will create:</p> <p>Continuing will create:</p>
<ul> <ul>