-
Notifications
You must be signed in to change notification settings - Fork 12
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
[oFix] Refactoring SellerWrapper and Adding Custom Hooks #139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,3 @@ | ||
import React, { useCallback, useEffect, useRef, useState } from 'react' | ||
import { useProduct, useProductDispatch } from 'vtex.product-context' | ||
import { useCssHandles } from 'vtex.css-handles' | ||
import type { Item } from 'vtex.product-context/react/ProductTypes' | ||
|
||
import useSeller from './hooks/useSeller' | ||
|
||
const CSS_HANDLES = ['sellerWrapper', 'loadingSeller'] | ||
const SELECT_ITEM_EVENT = 'SET_SELECTED_ITEM' | ||
|
||
type SellerWrapperProps = { | ||
children: React.ReactNode | ||
} | ||
|
||
const SellerWrapper = ({ children }: SellerWrapperProps) => { | ||
const { seller } = useSeller() | ||
const dispatch = useProductDispatch() | ||
const { selectedItem, product } = useProduct() ?? {} | ||
const latestItem = useRef((null as unknown) as Item) | ||
const handles = useCssHandles(CSS_HANDLES) | ||
const [loadingSeller, setLoadingSeller] = useState(true) | ||
|
||
const addSellerDefaultToItem = useCallback( | ||
itemSeller => ({ | ||
...itemSeller, | ||
sellerDefault: seller?.includes(itemSeller.sellerId), | ||
}), | ||
[seller] | ||
) | ||
|
||
useEffect(() => { | ||
const shouldDispatchSelectItem = | ||
!!seller && !!product && !!selectedItem && !!dispatch | ||
|
||
if (!shouldDispatchSelectItem) return | ||
const newCurrentSelectedItem = | ||
product?.items?.find(item => | ||
item.sellers?.find( | ||
itemSeller => | ||
seller?.includes(itemSeller.sellerId) && | ||
itemSeller.commertialOffer.AvailableQuantity > 0 | ||
) | ||
) ?? ({} as Item) | ||
|
||
if (!newCurrentSelectedItem) { | ||
return | ||
} | ||
|
||
const { sellers } = newCurrentSelectedItem | ||
const selectedItemWithSeller = { | ||
...newCurrentSelectedItem, | ||
sellers: sellers.map(addSellerDefaultToItem), | ||
} | ||
|
||
if ( | ||
JSON.stringify(latestItem.current) === | ||
JSON.stringify(selectedItemWithSeller) | ||
) { | ||
return | ||
} | ||
|
||
dispatch?.({ | ||
type: SELECT_ITEM_EVENT, | ||
args: { | ||
item: selectedItemWithSeller, | ||
}, | ||
}) | ||
|
||
setLoadingSeller(false) | ||
|
||
latestItem.current = selectedItemWithSeller | ||
}, [ | ||
seller, | ||
product, | ||
selectedItem, | ||
dispatch, | ||
addSellerDefaultToItem, | ||
latestItem, | ||
]) | ||
|
||
return ( | ||
<div | ||
className={`${handles.sellerWrapper} ${ | ||
loadingSeller ? handles.loadingSeller : '' | ||
}`} | ||
> | ||
{children} | ||
</div> | ||
) | ||
} | ||
import SellerWrapper from './components/SellerWrapper/SellerWrapper' | ||
|
||
export default SellerWrapper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useEffect } from 'react' | ||
import { useCssHandles } from 'vtex.css-handles' | ||
import { useProduct } from 'vtex.product-context' | ||
|
||
import { useSelectSeller } from '../../hooks/useSelectSeller' | ||
|
||
type SellerWrapperProps = { | ||
children: React.ReactNode | ||
} | ||
|
||
const CSS_HANDLES = ['sellerWrapper', 'sellerWrapperNoSeller'] | ||
|
||
const SellerWrapper = ({ children }: SellerWrapperProps) => { | ||
const handles = useCssHandles(CSS_HANDLES) | ||
const { currentSelectedItem, selectSeller } = useSelectSeller() | ||
|
||
const { selectedItem } = useProduct() ?? {} | ||
|
||
useEffect(() => { | ||
selectSeller({ selectedItem }) | ||
}, [selectedItem, selectSeller]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird, this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, I just added the dependency. However, in this logic, we need to force the seller whatever takes. So, that's the reason the selectedItem is assigned there. |
||
|
||
const className = currentSelectedItem | ||
? `${handles.sellerWrapper}` | ||
: `${handles.sellerWrapper} ${handles.sellerWrapperNoSeller}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is different from the original code that was refactored, it used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As part of refactoring, I noticed the loading seller handle has never been displayed, once the flow is synchronous and there is no "loading" time. It prevents the layout shifting. |
||
|
||
return <div className={className}>{children}</div> | ||
} | ||
|
||
export default SellerWrapper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useProduct, useProductDispatch } from 'vtex.product-context' | ||
import { useCallback, useMemo, useRef, useState } from 'react' | ||
import type { Item } from 'vtex.product-context/react/ProductTypes' | ||
|
||
import useSeller from './useSeller' | ||
|
||
const SELECT_ITEM_EVENT = 'SET_SELECTED_ITEM' | ||
|
||
export const useSelectSeller = () => { | ||
const { seller } = useSeller() | ||
const { product } = useProduct() ?? {} | ||
const latestItem = useRef((null as unknown) as Item) | ||
const [loading, setLoading] = useState(true) | ||
const productDispatch = useProductDispatch() | ||
|
||
const addSellerDefaultToItem = useCallback( | ||
itemSeller => ({ | ||
...itemSeller, | ||
sellerDefault: seller?.includes(itemSeller.sellerId), | ||
}), | ||
[seller] | ||
) | ||
|
||
const currentSelectedItem = useMemo( | ||
() => | ||
product?.items?.find(item => | ||
item.sellers?.find( | ||
itemSeller => | ||
seller?.includes(itemSeller.sellerId) && | ||
itemSeller.commertialOffer.AvailableQuantity > 0 | ||
) | ||
) ?? null, | ||
|
||
[seller, product] | ||
) | ||
|
||
const selectSeller = ({ | ||
selectedItem, | ||
}: { | ||
selectedItem: Item | null | undefined | ||
}) => { | ||
if (!currentSelectedItem || !selectedItem) { | ||
return | ||
} | ||
|
||
// just to keep the dependency | ||
setLoading(true) | ||
const { sellers } = (currentSelectedItem as unknown) as Item | ||
const selectedItemWithSeller = { | ||
...((currentSelectedItem as unknown) as Item), | ||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
sellers: sellers.map(addSellerDefaultToItem), | ||
} | ||
|
||
if ( | ||
JSON.stringify(latestItem.current) === | ||
JSON.stringify(selectedItemWithSeller) | ||
) { | ||
return | ||
} | ||
|
||
productDispatch?.({ | ||
type: SELECT_ITEM_EVENT, | ||
args: { | ||
item: selectedItemWithSeller, | ||
}, | ||
}) | ||
setLoading(false) | ||
latestItem.current = selectedItemWithSeller | ||
} | ||
|
||
return { loading, seller, currentSelectedItem, selectSeller } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that this PR was just a refactoring, does it also fix a bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I found a bug and leverage the PR to refactor the code and the logic/algorithms