forked from vtex-apps/store-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (113 loc) · 3.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { Fragment, useState } from 'react'
import { injectIntl, intlShape } from 'react-intl'
import { compose, withApollo } from 'react-apollo'
import PropTypes from 'prop-types'
import { Button } from 'vtex.styleguide'
import {
AddressRules,
AddressContainer,
PostalCodeGetter,
} from 'vtex.address-form'
import { StyleguideInput } from 'vtex.address-form/inputs'
import { addValidation, removeValidation } from 'vtex.address-form/helpers'
import ShippingTable from './components/ShippingTable'
import getShippingEstimates from './queries/getShippingEstimates.gql'
import ShippingSimulatorLoader from './Loader'
import styles from './shippingSimulator.css'
import { getNewAddress } from './utils'
const ShippingSimulator = ({
intl,
client,
skuId,
seller,
country,
loaderStyles,
}) => {
const [address, setAddress] = useState(() =>
addValidation(getNewAddress(country))
)
const [shipping, setShipping] = useState(null)
const [loading, setLoading] = useState(false)
const [isValid, setIsValid] = useState(false)
const handleAddressChange = newAddress => {
const updatedAddress = {
...address,
...newAddress,
}
setAddress(updatedAddress)
setIsValid(updatedAddress.postalCode.valid)
}
const handleClick = e => {
e.preventDefault()
setLoading(true)
const { postalCode } = removeValidation(address)
client
.query({
query: getShippingEstimates,
variables: {
country,
postalCode,
items: [
{
quantity: '1',
id: skuId,
seller,
},
],
},
})
.then(result => {
setShipping(result.data.shipping)
})
.catch(error => {
console.error(error)
})
.finally(() => {
setLoading(false)
})
}
if (!seller || !skuId) {
return <ShippingSimulatorLoader {...loaderStyles} />
}
return (
<Fragment>
<div className={`${styles.shippingContainer} t-small c-on-base`}>
<AddressRules country={country} shouldUseIOFetching>
<AddressContainer
Input={StyleguideInput}
address={address}
onChangeAddress={handleAddressChange}
autoCompletePostalCode
>
<PostalCodeGetter />
</AddressContainer>
</AddressRules>
<Button
onClick={handleClick}
className={styles.shippingCTA}
disabled={!isValid}
size="small"
type="submit"
block
isLoading={loading}
>
{intl.formatMessage({ id: 'store/shipping.label' })}
</Button>
</div>
<ShippingTable shipping={shipping} />
</Fragment>
)
}
ShippingSimulator.propTypes = {
intl: intlShape.isRequired,
client: PropTypes.object,
skuId: PropTypes.string,
seller: PropTypes.string,
country: PropTypes.string.isRequired,
/** Component and content loader styles */
styles: PropTypes.object,
}
export default compose(
withApollo,
injectIntl
)(ShippingSimulator)