-
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 #107 from debtcollective/feat/cancel-active-subscr…
…iption Feat/cancel active subscription
- Loading branch information
Showing
15 changed files
with
167 additions
and
24 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
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class SubscriptionsController < ApplicationController | ||
before_action :set_user, only: %i[destroy] | ||
|
||
# DELETE /user/:user_id/subscription | ||
# DELETE /user/:user_id/subscription.json | ||
def destroy | ||
head :not_authorized unless current_user == @user | ||
|
||
subscription = @user.active_subscription | ||
|
||
subscription.active = false | ||
if subscription.save | ||
head :ok | ||
else | ||
head :bad_request | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
app/javascript/bundles/User/components/SubscriptionCancel.jsx
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,79 @@ | ||
import React, { useState } from 'react' | ||
import Button from '@material-ui/core/Button' | ||
import Dialog from '@material-ui/core/Dialog' | ||
import DialogActions from '@material-ui/core/DialogActions' | ||
import DialogContent from '@material-ui/core/DialogContent' | ||
import DialogContentText from '@material-ui/core/DialogContentText' | ||
import DialogTitle from '@material-ui/core/DialogTitle' | ||
|
||
const SUBSCRIPTION_CANCEL_URL = userID => `/users/${userID}/subscription` | ||
|
||
function SubscriptionCancelView ({ user, subscription }) { | ||
const [open, setOpen] = useState(false) | ||
const [active, toggleActive] = useState(subscription.active) | ||
|
||
const handleClickOpen = () => { | ||
setOpen(true) | ||
} | ||
|
||
const handleClose = () => { | ||
setOpen(false) | ||
} | ||
|
||
const handleSubscriptionCancellation = async () => { | ||
const isSubscriptionCancelled = await fetch( | ||
SUBSCRIPTION_CANCEL_URL(user.id), | ||
{ | ||
method: 'delete' | ||
} | ||
) | ||
|
||
toggleActive(!subscription.active) | ||
|
||
handleClose() | ||
} | ||
|
||
if (!subscription.active) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div> | ||
<Button color='secondary' onClick={handleClickOpen}> | ||
Cancel Subscription | ||
</Button> | ||
{!active && 'No active subscription'} | ||
<Dialog | ||
open={open} | ||
onClose={handleClose} | ||
id='cancel-subscription-dialog' | ||
aria-labelledby='cancel-subscription-title' | ||
aria-describedby='cancel-subscription-description' | ||
> | ||
<DialogTitle id='cancel-subscription-title'> | ||
Do you want to terminate your current subscription? | ||
</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id='cancel-subscription-description'> | ||
Terminating your subscription will stop your current plan and the | ||
benefits you receive from it. | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={handleClose} color='primary'> | ||
Close | ||
</Button> | ||
<Button | ||
onClick={handleSubscriptionCancellation} | ||
color='primary' | ||
autoFocus | ||
> | ||
Cancel Subscription | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</div> | ||
) | ||
} | ||
|
||
export const SubscriptionCancel = props => <SubscriptionCancelView {...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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<section class="section-content"> | ||
<p id="notice"><%= notice %></p> | ||
|
||
<%= react_component("UserShow", props: {user: @user, streak: @user.current_streak, subscription: @user.subscription}) %> | ||
<%= react_component("DonationsHistory", props: {activePlan: @user.subscription&.plan, donations: @user.donations}) %> | ||
<%= react_component("UserShow", props: {user: @user, streak: @user.current_streak, subscription: @user.active_subscription}) %> | ||
<%= react_component("DonationsHistory", props: {activePlan: @user.active_subscription&.plan, donations: @user.donations}) %> | ||
<%= react_component("SubscriptionCancel", props: {user: @user, subscription: @user.active_subscription}) %> | ||
</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