Skip to content

Commit efe3566

Browse files
committed
[dashboard] Implement 'Cancel Downgrade' in Plans page
1 parent 3fcc5a9 commit efe3566

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

Diff for: components/dashboard/src/settings/Plans.tsx

+40-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export default function () {
185185

186186
const [ confirmUpgradeToPlan, setConfirmUpgradeToPlan ] = useState<Plan>();
187187
const [ confirmDowngradeToPlan, setConfirmDowngradeToPlan ] = useState<Plan>();
188+
const [ isConfirmCancelDowngrade, setIsConfirmCancelDowngrade ] = useState<boolean>(false);
188189
const confirmUpgrade = (to: Plan) => {
189190
if (pendingUpgradePlan || pendingDowngradePlan) {
190191
// Don't upgrade if we're still waiting for a Chargebee callback
@@ -267,6 +268,29 @@ export default function () {
267268
setConfirmDowngradeToPlan(undefined);
268269
}
269270
}
271+
const confirmCancelDowngrade = () => {
272+
if (!scheduledDowngradePlanId) {
273+
// No scheduled downgrade to be cancelled?
274+
return;
275+
}
276+
if ((paidSubscription?.paymentReference || '').startsWith("github:")) {
277+
return;
278+
}
279+
setIsConfirmCancelDowngrade(true);
280+
}
281+
const doCancelDowngrade = async (event: React.MouseEvent) => {
282+
if (!isConfirmCancelDowngrade || !paidSubscription) {
283+
return;
284+
}
285+
try {
286+
await server.subscriptionCancelDowngrade(paidSubscription.uid);
287+
// TODO: Update state somehow?
288+
} catch (error) {
289+
console.error('Cancel Downgrade Error', error);
290+
} finally {
291+
setIsConfirmCancelDowngrade(false);
292+
}
293+
}
270294

271295
const planCards = [];
272296

@@ -282,7 +306,7 @@ export default function () {
282306
const targetPlan = freePlan;
283307
let bottomLabel;
284308
if (scheduledDowngradePlanId === targetPlan.chargebeeId) {
285-
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)">Cancel</a></p>;
309+
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)" onClick={() => confirmCancelDowngrade()}>Cancel</a></p>;
286310
} else if (pendingDowngradePlan?.chargebeeId === targetPlan.chargebeeId) {
287311
bottomLabel = <p className="text-green-600 animate-pulse">Downgrade scheduled</p>;
288312
}
@@ -305,7 +329,7 @@ export default function () {
305329
const targetPlan = applyCoupons(personalPlan, availableCoupons);
306330
let bottomLabel;
307331
if (scheduledDowngradePlanId === targetPlan.chargebeeId) {
308-
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)">Cancel</a></p>;
332+
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)" onClick={() => confirmCancelDowngrade()}>Cancel</a></p>;
309333
} else if (pendingDowngradePlan?.chargebeeId === targetPlan.chargebeeId) {
310334
bottomLabel = <p className="text-green-600">Downgrade scheduled</p>;
311335
}
@@ -331,7 +355,7 @@ export default function () {
331355
const targetPlan = applyCoupons(professionalPlan, availableCoupons);
332356
let bottomLabel;
333357
if (scheduledDowngradePlanId === targetPlan.chargebeeId) {
334-
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)">Cancel</a></p>;
358+
bottomLabel = <p className="text-green-600">Downgrade scheduled<br/><a className="text-blue-light leading-6" href="javascript:void(0)" onClick={() => confirmCancelDowngrade()}>Cancel</a></p>;
335359
} else if (pendingDowngradePlan?.chargebeeId === targetPlan.chargebeeId) {
336360
bottomLabel = <p className="text-green-600">Downgrade scheduled</p>;
337361
}
@@ -424,6 +448,19 @@ export default function () {
424448
<button className="bg-red-600 border-red-800" onClick={doDowngrade}>Downgrade Plan</button>
425449
</div>
426450
</Modal>}
451+
{isConfirmCancelDowngrade && <Modal visible={true} onClose={() => setIsConfirmCancelDowngrade(false)}>
452+
<h3>Cancel downgrade and stay with {currentPlan.name}</h3>
453+
<div className="border-t border-b border-gray-200 mt-4 -mx-6 px-6 py-2">
454+
<p className="mt-1 mb-4 text-base">You are about to cancel the scheduled downgrade and stay with {currentPlan.name}.</p>
455+
<div className="flex rounded-md bg-gray-200 p-4 mb-4">
456+
<img className="w-4 h-4 m-1 ml-2 mr-4" src={info} />
457+
<span>You can continue using it right away.</span>
458+
</div>
459+
</div>
460+
<div className="flex justify-end mt-6">
461+
<button className="bg-red-600 border-red-800" onClick={doCancelDowngrade}>Cancel Downgrade</button>
462+
</div>
463+
</Modal>}
427464
{!!teamClaimModal && (<Modal visible={true} onClose={() => setTeamClaimModal(undefined)}>
428465
<h3 className="pb-2">Team Invitation</h3>
429466
<div className="border-t border-b border-gray-200 mt-2 -mx-6 px-6 py-4">

0 commit comments

Comments
 (0)