discourse-subscriptions/assets/javascripts/discourse/controllers/admin-plugins-discourse-sub...

104 lines
2.5 KiB
JavaScript
Raw Normal View History

import discourseComputed from "discourse-common/utils/decorators";
2019-10-22 00:45:51 -04:00
import DiscourseURL from "discourse/lib/url";
import Controller from "@ember/controller";
import { alias } from "@ember/object/computed";
2022-03-06 18:30:01 -05:00
import bootbox from "bootbox";
2019-10-22 00:45:51 -04:00
const RECURRING = "recurring";
const ONE_TIME = "one_time";
export default Controller.extend({
2019-11-30 00:39:28 -05:00
// Also defined in settings.
selectedCurrency: alias("model.plan.currency"),
selectedInterval: alias("model.plan.interval"),
@discourseComputed("model.plan.metadata.group_name")
selectedGroup(groupName) {
return groupName || "no-group";
},
@discourseComputed("model.groups")
availableGroups(groups) {
return [
{
id: null,
name: "no-group",
},
...groups,
];
},
2019-11-30 00:39:28 -05:00
@discourseComputed
currencies() {
return [
{ id: "AUD", name: "AUD" },
{ id: "CAD", name: "CAD" },
{ id: "EUR", name: "EUR" },
{ id: "GBP", name: "GBP" },
{ id: "USD", name: "USD" },
2020-06-30 11:13:05 -04:00
{ id: "INR", name: "INR" },
2020-09-16 10:53:50 -04:00
{ id: "BRL", name: "BRL" },
2021-04-06 10:59:27 -04:00
{ id: "DKK", name: "DKK" },
{ id: "SGD", name: "SGD" },
];
},
@discourseComputed
availableIntervals() {
return [
{ id: "day", name: "day" },
{ id: "week", name: "week" },
{ id: "month", name: "month" },
2020-09-16 10:53:50 -04:00
{ id: "year", name: "year" },
];
},
@discourseComputed("model.plan.isNew")
2019-10-23 00:55:06 -04:00
planFieldDisabled(isNew) {
return !isNew;
},
@discourseComputed("model.product.id")
2019-10-23 00:55:06 -04:00
productId(id) {
return id;
},
2019-10-22 00:45:51 -04:00
redirect(product_id) {
2019-10-24 23:00:59 -04:00
DiscourseURL.redirectTo(
2019-12-02 02:58:14 -05:00
`/admin/plugins/discourse-subscriptions/products/${product_id}`
2019-10-24 23:00:59 -04:00
);
2019-10-22 00:45:51 -04:00
},
actions: {
changeRecurring() {
const recurring = this.get("model.plan.isRecurring");
this.set("model.plan.type", recurring ? ONE_TIME : RECURRING);
this.set("model.plan.isRecurring", !recurring);
},
2019-10-22 00:45:51 -04:00
createPlan() {
if (this.model.plan.metadata.group_name === "no-group") {
this.set("model.plan.metadata.group_name", null);
2019-10-23 00:55:06 -04:00
}
2019-10-24 23:00:59 -04:00
this.get("model.plan")
.save()
.then(() => this.redirect(this.productId))
2020-09-16 10:53:50 -04:00
.catch((data) =>
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
2019-10-23 00:55:06 -04:00
},
updatePlan() {
if (this.model.plan.metadata.group_name === "no-group") {
this.set("model.plan.metadata.group_name", null);
}
2019-10-24 23:00:59 -04:00
this.get("model.plan")
.update()
.then(() => this.redirect(this.productId))
2020-09-16 10:53:50 -04:00
.catch((data) =>
bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))
);
2020-09-16 10:53:50 -04:00
},
},
2019-10-22 00:45:51 -04:00
});