diff --git a/ghost/portal/src/App.js b/ghost/portal/src/App.js index 07cafe5e00..ea184cab29 100644 --- a/ghost/portal/src/App.js +++ b/ghost/portal/src/App.js @@ -10,7 +10,7 @@ import * as Fixtures from './utils/fixtures'; import ActionHandler from './actions'; import './App.css'; import NotificationParser from './utils/notifications'; -import {createPopupNotification, getAvailablePrices, getCurrencySymbol, getFirstpromoterId, getProductFromId, getQueryPrice, getSiteDomain, isComplimentaryMember, isInviteOnlySite, isSentryEventAllowed, removePortalLinkFromUrl} from './utils/helpers'; +import {createPopupNotification, getAvailablePrices, getCurrencySymbol, getFirstpromoterId, getPriceIdFromPageQuery, getQueryPrice, getSiteDomain, isComplimentaryMember, isInviteOnlySite, isSentryEventAllowed, removePortalLinkFromUrl} from './utils/helpers'; const handleDataAttributes = require('./data-attributes'); const React = require('react'); @@ -334,7 +334,7 @@ export default class App extends React.Component { /** Fetch state from Portal Links */ fetchLinkData() { const productMonthlyPriceQueryRegex = /^(?:(\w+?))?\/monthly$/; - const productYearlyPriceQueryRegex = /^(?:(\w+?))?\/monthly$/; + const productYearlyPriceQueryRegex = /^(?:(\w+?))?\/yearly$/; const offersRegex = /^offers\/(\w+?)\/?$/; const [path] = window.location.hash.substr(1).split('?'); const linkRegex = /^\/portal\/?(?:\/(\w+(?:\/\w+)*))?\/?$/; @@ -530,22 +530,15 @@ export default class App extends React.Component { /** Handle direct signup link for a price */ handleSignupQuery({site, pageQuery}) { - const productMonthlyPriceQueryRegex = /^(?:(\w+?))?\/monthly$/; - const productYearlyPriceQueryRegex = /^(?:(\w+?))?\/monthly$/; const offerQueryRegex = /^offers\/(\w+?)\/?$/; let priceId = pageQuery; if (offerQueryRegex.test(pageQuery || '')) { const [, offerId] = pageQuery.match(offerQueryRegex); this.handleOfferQuery({site, offerId}); return; - } else if (productMonthlyPriceQueryRegex.test(pageQuery || '')) { - const [, productId] = pageQuery.match(productMonthlyPriceQueryRegex); - const product = getProductFromId({site, productId}); - priceId = product?.monthlyPrice?.id; - } else if (productYearlyPriceQueryRegex.test(pageQuery || '')) { - const [, productId] = pageQuery.match(productYearlyPriceQueryRegex); - const product = getProductFromId({site, productId}); - priceId = product?.yearlyPrice?.id; + } + if (getPriceIdFromPageQuery({site, pageQuery})) { + priceId = getPriceIdFromPageQuery({site, pageQuery}); } const queryPrice = getQueryPrice({site: site, priceId}); if (!this.state.member diff --git a/ghost/portal/src/utils/helpers.js b/ghost/portal/src/utils/helpers.js index c32cca032c..e5f47b6f5d 100644 --- a/ghost/portal/src/utils/helpers.js +++ b/ghost/portal/src/utils/helpers.js @@ -492,3 +492,19 @@ export const createPopupNotification = ({type, status, autoHide, duration, close count }; }; + +export function getPriceIdFromPageQuery({site, pageQuery}) { + const productMonthlyPriceQueryRegex = /^(?:(\w+?))?\/monthly$/; + const productYearlyPriceQueryRegex = /^(?:(\w+?))?\/yearly$/; + + if (productMonthlyPriceQueryRegex.test(pageQuery || '')) { + const [, productId] = pageQuery.match(productMonthlyPriceQueryRegex); + const product = getProductFromId({site, productId}); + return product?.monthlyPrice?.id; + } else if (productYearlyPriceQueryRegex.test(pageQuery || '')) { + const [, productId] = pageQuery.match(productYearlyPriceQueryRegex); + const product = getProductFromId({site, productId}); + return product?.yearlyPrice?.id; + } + return null; +} diff --git a/ghost/portal/src/utils/helpers.test.js b/ghost/portal/src/utils/helpers.test.js new file mode 100644 index 0000000000..525dabe343 --- /dev/null +++ b/ghost/portal/src/utils/helpers.test.js @@ -0,0 +1,10 @@ +import {getPriceIdFromPageQuery} from './helpers'; +import {site} from './fixtures'; + +describe('Helpers - ', () => { + test('can correctly fetch price id from page query ', () => { + const mockPriceIdFn = jest.fn(getPriceIdFromPageQuery); + const value = mockPriceIdFn({site, pageQuery: 'product_1/yearly'}); + expect(value).toBe('6086eff0823dd7345afc8083'); + }); +});