Skip to content

Commit 437ca30

Browse files
committed
feat: add shipping-option-components modals into radio group component
1 parent 95189e5 commit 437ca30

File tree

3 files changed

+42
-63
lines changed

3 files changed

+42
-63
lines changed

react/SearchResultFlexible.js

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useEffect, useRef, useState } from 'react'
1+
import React, { useMemo, useEffect, useRef } from 'react'
22
import {
33
SearchPageContext,
44
SearchPageStateContext,
@@ -9,9 +9,6 @@ import { useCssHandles } from 'vtex.css-handles'
99
import { useRuntime } from 'vtex.render-runtime'
1010
import { generateBlockClass } from '@vtex/css-handles'
1111
import { pathOr, isEmpty } from 'ramda'
12-
import PostalCodeModal from 'vtex.shipping-option-components/PostalCodeModal'
13-
import PickupModal from 'vtex.shipping-option-components/PickupModal'
14-
import { usePixelEventCallback } from 'vtex.pixel-manager'
1512

1613
import SearchResultContainer from './components/SearchResultContainer'
1714
import ContextProviders from './components/ContextProviders'
@@ -20,9 +17,6 @@ import LoadingOverlay from './components/LoadingOverlay'
2017
import { PAGINATION_TYPE } from './constants/paginationType'
2118
import styles from './searchResult.css'
2219

23-
const DELIVER_DRAWER_PIXEL_EVENT_ID = 'shipping-option-deliver-to'
24-
const STORE_DRAWER_PIXEL_EVENT_ID = 'shipping-option-store'
25-
2620
const emptyFacets = {
2721
brands: [],
2822
priceRanges: [],
@@ -71,21 +65,6 @@ const SearchResultFlexible = ({
7165
thresholdForFacetSearch,
7266
lazyItemsRemaining,
7367
}) => {
74-
const [isPostalCodeModalOpen, setIsPostalCodeModalOpen] = useState(false)
75-
const [isPickupModalOpen, setisPickupModalOpen] = useState(false)
76-
77-
usePixelEventCallback({
78-
eventId: DELIVER_DRAWER_PIXEL_EVENT_ID,
79-
handler: () => setIsPostalCodeModalOpen(true),
80-
})
81-
82-
usePixelEventCallback({
83-
eventId: STORE_DRAWER_PIXEL_EVENT_ID,
84-
handler: () => {
85-
setisPickupModalOpen(true)
86-
},
87-
})
88-
8968
// This makes infinite scroll unavailable.
9069
// Infinite scroll was deprecated and we have
9170
// removed it since the flexible search release
@@ -253,14 +232,6 @@ const SearchResultFlexible = ({
253232
>
254233
{children}
255234
</div>
256-
<PostalCodeModal
257-
isOpen={isPostalCodeModalOpen}
258-
onClose={() => setIsPostalCodeModalOpen(false)}
259-
/>
260-
<PickupModal
261-
isOpen={isPickupModalOpen}
262-
onClose={() => setisPickupModalOpen(false)}
263-
/>
264235
</LoadingOverlay>
265236
</SearchResultContainer>
266237
</ContextProviders>

react/components/RadioFilters.js

+40-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React, { useEffect, useState } from 'react'
22
import { useIntl } from 'react-intl'
33
import { RadioGroup } from 'vtex.styleguide'
4+
import PostalCodeModal from 'vtex.shipping-option-components/PostalCodeModal'
5+
import PickupModal from 'vtex.shipping-option-components/PickupModal'
46

57
import ShippingActionButton from './ShippingActionButton'
68
import useShippingActions from '../hooks/useShippingActions'
79

8-
const RadioItem = ({ facet }) => {
10+
const RadioItem = ({ facet, onOpenPostalCodeModal, onOpenPickupModal }) => {
911
const intl = useIntl()
1012

11-
const { actionLabel, actionType, openDrawer } = useShippingActions(facet)
13+
const { actionLabel, actionType } = useShippingActions(facet)
1214

1315
return (
1416
<div>
1517
<div>{facet.name}</div>
1618
{actionType ? (
1719
<ShippingActionButton
1820
label={intl.formatMessage({ id: actionLabel ?? 'none' })}
19-
openDrawer={openDrawer}
21+
openDrawer={
22+
actionType === 'DELIVERY'
23+
? onOpenPostalCodeModal
24+
: onOpenPickupModal
25+
}
2026
/>
2127
) : undefined}
2228
</div>
@@ -26,6 +32,8 @@ const RadioItem = ({ facet }) => {
2632
const RadioFilters = ({ facets, onChange }) => {
2733
const selectedOption = facets.find(facet => facet.selected)
2834
const lastValue = selectedOption ? selectedOption.value : undefined
35+
const [isPostalCodeModalOpen, setIsPostalCodeModalOpen] = useState(false)
36+
const [isPickupModalOpen, setisPickupModalOpen] = useState(false)
2937

3038
const [selectedValue, setSelectedValue] = useState(lastValue)
3139

@@ -48,19 +56,35 @@ const RadioFilters = ({ facets, onChange }) => {
4856
}
4957

5058
return (
51-
<RadioGroup
52-
hideBorder
53-
size="small"
54-
name="shipping"
55-
options={facets.map(facet => ({
56-
id: facet.value,
57-
value: facet.value,
58-
label: <RadioItem facet={facet} />,
59-
disabled: facet.quantity === 0,
60-
}))}
61-
value={selectedValue}
62-
onChange={onRadioSelect}
63-
/>
59+
<>
60+
<RadioGroup
61+
hideBorder
62+
size="small"
63+
name="shipping"
64+
options={facets.map(facet => ({
65+
id: facet.value,
66+
value: facet.value,
67+
label: (
68+
<RadioItem
69+
facet={facet}
70+
onOpenPostalCodeModal={() => setIsPostalCodeModalOpen(true)}
71+
onOpenPickupModal={() => setisPickupModalOpen(true)}
72+
/>
73+
),
74+
disabled: facet.quantity === 0,
75+
}))}
76+
value={selectedValue}
77+
onChange={onRadioSelect}
78+
/>
79+
<PostalCodeModal
80+
isOpen={isPostalCodeModalOpen}
81+
onClose={() => setIsPostalCodeModalOpen(false)}
82+
/>
83+
<PickupModal
84+
isOpen={isPickupModalOpen}
85+
onClose={() => setisPickupModalOpen(false)}
86+
/>
87+
</>
6488
)
6589
}
6690

react/hooks/useShippingActions.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useCallback, useEffect, useState } from 'react'
2-
import { usePixel } from 'vtex.pixel-manager'
1+
import { useEffect, useState } from 'react'
32
import { useShippingOptionState } from 'vtex.shipping-option-components/ShippingOptionContext'
43

54
import useShouldDisableFacet from './useShouldDisableFacet'
@@ -21,11 +20,6 @@ const placeHolders = {
2120
PICKUP_POINT: 'store/search.filter.shipping.action-button.pickup-in-point',
2221
}
2322

24-
const drawerEvent = {
25-
DELIVERY: 'shipping-option-deliver-to',
26-
PICKUP_POINT: 'shipping-option-store',
27-
}
28-
2923
const addressDependentValues = [
3024
'delivery',
3125
'pickup-in-point',
@@ -44,8 +38,6 @@ const useShippingActions = facet => {
4438
const isAddressDependent =
4539
addressDependentValues.findIndex(value => facet.value === value) > -1
4640

47-
const { push } = usePixel()
48-
4941
const { zipcode, selectedPickup, city, addressLabel } =
5042
useShippingOptionState()
5143

@@ -88,27 +80,19 @@ const useShippingActions = facet => {
8880
addressLabel,
8981
])
9082

91-
const openDrawer = useCallback(() => {
92-
push({
93-
id: drawerEvent[actionType],
94-
})
95-
}, [actionType, push])
96-
9783
const shouldDisable = useShouldDisableFacet(facet, isAddressSet, isPickupSet)
9884

9985
if (facet.value === 'pickup-nearby' || facet.value === 'pickup') {
10086
return {
10187
actionLabel: null,
10288
actionType: null,
103-
openDrawer: null,
10489
shouldDisable,
10590
}
10691
}
10792

10893
return {
10994
actionType,
11095
actionLabel,
111-
openDrawer,
11296
shouldDisable,
11397
}
11498
}

0 commit comments

Comments
 (0)