Skip to content

@W-18767088 refactor add to cart#2664

Merged
sf-kyle-wright merged 6 commits intostandard-product-supportfrom
t/cc-shark/W-18767088/refactor-add-to-cart
Jun 27, 2025
Merged

@W-18767088 refactor add to cart#2664
sf-kyle-wright merged 6 commits intostandard-product-supportfrom
t/cc-shark/W-18767088/refactor-add-to-cart

Conversation

@sf-kyle-wright
Copy link
Contributor

Description

Extracting existing add to cart functions from pdp into re-usable util file

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • (change1)

How to Test-Drive This PR

  1. Execute existing tests to verify that nothing is broken
  2. Verify Add to Cart on PDP still works as expected

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@sf-kyle-wright sf-kyle-wright requested a review from a team as a code owner June 26, 2025 18:03
@cc-prodsec
Copy link
Collaborator

cc-prodsec commented Jun 26, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@sf-kyle-wright sf-kyle-wright requested review from a team, jeremy-jung1 and sf-emmyzhang June 26, 2025 18:07
Copy link
Contributor

@sf-emmyzhang sf-emmyzhang left a comment

Choose a reason for hiding this comment

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

Assuming we want to move the unit tests over too?

@sf-kyle-wright
Copy link
Contributor Author

Assuming we want to move the unit tests over too?

Ya I'll do that in a few. Wanted to get feedback on the approach

*/

/**
* Handles adding products to cart and sending data to Einstein.
Copy link
Contributor

Choose a reason for hiding this comment

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

@sf-kyle-wright The WI description mentioned one reason to move addToCart out is it can be used from multiple clients like PDP, wishlist etc.
Then utils/cart-utils does not seem the right place to me.
Should this be a hook or something like that instead ?
Hooks can contain logic for data fetching, state management, UI behavior etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya so that's a great thought. There are some tradeoffs here but it looks like at this point it might not make sense to move them into a hook. I asked cursor, TLDR:

Summary:
It doesn't strictly make sense to turn these into a hook unless you want to manage state or context, or provide a more React-friendly API. If you do, a hook can be a good abstraction, but the current logic itself doesn't require it.

cursor_should_i_convert_util_functions.md

}

/**************** Product Set/Bundles Handlers ****************/
const handleChildProductValidation = useCallback(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to move this as well ? If Yes, this can go in some util file.
Others can go in some AddToCart hook/or something, as they are all Adding diff products to cart.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

handleChildProductValidation - Moved it back to product-detail since it didn't make sense to extract this one to the util

@sf-kyle-wright
Copy link
Contributor Author

There are currently no tests that directly test handleAddToCart, handleProductBundleAddToCart, or handleProductSetAddToCart. I will create dedicated tests for each of these in a follow up PR

…factor-add-to-cart

Signed-off-by: Kyle Wright <30636085+sf-kyle-wright@users.noreply.github.com>
@patricksullivansf
Copy link
Contributor

Fair warning, this refactor isn't compatible with BOPIS, add to cart needs much more data passed in. see https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2646/files#r2170144721

*/

/**
* Handles adding products to cart and sending data to Einstein.
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is only working on cart? How about we keep it in cart page? Unless we have plan for these funcs to be used by other components that is not cart

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a larger plan to re-use these functions across wishlist as well

Copy link
Contributor

Choose a reason for hiding this comment

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

if that is the case, let's name the file to be a bit more generic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexvuong What do you have in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

add-to-cart-utils?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like that name @sf-emmyzhang

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks. renamed to add-to-cart-utils.js

@sf-kyle-wright sf-kyle-wright merged commit eea4a61 into standard-product-support Jun 27, 2025
35 checks passed
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.

8 participants