diff --git a/app/controllers/discourse_subscriptions/subscribe_controller.rb b/app/controllers/discourse_subscriptions/subscribe_controller.rb index 65973b6..476d551 100644 --- a/app/controllers/discourse_subscriptions/subscribe_controller.rb +++ b/app/controllers/discourse_subscriptions/subscribe_controller.rb @@ -5,7 +5,7 @@ module DiscourseSubscriptions include DiscourseSubscriptions::Stripe include DiscourseSubscriptions::Group before_action :set_api_key - requires_login + requires_login except: [:index, :show] def index begin diff --git a/assets/javascripts/discourse/components/login-required.js.es6 b/assets/javascripts/discourse/components/login-required.js.es6 new file mode 100644 index 0000000..fc7682d --- /dev/null +++ b/assets/javascripts/discourse/components/login-required.js.es6 @@ -0,0 +1,17 @@ +import Component from "@ember/component"; +import cookie from "discourse/lib/cookie"; +import DiscourseURL from "discourse/lib/url"; +import { default as getURL } from "discourse-common/lib/get-url"; + +export default Component.extend({ + classNames: ["login-required", "subscriptions"], + actions: { + createAccount() { + const destinationUrl = window.location.href; + const cookiePath = getURL("/"); + + cookie("destination_url", destinationUrl, { path: cookiePath }); + DiscourseURL.redirectTo("/login"); + }, + }, +}); diff --git a/assets/javascripts/discourse/components/product-list.js.es6 b/assets/javascripts/discourse/components/product-list.js.es6 index ac84099..11457ee 100644 --- a/assets/javascripts/discourse/components/product-list.js.es6 +++ b/assets/javascripts/discourse/components/product-list.js.es6 @@ -1,17 +1,12 @@ import discourseComputed from "discourse-common/utils/decorators"; -import User from "discourse/models/user"; import { isEmpty } from "@ember/utils"; import Component from "@ember/component"; export default Component.extend({ classNames: ["product-list"], + @discourseComputed("products") emptyProducts(products) { return isEmpty(products); }, - - @discourseComputed() - isLoggedIn() { - return User.current(); - }, }); diff --git a/assets/javascripts/discourse/controllers/s-index.js.es6 b/assets/javascripts/discourse/controllers/s-index.js.es6 index fa4ba1e..6a5f0ca 100644 --- a/assets/javascripts/discourse/controllers/s-index.js.es6 +++ b/assets/javascripts/discourse/controllers/s-index.js.es6 @@ -1,3 +1,10 @@ import Controller from "@ember/controller"; +import discourseComputed from "discourse-common/utils/decorators"; +import User from "discourse/models/user"; -export default Controller.extend({}); +export default Controller.extend({ + @discourseComputed() + isLoggedIn() { + return User.current(); + }, +}); diff --git a/assets/javascripts/discourse/controllers/s-show.js.es6 b/assets/javascripts/discourse/controllers/s-show.js.es6 index 315a7f8..73755af 100644 --- a/assets/javascripts/discourse/controllers/s-show.js.es6 +++ b/assets/javascripts/discourse/controllers/s-show.js.es6 @@ -5,6 +5,7 @@ import I18n from "I18n"; export default Controller.extend({ selectedPlan: null, + isAnonymous: Ember.computed.not("currentUser"), init() { this._super(...arguments); diff --git a/assets/javascripts/discourse/templates/components/login-required.hbs b/assets/javascripts/discourse/templates/components/login-required.hbs new file mode 100644 index 0000000..6358ebc --- /dev/null +++ b/assets/javascripts/discourse/templates/components/login-required.hbs @@ -0,0 +1,2 @@ +

{{i18n 'discourse_subscriptions.subscribe.unauthenticated'}}

+{{d-button label="log_in" action="createAccount" icon="user" class="btn btn-primary"}} diff --git a/assets/javascripts/discourse/templates/components/product-list.hbs b/assets/javascripts/discourse/templates/components/product-list.hbs index 1102445..b016f08 100644 --- a/assets/javascripts/discourse/templates/components/product-list.hbs +++ b/assets/javascripts/discourse/templates/components/product-list.hbs @@ -13,7 +13,7 @@
{{#if product.subscribed}} ✓ {{i18n 'discourse_subscriptions.subscribe.purchased'}} - {{I18n 'discourse_subscriptions.subscribe.go_to_billing'}} + {{I18n 'discourse_subscriptions.subscribe.go_to_billing'}} {{else}} {{#link-to "s.show" product.id disabled=product.subscribed class="btn btn-primary"}} {{i18n 'discourse_subscriptions.subscribe.title'}} @@ -24,9 +24,4 @@
{{/each}} - {{#unless isLoggedIn}} -

- {{i18n 'discourse_subscriptions.subscribe.unauthenticated'}} -

- {{/unless}} {{/if}} diff --git a/assets/javascripts/discourse/templates/s/index.hbs b/assets/javascripts/discourse/templates/s/index.hbs index c152336..e0cd9c7 100644 --- a/assets/javascripts/discourse/templates/s/index.hbs +++ b/assets/javascripts/discourse/templates/s/index.hbs @@ -1 +1,4 @@ -{{product-list products=model}} +{{#unless isLoggedIn}} + {{login-required}} +{{/unless}} +{{product-list products=model isLoggedIn=isLoggedIn}} diff --git a/assets/javascripts/discourse/templates/s/show.hbs b/assets/javascripts/discourse/templates/s/show.hbs index 17ddaa8..c6e1b47 100644 --- a/assets/javascripts/discourse/templates/s/show.hbs +++ b/assets/javascripts/discourse/templates/s/show.hbs @@ -26,11 +26,13 @@
- {{subscribe-card cardElement=cardElement}} - {{#if loading}} {{loading-spinner}} + {{else if isAnonymous}} + {{login-required}} {{else}} + {{subscribe-card cardElement=cardElement}} + {{d-button disabled=loading action="stripePaymentHandler" diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index c75c454..4cb991c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -63,7 +63,7 @@ en: subscribe: title: Subscribe no_products: There are currently no products available. - unauthenticated: You need to create an account to subscribe. + unauthenticated: Log in or create an account to subscribe. card: title: Payment customer: diff --git a/test/javascripts/helpers/product-pretender.js.es6 b/test/javascripts/helpers/product-pretender.js.es6 index 14ccb40..b16179e 100644 --- a/test/javascripts/helpers/product-pretender.js.es6 +++ b/test/javascripts/helpers/product-pretender.js.es6 @@ -1,41 +1,41 @@ -export default function(helpers) { +export default function (helpers) { const { response } = helpers; - this.get("/s/products", () => { + this.get("/s", () => { const products = [ { id: "prod_23o8I7tU4g56", name: "Awesome Product", description: - "Subscribe to our awesome product. For only $230.10 per month, you can get access. This is a test site. No real credit card transactions." + "Subscribe to our awesome product. For only $230.10 per month, you can get access. This is a test site. No real credit card transactions.", }, { id: "prod_B23dc9I7tU4eCy", name: "Special Product", description: - "This is another subscription product. You can have more than one. From $12 per month." - } + "This is another subscription product. You can have more than one. From $12 per month.", + }, ]; return response(products); }); - this.get("/s/products/:id", () => { - const product = {}; - - return response(product); - }); - - this.get("/s/plans", () => { + this.get("/s/:id", () => { + const product = { + id: "prod_23o8I7tU4g56", + name: "Awesome Product", + description: + "Subscribe to our awesome product. For only $230.10 per month, you can get access. This is a test site. No real credit card transactions.", + }; const plans = [ { id: "plan_GHGHSHS8654G", amount: 200, currency: "usd", - interval: "month" - } + interval: "month", + }, ]; - return response(plans); + return response({ product, plans }); }); }