Files
discourse-subscriptions/assets/javascripts/discourse/components/donation-row.js.es6
T

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-08-27 20:37:20 +10:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { formatAnchor, formatAmount } from "../lib/donation-utilities";
import { default as computed } from "ember-addons/ember-computed-decorators";
2018-06-28 09:32:58 +10:00
import showModal from "discourse/lib/show-modal";
export default Ember.Component.extend({
2019-08-27 20:37:20 +10:00
classNameBindings: [":donation-row", "canceled", "updating"],
includePrefix: Ember.computed.or("invoice", "charge"),
canceled: Ember.computed.equal("subscription.status", "canceled"),
2018-06-28 09:32:58 +10:00
2019-08-27 20:37:20 +10:00
@computed("subscription", "invoice", "charge", "customer")
2018-06-28 09:32:58 +10:00
data(subscription, invoice, charge, customer) {
if (subscription) {
return $.extend({}, subscription.plan, {
anchor: subscription.billing_cycle_anchor
});
} else if (invoice) {
let receiptSent = false;
if (invoice.receipt_number && customer.email) {
receiptSent = true;
}
return $.extend({}, invoice.lines.data[0], {
anchor: invoice.date,
invoiceLink: invoice.invoice_pdf,
receiptSent
});
} else if (charge) {
let receiptSent = false;
if (charge.receipt_number && charge.receipt_email) {
receiptSent = true;
}
return $.extend({}, charge, {
anchor: charge.created,
receiptSent
});
}
},
2019-08-27 20:37:20 +10:00
@computed("data.currency")
2018-06-28 09:32:58 +10:00
currency(currency) {
return currency ? currency.toUpperCase() : null;
},
2019-08-27 20:37:20 +10:00
@computed("data.amount", "currency")
2018-06-28 09:32:58 +10:00
amount(amount, currency) {
return formatAmount(amount, currency);
},
2019-08-27 20:37:20 +10:00
@computed("data.interval")
2018-06-28 09:32:58 +10:00
interval(interval) {
2019-08-27 20:37:20 +10:00
return interval || "once";
2018-06-28 09:32:58 +10:00
},
2019-08-27 20:37:20 +10:00
@computed("data.anchor", "interval")
2018-06-28 09:32:58 +10:00
period(anchor, interval) {
return I18n.t(`discourse_donations.period.${interval}`, {
anchor: formatAnchor(interval, moment.unix(anchor))
2018-09-22 14:03:30 +10:00
});
2018-06-28 09:32:58 +10:00
},
cancelSubscription() {
2019-08-27 20:37:20 +10:00
const subscriptionId = this.get("subscription.id");
this.set("updating", true);
2018-06-28 09:32:58 +10:00
2019-08-27 20:37:20 +10:00
ajax("/donate/charges/cancel-subscription", {
2018-06-28 09:32:58 +10:00
data: {
subscription_id: subscriptionId
},
2019-08-27 20:37:20 +10:00
method: "put"
})
.then(result => {
if (result.success) {
this.set("subscription", result.subscription);
}
})
.catch(popupAjaxError)
.finally(() => {
this.set("updating", false);
});
2018-06-28 09:32:58 +10:00
},
actions: {
cancelSubscription() {
2019-08-27 20:37:20 +10:00
showModal("cancel-subscription", {
2018-06-28 09:32:58 +10:00
model: {
2019-08-27 20:37:20 +10:00
currency: this.get("currency"),
amount: this.get("amount"),
period: this.get("period"),
2018-06-28 09:32:58 +10:00
confirm: () => this.cancelSubscription()
}
});
}
}
2018-09-22 14:03:30 +10:00
});