Skip to content

Commit

Permalink
feat: show tier modal on account page after logging in
Browse files Browse the repository at this point in the history
This shows the user the plan they wanted from the home page after logging in.
  • Loading branch information
jsdevel committed Oct 18, 2022
1 parent 188eb6e commit 13ff0a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Expand Up @@ -30,11 +30,16 @@ export async function putUserPayment(pm_id, plan_id) {
return res.json();
}

/**
*
* @typedef {import('../../components/contexts/plansContext').Plan} Plan
*/

/**
* @param {object} obj
* @param {any} obj.isOpen
* @param {any} obj.onClose
* @param {any} obj.planSelection
* @param {undefined|Plan} obj.planSelection
* @param {any} obj.planList
* @param {any} obj.stripePromise
* @param {any} obj.setCurrentPlan
Expand All @@ -58,7 +63,7 @@ const AccountPlansModal = ({
stripePromise,
}) => {
const [isCreatingSub, setIsCreatingSub] = useState(false);
const currentPlan = planList.find(p => p.id === planSelection.id);
const currentPlan = planList.find(p => p.id === planSelection?.id);
return (
<div className="account-plans-modal">
<Modal
Expand Down
23 changes: 22 additions & 1 deletion packages/website/pages/account/payment.js
Expand Up @@ -2,6 +2,7 @@
* @fileoverview Account Payment Settings
*/

import { parse as queryParse } from 'querystring';
import { useState, useEffect, useMemo } from 'react';
import { Elements, ElementsConsumer } from '@stripe/react-stripe-js';
import { loadStripe } from '@stripe/stripe-js';
Expand Down Expand Up @@ -45,19 +46,36 @@ import GeneralPageData from '../../content/pages/general.json';
* @property {PaymentMethodCard} card
*/

function removePlanQueryParam() {
history.pushState({}, '', window.location.href.replace(/plan=[^&]+&?/, ''));
}

const PaymentSettingsPage = props => {
const planQueryParam = queryParse(window.location.search.substring(1))?.plan;
const [isPaymentPlanModalOpen, setIsPaymentPlanModalOpen] = useState(false);
const stripePromise = loadStripe(props.stripePublishableKey);
const [needsFetchPaymentSettings, setNeedsFetchPaymentSettings] = useState(true);
const [, setIsFetchingPaymentSettings] = useState(false);
const [paymentSettings, setPaymentSettings] = useState(/** @type {undefined|PaymentSettings} */ (undefined));
const [planSelection, setPlanSelection] = useState('');
const [planSelection, setPlanSelection] = useState(/** @type {undefined|Plan} */ (undefined));
const [editingPaymentMethod, setEditingPaymentMethod] = useState(false);
// subcomponents that save a new plan can set this, which will trigger a re-fetch but the
// ui can optimistically show the new value while the refetch happens.
const [optimisticCurrentPlan, setOptimisticCurrentPlan] = useState(/** @type {Plan|undefined} */ (undefined));
const [hasAcceptedTerms, setHasAcceptedTerms] = useState(false);

useEffect(() => {
if (planQueryParam) {
const desiredPlanFromQueryParam = plans.find(plan => plan.id === planQueryParam);
if (desiredPlanFromQueryParam) {
setPlanSelection(desiredPlanFromQueryParam);
setIsPaymentPlanModalOpen(true);
} else {
removePlanQueryParam();
}
}
}, [planQueryParam]);

// fetch payment settings whenever needed
useEffect(() => {
async function loadPaymentSettings() {
Expand Down Expand Up @@ -187,6 +205,9 @@ const PaymentSettingsPage = props => {
onClose={() => {
setIsPaymentPlanModalOpen(false);
setHasAcceptedTerms(false);
if (planQueryParam) {
removePlanQueryParam();
}
}}
planList={planList}
planSelection={planSelection}
Expand Down

0 comments on commit 13ff0a2

Please sign in to comment.