diff --git a/assets/javascripts/discourse/components/donation-form.es6 b/assets/javascripts/discourse/components/donation-form.es6 index 75f2ec3..84e446d 100644 --- a/assets/javascripts/discourse/components/donation-form.es6 +++ b/assets/javascripts/discourse/components/donation-form.es6 @@ -1,4 +1 @@ - -export default Ember.Component.extend({ - -}); +export default Ember.Component.extend({}); diff --git a/assets/javascripts/discourse/components/donation-list.js.es6 b/assets/javascripts/discourse/components/donation-list.js.es6 index 57ec8f9..b9fcea7 100644 --- a/assets/javascripts/discourse/components/donation-list.js.es6 +++ b/assets/javascripts/discourse/components/donation-list.js.es6 @@ -1,5 +1,5 @@ export default Ember.Component.extend({ - classNames: 'donation-list', - hasSubscriptions: Ember.computed.notEmpty('subscriptions'), - hasCharges: Ember.computed.notEmpty('charges') + classNames: "donation-list", + hasSubscriptions: Ember.computed.notEmpty("subscriptions"), + hasCharges: Ember.computed.notEmpty("charges") }); diff --git a/assets/javascripts/discourse/components/donation-row.js.es6 b/assets/javascripts/discourse/components/donation-row.js.es6 index 9c00574..fb2ab1a 100644 --- a/assets/javascripts/discourse/components/donation-row.js.es6 +++ b/assets/javascripts/discourse/components/donation-row.js.es6 @@ -1,15 +1,15 @@ -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'; +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"; import showModal from "discourse/lib/show-modal"; export default Ember.Component.extend({ - classNameBindings: [':donation-row', 'canceled', 'updating'], - includePrefix: Ember.computed.or('invoice', 'charge'), - canceled: Ember.computed.equal('subscription.status', 'canceled'), + classNameBindings: [":donation-row", "canceled", "updating"], + includePrefix: Ember.computed.or("invoice", "charge"), + canceled: Ember.computed.equal("subscription.status", "canceled"), - @computed('subscription', 'invoice', 'charge', 'customer') + @computed("subscription", "invoice", "charge", "customer") data(subscription, invoice, charge, customer) { if (subscription) { return $.extend({}, subscription.plan, { @@ -41,22 +41,22 @@ export default Ember.Component.extend({ } }, - @computed('data.currency') + @computed("data.currency") currency(currency) { return currency ? currency.toUpperCase() : null; }, - @computed('data.amount', 'currency') + @computed("data.amount", "currency") amount(amount, currency) { return formatAmount(amount, currency); }, - @computed('data.interval') + @computed("data.interval") interval(interval) { - return interval || 'once'; + return interval || "once"; }, - @computed('data.anchor', 'interval') + @computed("data.anchor", "interval") period(anchor, interval) { return I18n.t(`discourse_donations.period.${interval}`, { anchor: formatAnchor(interval, moment.unix(anchor)) @@ -64,30 +64,33 @@ export default Ember.Component.extend({ }, cancelSubscription() { - const subscriptionId = this.get('subscription.id'); - this.set('updating', true); + const subscriptionId = this.get("subscription.id"); + this.set("updating", true); - ajax('/donate/charges/cancel-subscription', { + ajax("/donate/charges/cancel-subscription", { data: { subscription_id: subscriptionId }, - method: 'put' - }).then(result => { - if (result.success) { - this.set('subscription', result.subscription); - } - }).catch(popupAjaxError).finally(() => { - this.set('updating', false); - }); + method: "put" + }) + .then(result => { + if (result.success) { + this.set("subscription", result.subscription); + } + }) + .catch(popupAjaxError) + .finally(() => { + this.set("updating", false); + }); }, actions: { cancelSubscription() { - showModal('cancel-subscription', { + showModal("cancel-subscription", { model: { - currency: this.get('currency'), - amount: this.get('amount'), - period: this.get('period'), + currency: this.get("currency"), + amount: this.get("amount"), + period: this.get("period"), confirm: () => this.cancelSubscription() } }); diff --git a/assets/javascripts/discourse/components/stripe-card.js.es6 b/assets/javascripts/discourse/components/stripe-card.js.es6 index c123caa..abc9af1 100644 --- a/assets/javascripts/discourse/components/stripe-card.js.es6 +++ b/assets/javascripts/discourse/components/stripe-card.js.es6 @@ -1,6 +1,6 @@ -import { ajax } from 'discourse/lib/ajax'; -import { formatAnchor, zeroDecimalCurrencies } from '../lib/donation-utilities'; -import { default as computed } from 'ember-addons/ember-computed-decorators'; +import { ajax } from "discourse/lib/ajax"; +import { formatAnchor, zeroDecimalCurrencies } from "../lib/donation-utilities"; +import { default as computed } from "ember-addons/ember-computed-decorators"; import { emailValid as emailValidHelper } from "discourse/lib/utilities"; export default Ember.Component.extend({ @@ -13,14 +13,17 @@ export default Ember.Component.extend({ init() { this._super(); - const user = this.get('currentUser'); + const user = this.get("currentUser"); const settings = Discourse.SiteSettings; - this.set('create_accounts', !user && settings.discourse_donations_enable_create_accounts); - this.set('stripe', Stripe(settings.discourse_donations_public_key)); + this.set( + "create_accounts", + !user && settings.discourse_donations_enable_create_accounts + ); + this.set("stripe", Stripe(settings.discourse_donations_public_key)); - const types = settings.discourse_donations_types.split('|') || []; - const amounts = this.get('donateAmounts'); + const types = settings.discourse_donations_types.split("|") || []; + const amounts = this.get("donateAmounts"); this.setProperties({ types, @@ -31,10 +34,13 @@ export default Ember.Component.extend({ @computed causes() { - const categoryEnabled = Discourse.SiteSettings.discourse_donations_cause_category; + const categoryEnabled = + Discourse.SiteSettings.discourse_donations_cause_category; if (categoryEnabled) { - let categoryIds = Discourse.SiteSettings.discourse_donations_causes_categories.split('|'); + let categoryIds = Discourse.SiteSettings.discourse_donations_causes_categories.split( + "|" + ); if (categoryIds.length) { categoryIds = categoryIds.map(Number); @@ -42,7 +48,8 @@ export default Ember.Component.extend({ .get("categoriesList") .filter(c => { return categoryIds.indexOf(c.id) > -1; - }).map(c => { + }) + .map(c => { return { id: c.id, name: c.name @@ -53,13 +60,13 @@ export default Ember.Component.extend({ } } else { const causes = Discourse.SiteSettings.discourse_donations_causes; - return causes ? causes.split('|') : []; + return causes ? causes.split("|") : []; } }, - @computed('types') + @computed("types") donationTypes(types) { - return types.map((type) => { + return types.map(type => { return { id: type, name: I18n.t(`discourse_donations.types.${type}`) @@ -67,16 +74,20 @@ export default Ember.Component.extend({ }); }, - @computed('type') + @computed("type") period(type) { - return I18n.t(`discourse_donations.period.${type}`, { anchor: formatAnchor(type) }); + return I18n.t(`discourse_donations.period.${type}`, { + anchor: formatAnchor(type) + }); }, @computed donateAmounts() { - const setting = Discourse.SiteSettings.discourse_donations_amounts.split('|'); + const setting = Discourse.SiteSettings.discourse_donations_amounts.split( + "|" + ); if (setting.length) { - return setting.map((amount) => { + return setting.map(amount => { return { value: parseInt(amount, 10), name: `${amount}.00` @@ -87,167 +98,177 @@ export default Ember.Component.extend({ } }, - @computed('stripe') + @computed("stripe") card(stripe) { let elements = stripe.elements(); - let card = elements.create('card', { + let card = elements.create("card", { hidePostalCode: !Discourse.SiteSettings.discourse_donations_zip_code }); - card.addEventListener('change', (event) => { + card.addEventListener("change", event => { if (event.error) { - this.set('stripeError', event.error.message); + this.set("stripeError", event.error.message); } else { - this.set('stripeError', ''); + this.set("stripeError", ""); } - if (event.elementType === 'card' && event.complete) { - this.set('stripeReady', true); + if (event.elementType === "card" && event.complete) { + this.set("stripeReady", true); } }); return card; }, - @computed('amount') + @computed("amount") transactionFee(amount) { - const fixed = Discourse.SiteSettings.discourse_donations_transaction_fee_fixed; - const percent = Discourse.SiteSettings.discourse_donations_transaction_fee_percent; - const fee = ((amount + fixed) / (1 - percent)) - amount; + const fixed = + Discourse.SiteSettings.discourse_donations_transaction_fee_fixed; + const percent = + Discourse.SiteSettings.discourse_donations_transaction_fee_percent; + const fee = (amount + fixed) / (1 - percent) - amount; return Math.round(fee * 100) / 100; }, - @computed('amount', 'transactionFee', 'includeTransactionFee') + @computed("amount", "transactionFee", "includeTransactionFee") totalAmount(amount, fee, include) { if (include) return amount + fee; return amount; }, - @computed('email') + @computed("email") emailValid(email) { return emailValidHelper(email); }, - @computed('email', 'emailValid') + @computed("email", "emailValid") showEmailError(email, emailValid) { return email && email.length > 3 && !emailValid; }, - @computed('currentUser', 'emailValid') + @computed("currentUser", "emailValid") userReady(currentUser, emailValid) { return currentUser || emailValid; }, - @computed('cause') + @computed("cause") causeValid(cause) { return cause || !Discourse.SiteSettings.discourse_donations_cause_required; }, - @computed('userReady', 'stripeReady', 'causeValid') + @computed("userReady", "stripeReady", "causeValid") formIncomplete(userReady, stripeReady, causeValid) { return !userReady || !stripeReady || !causeValid; }, - @computed('transactionInProgress', 'formIncomplete') + @computed("transactionInProgress", "formIncomplete") disableSubmit(transactionInProgress, formIncomplete) { return transactionInProgress || formIncomplete; }, didInsertElement() { this._super(); - this.get('card').mount('#card-element'); - Ember.$(document).on('click', Ember.run.bind(this, this.documentClick)); + this.get("card").mount("#card-element"); + Ember.$(document).on("click", Ember.run.bind(this, this.documentClick)); }, willDestroyElement() { - Ember.$(document).off('click', Ember.run.bind(this, this.documentClick)); + Ember.$(document).off("click", Ember.run.bind(this, this.documentClick)); }, documentClick(e) { - let $element = this.$('.transaction-fee-description'); + let $element = this.$(".transaction-fee-description"); let $target = $(e.target); - if ($target.closest($element).length < 1 && - this._state !== 'destroying') { - this.set('showTransactionFeeDescription', false); + if ($target.closest($element).length < 1 && this._state !== "destroying") { + this.set("showTransactionFeeDescription", false); } }, setSuccess() { - this.set('paymentSuccess', true); + this.set("paymentSuccess", true); }, endTranscation() { - this.set('transactionInProgress', false); + this.set("transactionInProgress", false); }, concatMessages(messages) { - this.set('result', this.get('result').concat(messages)); + this.set("result", this.get("result").concat(messages)); }, actions: { toggleTransactionFeeDescription() { - this.toggleProperty('showTransactionFeeDescription'); + this.toggleProperty("showTransactionFeeDescription"); }, submitStripeCard() { let self = this; - this.set('transactionInProgress', true); + this.set("transactionInProgress", true); - this.get('stripe').createToken(this.get('card')).then(data => { - self.set('result', []); + this.get("stripe") + .createToken(this.get("card")) + .then(data => { + self.set("result", []); - if (data.error) { - this.setProperties({ - stripeError: data.error.message, - stripeReady: false - }); - self.endTranscation(); - } else { - const settings = Discourse.SiteSettings; - - const transactionFeeEnabled = settings.discourse_donations_enable_transaction_fee; - let amount = transactionFeeEnabled ? this.get('totalAmount') : this.get('amount'); - - if (zeroDecimalCurrencies.indexOf(settings.discourse_donations_currency) === -1) { - amount = amount * 100; - } - - let params = { - stripeToken: data.token.id, - cause: self.get('cause'), - type: self.get('type'), - amount, - email: self.get('email'), - username: self.get('username'), - create_account: self.get('create_accounts') - }; - - if(!self.get('paymentSuccess')) { - ajax('/donate/charges', { - data: params, - method: 'post' - }).then(result => { - if (result.subscription) { - let subscription = $.extend({}, result.subscription, { - new: true - }); - this.get('subscriptions').unshiftObject(subscription); - } - - if (result.charge) { - let charge = $.extend({}, result.charge, { - new: true - }); - this.get('charges').unshiftObject(charge); - } - - self.concatMessages(result.messages); - - self.endTranscation(); + if (data.error) { + this.setProperties({ + stripeError: data.error.message, + stripeReady: false }); + self.endTranscation(); + } else { + const settings = Discourse.SiteSettings; + + const transactionFeeEnabled = + settings.discourse_donations_enable_transaction_fee; + let amount = transactionFeeEnabled + ? this.get("totalAmount") + : this.get("amount"); + + if ( + zeroDecimalCurrencies.indexOf( + settings.discourse_donations_currency + ) === -1 + ) { + amount = amount * 100; + } + + let params = { + stripeToken: data.token.id, + cause: self.get("cause"), + type: self.get("type"), + amount, + email: self.get("email"), + username: self.get("username"), + create_account: self.get("create_accounts") + }; + + if (!self.get("paymentSuccess")) { + ajax("/donate/charges", { + data: params, + method: "post" + }).then(result => { + if (result.subscription) { + let subscription = $.extend({}, result.subscription, { + new: true + }); + this.get("subscriptions").unshiftObject(subscription); + } + + if (result.charge) { + let charge = $.extend({}, result.charge, { + new: true + }); + this.get("charges").unshiftObject(charge); + } + + self.concatMessages(result.messages); + + self.endTranscation(); + }); + } } - } - }); + }); } } }); diff --git a/assets/javascripts/discourse/controllers/cancel-subscription.js.es6 b/assets/javascripts/discourse/controllers/cancel-subscription.js.es6 index 5afc458..c6949b0 100644 --- a/assets/javascripts/discourse/controllers/cancel-subscription.js.es6 +++ b/assets/javascripts/discourse/controllers/cancel-subscription.js.es6 @@ -1,12 +1,12 @@ export default Ember.Controller.extend({ actions: { confirm() { - this.get('model.confirm')(); - this.send('closeModal'); + this.get("model.confirm")(); + this.send("closeModal"); }, cancel() { - this.send('closeModal'); + this.send("closeModal"); } } }); diff --git a/assets/javascripts/discourse/controllers/donate.js.es6 b/assets/javascripts/discourse/controllers/donate.js.es6 index cd4b511..e2c7f63 100644 --- a/assets/javascripts/discourse/controllers/donate.js.es6 +++ b/assets/javascripts/discourse/controllers/donate.js.es6 @@ -1,54 +1,59 @@ -import { default as computed } from 'ember-addons/ember-computed-decorators'; -import { popupAjaxError } from 'discourse/lib/ajax-error'; -import { ajax } from 'discourse/lib/ajax'; -import { getOwner } from 'discourse-common/lib/get-owner'; +import { default as computed } from "ember-addons/ember-computed-decorators"; +import { popupAjaxError } from "discourse/lib/ajax-error"; +import { ajax } from "discourse/lib/ajax"; +import { getOwner } from "discourse-common/lib/get-owner"; import { emailValid } from "discourse/lib/utilities"; export default Ember.Controller.extend({ loadingDonations: false, - loadDonationsDisabled: Ember.computed.not('emailVaild'), + loadDonationsDisabled: Ember.computed.not("emailVaild"), - @computed('charges.[]', 'subscriptions.[]') + @computed("charges.[]", "subscriptions.[]") hasDonations(charges, subscriptions) { - return (charges && charges.length > 0) || - (subscriptions && subscriptions.length > 0); + return ( + (charges && charges.length > 0) || + (subscriptions && subscriptions.length > 0) + ); }, - @computed('email') + @computed("email") emailVaild(email) { return emailValid(email); }, actions: { loadDonations() { - let email = this.get('email'); + let email = this.get("email"); - this.set('loadingDonations', true); + this.set("loadingDonations", true); - ajax('/donate/charges', { + ajax("/donate/charges", { data: { email }, - type: 'GET' - }).then((result) => { - this.setProperties({ - charges: Ember.A(result.charges), - subscriptions: Ember.A(result.subscriptions), - customer: result.customer - }); - }).catch(popupAjaxError).finally(() => { - this.setProperties({ - loadingDonations: false, - hasEmailResult: true - }); + type: "GET" + }) + .then(result => { + this.setProperties({ + charges: Ember.A(result.charges), + subscriptions: Ember.A(result.subscriptions), + customer: result.customer + }); + }) + .catch(popupAjaxError) + .finally(() => { + this.setProperties({ + loadingDonations: false, + hasEmailResult: true + }); - Ember.run.later(() => { - this.set('hasEmailResult', false); - }, 6000); - }); + Ember.run.later(() => { + this.set("hasEmailResult", false); + }, 6000); + }); }, showLogin() { - const controller = getOwner(this).lookup('route:application'); - controller.send('showLogin'); + const controller = getOwner(this).lookup("route:application"); + controller.send("showLogin"); } } }); diff --git a/assets/javascripts/discourse/discourse-payments-route-map.js.es6 b/assets/javascripts/discourse/discourse-payments-route-map.js.es6 index 0ff4ef7..dbf4000 100644 --- a/assets/javascripts/discourse/discourse-payments-route-map.js.es6 +++ b/assets/javascripts/discourse/discourse-payments-route-map.js.es6 @@ -1,3 +1,3 @@ export default function() { - this.route('donate'); -}; + this.route("donate"); +} diff --git a/assets/javascripts/discourse/initializers/donations-edits.js.es6 b/assets/javascripts/discourse/initializers/donations-edits.js.es6 index 928d01c..f6b7b0f 100644 --- a/assets/javascripts/discourse/initializers/donations-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/donations-edits.js.es6 @@ -1,34 +1,49 @@ -import { withPluginApi } from 'discourse/lib/plugin-api'; +import { withPluginApi } from "discourse/lib/plugin-api"; export default { - name: 'donations-edits', + name: "donations-edits", initialize(container) { - const siteSettings = container.lookup('site-settings:main'); + const siteSettings = container.lookup("site-settings:main"); - withPluginApi('0.8.12', api => { + withPluginApi("0.8.12", api => { api.decorateCooked($post => { - const $form = $post.find('.stripe-checkout'); + const $form = $post.find(".stripe-checkout"); if ($form.length) { - const $input = $form.find('input'); - var s = document.createElement('script'); - s.src = 'https://checkout.stripe.com/checkout.js'; - s.setAttribute('class', 'stripe-button'); - s.setAttribute('data-key', siteSettings.discourse_donations_public_key); - s.setAttribute('data-amount', $input.attr('amount')); - s.setAttribute('data-name', siteSettings.discourse_donations_shop_name); - s.setAttribute('data-description', $form.attr('content')); - s.setAttribute('data-image', $form.attr('image') || ''); - s.setAttribute('data-locale', 'auto'); - s.setAttribute('data-zip-code', siteSettings.discourse_donations_zip_code); - s.setAttribute('data-billing-address', siteSettings.discourse_donations_billing_address); - s.setAttribute('data-currency', siteSettings.discourse_donations_currency); + const $input = $form.find("input"); + var s = document.createElement("script"); + s.src = "https://checkout.stripe.com/checkout.js"; + s.setAttribute("class", "stripe-button"); + s.setAttribute( + "data-key", + siteSettings.discourse_donations_public_key + ); + s.setAttribute("data-amount", $input.attr("amount")); + s.setAttribute( + "data-name", + siteSettings.discourse_donations_shop_name + ); + s.setAttribute("data-description", $form.attr("content")); + s.setAttribute("data-image", $form.attr("image") || ""); + s.setAttribute("data-locale", "auto"); + s.setAttribute( + "data-zip-code", + siteSettings.discourse_donations_zip_code + ); + s.setAttribute( + "data-billing-address", + siteSettings.discourse_donations_billing_address + ); + s.setAttribute( + "data-currency", + siteSettings.discourse_donations_currency + ); $form.append(s); } }); if (siteSettings.discourse_donations_cause_category) { - api.decorateWidget('category-header-widget:after', helper => { - helper.widget.appEvents.on('page:changed', () => { + api.decorateWidget("category-header-widget:after", helper => { + helper.widget.appEvents.on("page:changed", () => { helper.widget.scheduleRerender(); }); }); diff --git a/assets/javascripts/discourse/lib/donation-utilities.js.es6 b/assets/javascripts/discourse/lib/donation-utilities.js.es6 index 924ee4f..619fde4 100644 --- a/assets/javascripts/discourse/lib/donation-utilities.js.es6 +++ b/assets/javascripts/discourse/lib/donation-utilities.js.es6 @@ -1,27 +1,44 @@ const formatAnchor = function(type = null, time = moment()) { let format; - switch(type) { - case 'once': - format = 'Do MMMM YYYY'; + switch (type) { + case "once": + format = "Do MMMM YYYY"; break; - case 'week': - format = 'dddd'; + case "week": + format = "dddd"; break; - case 'month': - format = 'Do'; + case "month": + format = "Do"; break; - case 'year': - format = 'MMMM D'; + case "year": + format = "MMMM D"; break; default: - format = 'dddd'; + format = "dddd"; } return moment(time).format(format); }; -const zeroDecimalCurrencies = ['MGA', 'BIF', 'CLP', 'PYG', 'DFJ', 'RWF', 'GNF', 'UGX', 'JPY', 'VND', 'VUV', 'XAF', 'KMF', 'KRW', 'XOF', 'XPF']; +const zeroDecimalCurrencies = [ + "MGA", + "BIF", + "CLP", + "PYG", + "DFJ", + "RWF", + "GNF", + "UGX", + "JPY", + "VND", + "VUV", + "XAF", + "KMF", + "KRW", + "XOF", + "XPF" +]; const formatAmount = function(amount, currency) { let zeroDecimal = zeroDecimalCurrencies.indexOf(currency) > -1; diff --git a/assets/javascripts/discourse/routes/donate.js.es6 b/assets/javascripts/discourse/routes/donate.js.es6 index 4a52b8c..d9ed51a 100644 --- a/assets/javascripts/discourse/routes/donate.js.es6 +++ b/assets/javascripts/discourse/routes/donate.js.es6 @@ -1,12 +1,12 @@ import DiscourseRoute from "discourse/routes/discourse"; -import DiscourseURL from 'discourse/lib/url'; -import { popupAjaxError } from 'discourse/lib/ajax-error'; -import { ajax } from 'discourse/lib/ajax'; +import DiscourseURL from "discourse/lib/url"; +import { popupAjaxError } from "discourse/lib/ajax-error"; +import { ajax } from "discourse/lib/ajax"; export default DiscourseRoute.extend({ redirect() { if (!Discourse.SiteSettings.discourse_donations_enabled) { - DiscourseURL.routeTo('/'); + DiscourseURL.routeTo("/"); return; } }, @@ -16,22 +16,25 @@ export default DiscourseRoute.extend({ let subscriptions = []; let customer = {}; - controller.set('loadingDonations', true); + controller.set("loadingDonations", true); - ajax('/donate/charges').then((result) => { - if (result) { - charges = result.charges; - subscriptions = result.subscriptions; - customer = result.customer; - } + ajax("/donate/charges") + .then(result => { + if (result) { + charges = result.charges; + subscriptions = result.subscriptions; + customer = result.customer; + } - controller.setProperties({ - charges: Ember.A(charges), - subscriptions: Ember.A(subscriptions), - customer + controller.setProperties({ + charges: Ember.A(charges), + subscriptions: Ember.A(subscriptions), + customer + }); + }) + .catch(popupAjaxError) + .finally(() => { + controller.set("loadingDonations", false); }); - }).catch(popupAjaxError).finally(() => { - controller.set('loadingDonations', false); - }); } }); diff --git a/assets/javascripts/discourse/widgets/donations-category-header-widget.js.es6 b/assets/javascripts/discourse/widgets/donations-category-header-widget.js.es6 index 354c45d..9f345cb 100644 --- a/assets/javascripts/discourse/widgets/donations-category-header-widget.js.es6 +++ b/assets/javascripts/discourse/widgets/donations-category-header-widget.js.es6 @@ -1,86 +1,96 @@ -import { createWidget } from 'discourse/widgets/widget'; -import { h } from 'virtual-dom'; -import { avatarFor } from 'discourse/widgets/post'; +import { createWidget } from "discourse/widgets/widget"; +import { h } from "virtual-dom"; +import { avatarFor } from "discourse/widgets/post"; import { userPath } from "discourse/lib/url"; function donationDisplay(amount, type) { return h(`div.donations-${type}`, [ - h('span', I18n.t(`discourse_donations.cause.category.${type}`)), - h('span', `$${(amount/100).toFixed(2)}`) + h("span", I18n.t(`discourse_donations.cause.category.${type}`)), + h("span", `$${(amount / 100).toFixed(2)}`) ]); } -createWidget('category-header-widget', { - tagName: 'span', +createWidget("category-header-widget", { + tagName: "span", html(args) { - - const controller = this.register.lookup('controller:navigation/category'); + const controller = this.register.lookup("controller:navigation/category"); const category = controller.get("category"); - if (args.currentPath.toLowerCase().indexOf('category') > -1 && - category && - category.donations_cause) { + if ( + args.currentPath.toLowerCase().indexOf("category") > -1 && + category && + category.donations_cause + ) { $("body").addClass("donations-category"); let contents = [ - h('div.donations-category-contents', [ - h('h1', category.name), - h('div.category-title-description', h('p', category.description_text)) + h("div.donations-category-contents", [ + h("h1", category.name), + h("div.category-title-description", h("p", category.description_text)) ]) ]; let metadata = []; if (category.donations_total !== undefined) { - metadata.push(donationDisplay(category.donations_total || 0, 'total')); + metadata.push(donationDisplay(category.donations_total || 0, "total")); if (Discourse.SiteSettings.discourse_donations_cause_month) { - metadata.push(donationDisplay(category.donations_month || 0, 'month')); + metadata.push( + donationDisplay(category.donations_month || 0, "month") + ); } } if (category.donations_github) { metadata.push( - h('div.donations-github', this.attach('link', { - icon: 'github', - label: 'discourse_donations.cause.github.label', - href: category.donations_github - })) + h( + "div.donations-github", + this.attach("link", { + icon: "github", + label: "discourse_donations.cause.github.label", + href: category.donations_github + }) + ) ); } if (category.donations_meta) { metadata.push( - h('div.donations-meta', this.attach('link', { - href: category.donations_meta, - contents: () => { - return [ - h('img.meta-icon', { - attributes: { - src: 'https://discourse-meta.s3.dualstack.us-west-1.amazonaws.com/original/3X/b/1/b19ba793155a785bbd9707bc0cabbd3a987fa126.png?v=6' - } - }), - h('span', I18n.t('discourse_donations.cause.meta.label')) - ]; - } - })) + h( + "div.donations-meta", + this.attach("link", { + href: category.donations_meta, + contents: () => { + return [ + h("img.meta-icon", { + attributes: { + src: + "https://discourse-meta.s3.dualstack.us-west-1.amazonaws.com/original/3X/b/1/b19ba793155a785bbd9707bc0cabbd3a987fa126.png?v=6" + } + }), + h("span", I18n.t("discourse_donations.cause.meta.label")) + ]; + } + }) + ) ); } if (category.donations_release_oldest) { - let releaseArray = category.donations_release_oldest.split('/'); + let releaseArray = category.donations_release_oldest.split("/"); let label = releaseArray[releaseArray.length - 1]; metadata.push( - h('div.donations-release-oldest', [ - h('span', '>='), - this.attach('link', { + h("div.donations-release-oldest", [ + h("span", ">="), + this.attach("link", { href: category.donations_release_oldest, - icon: 'tag', + icon: "tag", rawLabel: label, omitSpan: true, attributes: { - target: '_blank' + target: "_blank" } }) ]) @@ -88,18 +98,18 @@ createWidget('category-header-widget', { } if (category.donations_release_latest) { - let releaseArray = category.donations_release_latest.split('/'); + let releaseArray = category.donations_release_latest.split("/"); let label = releaseArray[releaseArray.length - 1]; metadata.push( - h('div.donations-release-latest', [ - h('span', '<='), - this.attach('link', { + h("div.donations-release-latest", [ + h("span", "<="), + this.attach("link", { href: category.donations_release_latest, - icon: 'tag', + icon: "tag", rawLabel: label, omitSpan: true, attributes: { - target: '_blank' + target: "_blank" } }) ]) @@ -107,61 +117,78 @@ createWidget('category-header-widget', { } if (metadata.length) { - contents.push(h('div.donations-category-metadata', metadata)); + contents.push(h("div.donations-category-metadata", metadata)); } let users = []; if (category.donations_backers.length) { - users.push(h('div.donations-backers', [ - h('div.donations-backers-title', I18n.t('discourse_donations.cause.backers.label')), - category.donations_backers.map(user => { - if (user) { - return avatarFor('medium', { - template: user.avatar_template, - username: user.username, - name: user.name, - url: userPath(user.username), - className: "backer-avatar" - }); - } else { - return; - } - }) - ])); - }; + users.push( + h("div.donations-backers", [ + h( + "div.donations-backers-title", + I18n.t("discourse_donations.cause.backers.label") + ), + category.donations_backers.map(user => { + if (user) { + return avatarFor("medium", { + template: user.avatar_template, + username: user.username, + name: user.name, + url: userPath(user.username), + className: "backer-avatar" + }); + } else { + return; + } + }) + ]) + ); + } if (category.donations_maintainers.length) { - let maintainersLabel = category.donations_maintainers_label || - I18n.t('discourse_donations.cause.maintainers.label'); + let maintainersLabel = + category.donations_maintainers_label || + I18n.t("discourse_donations.cause.maintainers.label"); - users.push(h('div.donations-maintainers', [ - h('div.donations-maintainers-title', maintainersLabel), - category.donations_maintainers.map(user => { - if (user) { - return avatarFor('medium', { - template: user.avatar_template, - username: user.username, - name: user.name, - url: userPath(user.username), - className: "maintainer-avatar" - }); - } else { - return; - } - }) - ])); + users.push( + h("div.donations-maintainers", [ + h("div.donations-maintainers-title", maintainersLabel), + category.donations_maintainers.map(user => { + if (user) { + return avatarFor("medium", { + template: user.avatar_template, + username: user.username, + name: user.name, + url: userPath(user.username), + className: "maintainer-avatar" + }); + } else { + return; + } + }) + ]) + ); } if (users.length) { - contents.push(h('div.donations-category-users', users)); + contents.push(h("div.donations-category-users", users)); } - return h('div.donations-category-header', { - "attributes" : { - "style" : "background-color: #" + category.color + "; color: #" + category.text_color + ";" - } - }, contents); + return h( + "div.donations-category-header", + { + attributes: { + style: + "background-color: #" + + category.color + + "; color: #" + + category.text_color + + ";" + } + }, + contents + ); } else { $("body").removeClass("donations-category"); } diff --git a/assets/javascripts/lib/discourse-markdown/stripe.js.es6 b/assets/javascripts/lib/discourse-markdown/stripe.js.es6 index 48ec98c..3ce2bee 100644 --- a/assets/javascripts/lib/discourse-markdown/stripe.js.es6 +++ b/assets/javascripts/lib/discourse-markdown/stripe.js.es6 @@ -1,90 +1,104 @@ function validationErrors(tagInfo, content, siteSettings) { - let errors = []; - if (!siteSettings.discourse_donations_public_key) { errors.push("missing key (site setting)"); } - if (!siteSettings.discourse_donations_currency) { errors.push("missing currency (site setting)"); } - if (!siteSettings.discourse_donations_shop_name) { errors.push("missing name (site setting)"); } - if (!siteSettings.discourse_donations_zip_code) { errors.push("missing zip code toggle (site setting)"); } - if (!siteSettings.discourse_donations_billing_address) { errors.push("missing billing address toggle (site setting)"); } - if (!tagInfo.attrs['amount']) { errors.push("missing amount"); } - if (!content) { errors.push("missing description"); } - return errors; + let errors = []; + if (!siteSettings.discourse_donations_public_key) { + errors.push("missing key (site setting)"); + } + if (!siteSettings.discourse_donations_currency) { + errors.push("missing currency (site setting)"); + } + if (!siteSettings.discourse_donations_shop_name) { + errors.push("missing name (site setting)"); + } + if (!siteSettings.discourse_donations_zip_code) { + errors.push("missing zip code toggle (site setting)"); + } + if (!siteSettings.discourse_donations_billing_address) { + errors.push("missing billing address toggle (site setting)"); + } + if (!tagInfo.attrs["amount"]) { + errors.push("missing amount"); + } + if (!content) { + errors.push("missing description"); + } + return errors; } function replaceWithStripeOrError(siteSettings) { - return function (state, tagInfo, content) { - let errors = validationErrors(tagInfo, content, siteSettings); - if (errors.length) { - displayErrors(state, errors); - } else { - insertCheckout(state, tagInfo, content); - } - return true; - }; + return function(state, tagInfo, content) { + let errors = validationErrors(tagInfo, content, siteSettings); + if (errors.length) { + displayErrors(state, errors); + } else { + insertCheckout(state, tagInfo, content); + } + return true; + }; } function displayErrors(state, errors) { - let token = state.push('div-open', 'div', 1); - token.attrs = [['class', 'stripe-errors']]; - token = state.push('html_inline', '', 0); - token.content = "Stripe checkout can't be rendered: " + errors.join(", "); - state.push('div-close', 'div', -1); + let token = state.push("div-open", "div", 1); + token.attrs = [["class", "stripe-errors"]]; + token = state.push("html_inline", "", 0); + token.content = "Stripe checkout can't be rendered: " + errors.join(", "); + state.push("div-close", "div", -1); } function insertCheckout(state, tagInfo, content) { - let token = state.push('stripe-checkout-form-open', 'form', 1); - token.attrs = [ - ['method', 'POST'], - ['action', '/checkout'], - ['content', content], - ['image', tagInfo.attrs['image']], - ['class', 'stripe-checkout'] - ]; + let token = state.push("stripe-checkout-form-open", "form", 1); + token.attrs = [ + ["method", "POST"], + ["action", "/checkout"], + ["content", content], + ["image", tagInfo.attrs["image"]], + ["class", "stripe-checkout"] + ]; - token = state.push('stripe-checkout-form-amount', 'input', 0); - token.attrs = [ - ['type', 'hidden'], - ['name', 'amount'], - ['value', tagInfo.attrs['amount']] - ]; + token = state.push("stripe-checkout-form-amount", "input", 0); + token.attrs = [ + ["type", "hidden"], + ["name", "amount"], + ["value", tagInfo.attrs["amount"]] + ]; - state.push('stripe-checkout-form-close', 'form', -1); + state.push("stripe-checkout-form-close", "form", -1); } function setupMarkdownIt(helper, siteSettings) { - helper.registerPlugin(md => { - md.inline.bbcode.ruler.push('stripe-checkout', { - tag: 'stripe', - replace: replaceWithStripeOrError(siteSettings) - }); + helper.registerPlugin(md => { + md.inline.bbcode.ruler.push("stripe-checkout", { + tag: "stripe", + replace: replaceWithStripeOrError(siteSettings) }); + }); } export function setup(helper) { - helper.registerOptions((opts,siteSettings)=>{ - helper.whiteList([ - 'div[class]', - 'form[method]', - 'form[action]', - 'form[class]', - 'form[content]', - 'form[image]', - 'input[type]', - 'input[name]', - 'input[value]', - 'script[class]', - 'script[src]', - 'script[data-key]', - 'script[data-amount]', - 'script[data-name]', - 'script[data-description]', - 'script[data-image]', - 'script[data-zip-code]', - 'script[data-billing-address]', - 'script[data-currency]', - 'script[data-locale]' - ]); - if (helper.markdownIt) { - setupMarkdownIt(helper, siteSettings); - } - }); + helper.registerOptions((opts, siteSettings) => { + helper.whiteList([ + "div[class]", + "form[method]", + "form[action]", + "form[class]", + "form[content]", + "form[image]", + "input[type]", + "input[name]", + "input[value]", + "script[class]", + "script[src]", + "script[data-key]", + "script[data-amount]", + "script[data-name]", + "script[data-description]", + "script[data-image]", + "script[data-zip-code]", + "script[data-billing-address]", + "script[data-currency]", + "script[data-locale]" + ]); + if (helper.markdownIt) { + setupMarkdownIt(helper, siteSettings); + } + }); } diff --git a/assets/stylesheets/common/discourse-donations.scss b/assets/stylesheets/common/discourse-donations.scss index de07ac2..4b2264b 100644 --- a/assets/stylesheets/common/discourse-donations.scss +++ b/assets/stylesheets/common/discourse-donations.scss @@ -28,7 +28,8 @@ div.stripe-errors { margin: 0; } - .error, .stripe-error { + .error, + .stripe-error { margin-top: 5px; color: $danger; } @@ -50,7 +51,7 @@ div.stripe-errors { background-color: $secondary; border: 1px solid $primary-low; padding: 10px; - box-shadow: 0 2px 2px rgba(0,0,0,0.4); + box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4); width: 400px; z-index: 1; } @@ -59,7 +60,8 @@ div.stripe-errors { margin-bottom: 20px; .donation-list { - .subscription-list, .charge-list { + .subscription-list, + .charge-list { margin-bottom: 10px; > ul { @@ -120,7 +122,7 @@ div.stripe-errors { text-align: center; i { - margin-right: .25em; + margin-right: 0.25em; font-size: 1.5em; } @@ -147,11 +149,13 @@ div.stripe-errors { justify-content: space-around; font-size: 1.2rem; - .donations-total span:first-of-type, .donations-month span:first-of-type { + .donations-total span:first-of-type, + .donations-month span:first-of-type { margin-right: 5px; } - .donations-github a, .donations-meta a { + .donations-github a, + .donations-meta a { color: inherit; } @@ -167,7 +171,8 @@ div.stripe-errors { } } - .donations-release-latest, .donations-release-oldest { + .donations-release-latest, + .donations-release-oldest { display: flex; align-items: center; @@ -189,15 +194,18 @@ div.stripe-errors { display: flex; align-items: center; - .donations-backers, .donations-maintainers { + .donations-backers, + .donations-maintainers { flex: 1 1 auto; } - .backer-avatar, .maintainer-avatar { + .backer-avatar, + .maintainer-avatar { margin: 0 5px; } - .donations-backers-title, .donations-maintainers-title { + .donations-backers-title, + .donations-maintainers-title { padding-bottom: 10px; } } diff --git a/test/javascripts/components/donation-form-test.es6 b/test/javascripts/components/donation-form-test.es6 index 58bb08a..d4ccca7 100644 --- a/test/javascripts/components/donation-form-test.es6 +++ b/test/javascripts/components/donation-form-test.es6 @@ -1,8 +1,8 @@ -import componentTest from 'helpers/component-test'; +import componentTest from "helpers/component-test"; -moduleForComponent('donation-form', { integration: true }); +moduleForComponent("donation-form", { integration: true }); -componentTest('donation form', { +componentTest("donation form", { template: `{{donation-form}}`, test(assert) { diff --git a/test/javascripts/components/donation-row-test.js.es6 b/test/javascripts/components/donation-row-test.js.es6 index ae17141..46479f1 100644 --- a/test/javascripts/components/donation-row-test.js.es6 +++ b/test/javascripts/components/donation-row-test.js.es6 @@ -1,26 +1,26 @@ -import componentTest from 'helpers/component-test'; +import componentTest from "helpers/component-test"; -moduleForComponent('donation-row', { integration: true }); +moduleForComponent("donation-row", { integration: true }); -componentTest('donation-row', { +componentTest("donation-row", { template: `{{donation-row currency=3 amount=21 period='monthly'}}`, test(assert) { - assert.equal(find('.donation-row-currency').text(), '3'); - assert.equal(find('.donation-row-amount').text(), '21'); - assert.equal(find('.donation-row-period').text(), 'monthly'); - }, + assert.equal(find(".donation-row-currency").text(), "3"); + assert.equal(find(".donation-row-amount").text(), "21"); + assert.equal(find(".donation-row-period").text(), "monthly"); + } }); -componentTest('donation-row cancels subscription', { +componentTest("donation-row cancels subscription", { template: `{{donation-row currentUser=currentUser subscription=subscription}}`, beforeEach() { - this.set('currentUser', true); - this.set('subscription', true); + this.set("currentUser", true); + this.set("subscription", true); }, async test(assert) { - assert.ok(find('.donation-row-subscription').length); - }, + assert.ok(find(".donation-row-subscription").length); + } }); diff --git a/test/javascripts/components/stripe-card-test.js.es6 b/test/javascripts/components/stripe-card-test.js.es6 index 17ad420..0dc1e47 100644 --- a/test/javascripts/components/stripe-card-test.js.es6 +++ b/test/javascripts/components/stripe-card-test.js.es6 @@ -1,6 +1,6 @@ -import componentTest from 'helpers/component-test'; +import componentTest from "helpers/component-test"; -moduleForComponent('stripe-card', { integration: true }); +moduleForComponent("stripe-card", { integration: true }); window.Stripe = function() { return { @@ -13,18 +13,18 @@ window.Stripe = function() { }; } }; - }, + } }; }; -componentTest('stripe card', { +componentTest("stripe card", { template: `{{stripe-card donateAmounts=donateAmounts}}`, skip: true, beforeEach() { - Discourse.SiteSettings.discourse_donations_types = ''; - this.set('donateAmounts', [{ value: 2 }]); + Discourse.SiteSettings.discourse_donations_types = ""; + this.set("donateAmounts", [{ value: 2 }]); }, test(assert) {