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

Fix ecommerce-app-base bug in drag-n-drop refetch logic #7944

Conversation

ethan-ozelius-contentful
Copy link
Contributor

@ethan-ozelius-contentful ethan-ozelius-contentful commented Jun 4, 2024

Purpose

Fix a bug that was introduced in this PR that was causing an infinite loop rerender to occur.

Root cause

  1. On initial render, usePreviousSkus() hook is invoked to track skus from previous mount,
  2. This useEffect invokes fetchProductPreviews() on mount, as expected.
  3. fetchProductPreviews() retrieves product previews via network request
  4. fetchProductPreviews() updates the productPreviews state, causing a rerender, as expected.
  5. on rerender, usePreviousSkus() runs and retrieves the list of skus from previous mount, as well as updates the current ref to the most current list of skus (list of skus received via props).
  6. since the useEffect (see number 1) lists previousSkus as a dependency, the useEffect runs again. Setting off an infinite loop.

Screen capture of working solution

shopify-fixed

Approach

Testing steps

Breaking Changes

Dependencies and/or References

Deployment

Copy link

netlify bot commented Jun 4, 2024

Deploy Preview for ecommerce-app-base-components ready!

Name Link
🔨 Latest commit c64a7d9
🔍 Latest deploy log https://app.netlify.com/sites/ecommerce-app-base-components/deploys/6660a0f7d244960008567af6
😎 Deploy Preview https://deploy-preview-7944--ecommerce-app-base-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ethan-ozelius-contentful ethan-ozelius-contentful changed the title fix(ecommerce-app-base): drag-n-drop bug in refetch logic that was ca… Fix ecommerce-app-base bug in drag-n-drop refetch logic Jun 4, 2024
@@ -5,53 +5,7 @@ import { ConfigAppSDK } from '@contentful/app-sdk';

import AppConfig from './AppConfig';
import { definitions } from './parameters.spec';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied/pasted this code out of AppConfig into __mocks__ dir so that other test files can re-use this functionality.

@@ -57,28 +57,31 @@ export const SortableComponent: FC<Props> = ({
const [productPreviews, setProductPreviews] = useState<Product[]>([]);
const previousSkus = usePreviousSkus(skus);

const getProductPreviews = useCallback(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed shouldRefetch arg from this callback and reversed the dependency, so that consumers of this callback are responsible for determining shouldRefetch. See line 82.

This has the added benefit of removing the productPreviews from the useCallback dependencies, which was causing this callback to be re-defined every time productPreviews state updated.

@ethan-ozelius-contentful ethan-ozelius-contentful force-pushed the eozelius/ecommerce-app-base/v4/fix-drag-n-drop branch 2 times, most recently from b1b7308 to dbbea6f Compare June 5, 2024 16:05
@ethan-ozelius-contentful ethan-ozelius-contentful marked this pull request as ready for review June 5, 2024 16:23
@ethan-ozelius-contentful ethan-ozelius-contentful requested a review from a team as a code owner June 5, 2024 16:23
@ethan-ozelius-contentful ethan-ozelius-contentful force-pushed the eozelius/ecommerce-app-base/v4/fix-drag-n-drop branch from dbbea6f to c64a7d9 Compare June 5, 2024 17:31
Copy link
Contributor

@whitelisab whitelisab left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you for the helpful comments and tests in this PR 🚀

@ethan-ozelius-contentful ethan-ozelius-contentful merged commit e5dcf5a into master Jun 5, 2024
12 checks passed
@ethan-ozelius-contentful ethan-ozelius-contentful deleted the eozelius/ecommerce-app-base/v4/fix-drag-n-drop branch June 5, 2024 21:30
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