-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from debtcollective/feat/subscription-toggling
Feat/subscription toggling
- Loading branch information
Showing
22 changed files
with
419 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
class PlanChangesController < ApplicationController | ||
before_action :set_current_user_plan, only: %i[index create] | ||
before_action :not_authorized_user, only: %i[create] | ||
|
||
# GET /users/:user_id/plan_change | ||
def index | ||
redirect_to root_path unless current_user&.active_subscription | ||
@user = current_user | ||
@plans = Plan.all | ||
end | ||
|
||
# GET /users/:user_id/plan_change/new | ||
# GET /users/:user_id/plan_change/new.json | ||
def new | ||
@plan_change = UserPlanChange.new | ||
end | ||
|
||
# POST /users/:user_id/plan_change | ||
# POST /users/:user_id/plan_change.json | ||
def create | ||
@plan = UserPlanChange.new(user_id: plan_change_params[:user_id], old_plan_id: plan_change_params[:old_plan_id], new_plan_id: plan_change_params[:new_plan_id], status: 'pending') | ||
|
||
respond_to do |format| | ||
if @plan.save | ||
format.json { render json: @plan, status: :ok } | ||
else | ||
format.html { render :index } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def not_authorized_user | ||
head :not_authorized unless current_user | ||
end | ||
|
||
def set_current_user_plan | ||
@current_plan = current_user&.active_subscription | ||
end | ||
|
||
def plan_change_params | ||
params.require(:plan_change).permit(:user_id, :old_plan_id, :new_plan_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, { useState } from 'react' | ||
import numeral from 'numeral' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
import Button from '@material-ui/core/Button' | ||
import Paper from '@material-ui/core/Paper' | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
root: { | ||
width: '100%', | ||
marginTop: theme.spacing(3), | ||
padding: theme.spacing(4), | ||
overflowX: 'auto' | ||
}, | ||
table: { | ||
minWidth: 650 | ||
} | ||
})) | ||
|
||
const CHANGE_PLAN_ENDPOINT = id => `/users/${id}/plan_changes` | ||
|
||
function CurrentPlanView ({ user, activePlan, plans }) { | ||
const classes = useStyles() | ||
const [changedPlan, setPlanChanged] = useState(false) | ||
|
||
const changePlan = async selectedPlanId => { | ||
try { | ||
await fetch(CHANGE_PLAN_ENDPOINT(user.id), { | ||
method: 'post', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
plan_change: { | ||
user_id: user.id, | ||
old_plan_id: activePlan.id, | ||
new_plan_id: selectedPlanId | ||
} | ||
}) | ||
}) | ||
setPlanChanged(true) | ||
} catch (error) { | ||
console.error(error) // TODO: Replace with sentry | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<Paper className={classes.root}> | ||
<h3>Change your susbcription</h3> | ||
<p> | ||
The plan you're using to contribute is{' '} | ||
<strong> {activePlan.name}</strong>. | ||
</p> | ||
</Paper> | ||
<br /> | ||
<h2>Available plans</h2> | ||
<p>You can change your current plan for another one at anytime.</p> | ||
{changedPlan && ( | ||
<p className='notice--subscription'> | ||
We have changed your subscription successfully. | ||
</p> | ||
)} | ||
{plans.map(plan => { | ||
function createMarkup () { | ||
return { __html: plan.description.body } | ||
} | ||
|
||
function changeHandler () { | ||
return changePlan(plan.id) | ||
} | ||
|
||
return ( | ||
<Paper className={classes.root} key={plan.id} id={`plan-${plan.id}`}> | ||
<h4>{plan.name}</h4> | ||
<p>Price: {numeral(plan.amount).format('$0,0.00')}</p> | ||
<p dangerouslySetInnerHTML={createMarkup()} /> | ||
<Button | ||
variant='contained' | ||
color='primary' | ||
disabled={activePlan.id === plan.id || changedPlan} | ||
onClick={changeHandler} | ||
> | ||
Pick plan | ||
</Button> | ||
</Paper> | ||
) | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
export const CurrentPlan = props => <CurrentPlanView {...props} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class ChangeSubscriptionPlansJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform | ||
pending_user_plan_changes = UserPlanChange.where(status: 'pending') | ||
pending_user_plan_changes.each do |pending_user_plan_change| | ||
ToggleUserActiveSubscriptionPlanJob.perform_later(pending_user_plan_change) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class ToggleUserActiveSubscriptionPlanJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(user_plan_change) | ||
# find the user | ||
user = User.find(user_plan_change.user_id) | ||
|
||
# disable current user subscription. | ||
old_subscription = user.active_subscription | ||
ActiveRecord::Base.transaction do | ||
old_subscription.update(active: false) | ||
|
||
# creates a new subscription with the new plan details. | ||
new_subscription = Subscription.new( | ||
user_id: user_plan_change.user_id, | ||
plan_id: user_plan_change.new_plan_id, | ||
last_charge_at: old_subscription.last_charge_at, | ||
active: true | ||
) | ||
|
||
if new_subscription.save | ||
UserPlanChange.find(user_plan_change.id).update(status: 'finished') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class UserPlanChange < ApplicationRecord | ||
belongs_to :user | ||
|
||
enum status: %i[finished pending archived failed] | ||
|
||
validates :old_plan_id, :new_plan_id, :user_id, presence: true | ||
validates :user_id, uniqueness: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<section class="section-content"> | ||
<%= react_component("CurrentPlan", props: {user: @user, activePlan: @user.active_subscription&.plan, plans: @plans}) %> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddSubscriptionChange < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :user_plan_changes, id: :uuid do |t| | ||
t.string :old_plan_id | ||
t.string :new_plan_id | ||
t.string :user_id | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStatusToPlanChange < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :user_plan_changes, :status, :integer, default: 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.