Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use discount api response for cancellation offer payment date #1384

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/components/mma/cancel/Cancellation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const Offer: StoryObj<typeof CancellationContainer> = {
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
},
Expand All @@ -116,6 +117,7 @@ export const OfferReview: StoryObj<typeof CancellationContainer> = {
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
msw: [
Expand All @@ -136,6 +138,7 @@ export const OfferConfirmed: StoryObj<typeof CancellationContainer> = {
reactRouter: {
state: {
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
},
Expand All @@ -156,6 +159,9 @@ export const SupportplusCancelConfirm: StoryObj<typeof CancellationContainer> =
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [
{ date: '2024-07-30', amount: 14.99 },
],
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { Ribbon } from '@/client/components/shared/Ribbon';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import { number2words } from '@/shared/numberUtils';
import { getMainPlan, isPaidSubscriptionPlan } from '@/shared/productResponse';
Expand Down Expand Up @@ -175,6 +176,11 @@ export const SupporterPlusOffer = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableStrikethroughPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

return (
<>
<ProgressStepper
Expand Down Expand Up @@ -220,7 +226,8 @@ export const SupporterPlusOffer = () => {
<p css={strikethroughPriceCss}>
<s>
{mainPlan.currency}
{mainPlan.price / 100}/{mainPlan.billingPeriod}
{humanReadableStrikethroughPrice}/
{mainPlan.billingPeriod}
</s>
</p>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { useContext, useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import type { PaidSubscriptionPlan } from '@/shared/productResponse';
import { getMainPlan } from '@/shared/productResponse';
import { DownloadAppCta } from '../../../shared/DownloadAppCta';
import { Heading } from '../../../shared/Heading';
import type {
Expand Down Expand Up @@ -170,6 +173,9 @@ export const SupporterPlusOfferConfirmed = () => {
) as CancellationContextInterface;

const productDetail = cancellationContext.productDetail;
const mainPlan = getMainPlan(
productDetail.subscription,
) as PaidSubscriptionPlan;

useEffect(() => {
pageTitleContext.setPageTitle('Confirmation');
Expand All @@ -181,6 +187,11 @@ export const SupporterPlusOfferConfirmed = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableNextNonDiscountedPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

return (
<>
<Heading
Expand Down Expand Up @@ -210,7 +221,10 @@ export const SupporterPlusOfferConfirmed = () => {
</li>
<li>
You will not be billed until{' '}
{nextNonDiscountedPaymentDate}
{nextNonDiscountedPaymentDate} after which you will pay{' '}
{mainPlan.currency}
{humanReadableNextNonDiscountedPrice}/
{mainPlan.billingPeriod}
</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useContext, useState } from 'react';
import { Link, useLocation, useNavigate } from 'react-router-dom';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { fetchWithDefaultParameters } from '@/client/utilities/fetch';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import { number2words } from '@/shared/numberUtils';
Expand Down Expand Up @@ -127,6 +128,11 @@ export const SupporterPlusOfferReview = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableStrikethroughPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

const [performingDiscountStatus, setPerformingDiscountStatus] =
useState<OfferApiCallStatus>('NOT_READY');

Expand Down Expand Up @@ -194,7 +200,8 @@ export const SupporterPlusOfferReview = () => {
<p css={strikethroughPriceCss}>
<s>
{mainPlan.currency}
{mainPlan.price / 100}/{mainPlan.billingPeriod}
{humanReadableStrikethroughPrice}/
{mainPlan.billingPeriod}
</s>
</p>
)}
Expand Down
20 changes: 20 additions & 0 deletions client/utilities/discountPreview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
interface NonDiscountedPayments {
date: string;
amount: number;
}

export type DiscountPreviewResponse = {
discountedPrice: number;
upToPeriods: number;
upToPeriodsType: string;
firstDiscountedPaymentDate: string;
nextNonDiscountedPaymentDate: string;
nonDiscountedPayments: NonDiscountedPayments[];
};

export const getMaxNonDiscountedPrice = (
nonDiscountedPayments: NonDiscountedPayments[],
asHumanReadable?: boolean,
) => {
const allNonDiscountedAmounts = nonDiscountedPayments.map((p) => p.amount);
const maxNonDiscountedPrice = Math.max(...allNonDiscountedAmounts);
if (!asHumanReadable) {
return maxNonDiscountedPrice;
}
return Number.isInteger(maxNonDiscountedPrice)
? maxNonDiscountedPrice
: maxNonDiscountedPrice.toFixed(2);
};
3 changes: 3 additions & 0 deletions cypress/tests/mocked/parallel-2/cancelSupporterPlus.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe('Cancel Supporter Plus', () => {
upToPeriodsType: 'Months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.5 }],
};
it('user accepts offer instead of cancelling', () => {
cy.intercept('GET', '/api/me/mma', {
Expand Down Expand Up @@ -184,6 +185,8 @@ describe('Cancel Supporter Plus', () => {
name: 'Continue to cancellation',
}).click();

cy.findByText('£14.50/month');

cy.findByRole('button', { name: 'Redeem your offer' }).click();

cy.findByRole('button', {
Expand Down
Loading