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

Extract form submission code out of the checkout component #6805

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

rupertbates
Copy link
Member

@rupertbates rupertbates commented Feb 26, 2025

What are you doing in this PR?

This PR moves the logic related to submitting the generic checkout form to the server and handling the response, out of checkoutComponent.tsx and into separate files.

Why are you doing this?

checkoutComponent.tsx currently contains several unrelated concerns from presentation through to the server interaction code mentioned above. By separating them out the aim is to apply the single responsibility principle - that "A module should be responsible to one, and only one, actor".

The extracted functions are fairly different to the original version because the original used React hooks extensively, so to move the logic out of the component and to introduce a separation of concerns, I have had to refactor that dependency on React out of the code.

Testing

I have deployed this to CODE and run the E2E smoke tests against it successfully

@rupertbates rupertbates force-pushed the refactor-checkout-component branch from 2bba0da to 75f7253 Compare February 26, 2025 09:54
Copy link
Contributor

github-actions bot commented Feb 26, 2025

Size Change: +258 B (+0.01%)

Total Size: 1.94 MB

ℹ️ View Unchanged
Filename Size Change
./public/compiled-assets/javascripts/[countryGroupId]/events/router.js 92.3 kB 0 B
./public/compiled-assets/javascripts/[countryGroupId]/router.js 96.7 kB 0 B
./public/compiled-assets/javascripts/ausMomentMap.js 108 kB 0 B
./public/compiled-assets/javascripts/contributionsRedirectStyles.js 20 B 0 B
./public/compiled-assets/javascripts/digitalSubscriptionLandingPage.js 225 kB 0 B
./public/compiled-assets/javascripts/downForMaintenancePage.js 69.1 kB 0 B
./public/compiled-assets/javascripts/error404Page.js 69.1 kB 0 B
./public/compiled-assets/javascripts/error500Page.js 69 kB 0 B
./public/compiled-assets/javascripts/favicons.js 617 B 0 B
./public/compiled-assets/javascripts/paperSubscriptionCheckoutPage.js 165 kB 0 B
./public/compiled-assets/javascripts/paperSubscriptionLandingPage.js 89.5 kB 0 B
./public/compiled-assets/javascripts/payPalErrorPage.js 67.6 kB 0 B
./public/compiled-assets/javascripts/payPalErrorPageStyles.js 20 B 0 B
./public/compiled-assets/javascripts/promotionTerms.js 75.7 kB 0 B
./public/compiled-assets/javascripts/subscriptionsLandingPage.js 80.1 kB 0 B
./public/compiled-assets/javascripts/subscriptionsRedemptionPage.js 120 kB 0 B
./public/compiled-assets/javascripts/unsupportedBrowserStyles.js 20 B 0 B
./public/compiled-assets/javascripts/weeklySubscriptionCheckoutPage.js 162 kB 0 B
./public/compiled-assets/javascripts/weeklySubscriptionLandingPage.js 89.8 kB 0 B
./public/compiled-assets/webpack/114.js 12.2 kB 0 B
./public/compiled-assets/webpack/127.js 3.53 kB 0 B
./public/compiled-assets/webpack/136.js 2.17 kB 0 B
./public/compiled-assets/webpack/163.js 8.9 kB 0 B
./public/compiled-assets/webpack/187.js 20.7 kB 0 B
./public/compiled-assets/webpack/192.js 5.71 kB 0 B
./public/compiled-assets/webpack/276.js 4.39 kB 0 B
./public/compiled-assets/webpack/344.js 2 kB 0 B
./public/compiled-assets/webpack/445.js 6.96 kB 0 B
./public/compiled-assets/webpack/492.js 7.58 kB 0 B
./public/compiled-assets/webpack/503.js 37.7 kB -18 B (-0.05%)
./public/compiled-assets/webpack/706.js 107 kB 0 B
./public/compiled-assets/webpack/847.js 26 kB 0 B
./public/compiled-assets/webpack/975.js 24 kB 0 B
./public/compiled-assets/webpack/checkout.js 17.3 kB +305 B (+1.8%)
./public/compiled-assets/webpack/GuardianAdLiteLanding.js 8.17 kB -14 B (-0.17%)
./public/compiled-assets/webpack/LandingPage.js 15.4 kB 0 B
./public/compiled-assets/webpack/oneTimeCheckout.js 6.17 kB 0 B
./public/compiled-assets/webpack/ThankYou.js 44.5 kB -15 B (-0.03%)

compressed-size-action

@rupertbates rupertbates force-pushed the refactor-checkout-component branch from 53313f5 to 5475bb6 Compare February 26, 2025 10:03
@rupertbates rupertbates changed the title Refactor checkout component Refactor form submission code out of the checkout component Feb 26, 2025
@rupertbates rupertbates changed the title Refactor form submission code out of the checkout component Extract form submission code out of the checkout component Feb 26, 2025
@@ -248,7 +168,7 @@ export function CheckoutComponent({
abParticipations,
}: CheckoutComponentProps) {
/** we unset any previous orders that have been made */
unsetThankYouOrder();
// unsetThankYouOrder(); TODO: after refactor this clears the session state after the submit is finished but before we get to the thank you page which causes an error
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we need this? Does anyone know why it was added?

Copy link
Contributor

@GHaberis GHaberis Feb 27, 2025

Choose a reason for hiding this comment

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

I'm not sure why this was originally added tbh, we use the thankYouOrder item in session storage to retrieve info about the order to be rendered on the TY page such as firstName, email and paymentMethod. The only reason to unset AFAICT would be to prevent rendering incorrect info on the TY page if they order two products in the same session? However I can't see how this scenario would be possible as we call setThankYouOrder on form submission, so it should always have the latest correct details by the time a user lands on the TY page: `https://github.com/guardian/support-frontend/blob/pd/one-time-checkout-email-verification/support-frontend/assets/pages/%5BcountryGroupId%5D/components/checkoutComponent.tsx#L638

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that was the conclusion I came to as well

Copy link
Member

@tjmw tjmw left a comment

Choose a reason for hiding this comment

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

Nice, a definite improvement 👍

}
};

const createNewSupportWorkersExecution = async (
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 make sense to move these methods around creating the support workers request and polling for the status to a separate file?

}
};

const goToThankYouPage = (
Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily for now, but perhaps in future the return value of submitForm could be used to represent success or failure to the caller? The success status could even include the URL to redirect to, e.g.

{
  status: "success",
  redirectPath: "/uk/thank-you..."
}

Then we'd decouple this code from needing to know about the window.

),
);
setIsProcessingPayment(false);
setErrorMessage('Sorry, something went wrong');
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder whether we should log errors that aren't FormSubmissionError in Sentry? I don't think we'd want to do that for FormSubmissionError errors as they're known errors that could occur and are handled with meaningful messages displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants