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 1 commit
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 @@ -175,6 +175,14 @@ export const SupporterPlusOffer = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const strikethroughPrice = routerState.nonDiscountedPayments.reduce(
(prev, current) =>
prev && prev.amount > current.amount ? prev : current,
).amount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I'm not a fan of reduce (I am, and think I probably overuse it) but what about this as an alternative? Maybe it's slightly clearer what the intention is?

Suggested change
const strikethroughPrice = routerState.nonDiscountedPayments.reduce(
(prev, current) =>
prev && prev.amount > current.amount ? prev : current,
).amount;
const strikethroughPrice = Math.max(...routerState.nonDiscountedPayments.map(p => p.amount));

Or splitting it into two:

    const allAmounts = routerState.nonDiscountedPayments.map(p => p.amount);
    const strikeThroughPrice = Math.max(...allAmounts);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I like this 👍

const humanReadableStrikethroughPrice = Number.isInteger(strikethroughPrice)
? strikethroughPrice
: strikethroughPrice.toFixed(2);

return (
<>
<ProgressStepper
Expand Down Expand Up @@ -220,7 +228,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 @@ -14,6 +14,8 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } 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 +172,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 +186,16 @@ export const SupporterPlusOfferConfirmed = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const nextNonDiscountedPrice = routerState.nonDiscountedPayments.reduce(
(prev, current) =>
prev && prev.amount > current.amount ? prev : current,
).amount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth extracting this to a method, since it's the same logic repeated in SupporterPlusOffer.tsx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, good shout 👍

const humanReadableNextNonDiscountedPrice = Number.isInteger(
nextNonDiscountedPrice,
)
? nextNonDiscountedPrice
: nextNonDiscountedPrice.toFixed(2);

return (
<>
<Heading
Expand Down Expand Up @@ -210,7 +225,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 @@ -127,6 +127,14 @@ export const SupporterPlusOfferReview = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const strikethroughPrice = routerState.nonDiscountedPayments.reduce(
(prev, current) =>
prev && prev.amount > current.amount ? prev : current,
).amount;
const humanReadableStrikethroughPrice = Number.isInteger(strikethroughPrice)
? strikethroughPrice
: strikethroughPrice.toFixed(2);

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

Expand Down Expand Up @@ -194,7 +202,8 @@ export const SupporterPlusOfferReview = () => {
<p css={strikethroughPriceCss}>
<s>
{mainPlan.currency}
{mainPlan.price / 100}/{mainPlan.billingPeriod}
{humanReadableStrikethroughPrice}/
{mainPlan.billingPeriod}
</s>
</p>
)}
Expand Down
1 change: 1 addition & 0 deletions client/utilities/discountPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type DiscountPreviewResponse = {
upToPeriodsType: string;
firstDiscountedPaymentDate: string;
nextNonDiscountedPaymentDate: string;
nonDiscountedPayments: Array<{ date: string; amount: number }>;
};
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