diff --git a/react/client.ts b/react/client.ts index 1a18fab..05596fe 100644 --- a/react/client.ts +++ b/react/client.ts @@ -30,3 +30,16 @@ export const getPickups = ( fetch( `/api/checkout/pub/pickup-points?an=${account}&countryCode=${countryCode}&postalCode=${zipCode}` ).then((res) => res.json()) + +export const updateOrderForm = ( + country: string, + zipCode: string, + orderFormId: string +) => + fetch(`/api/checkout/pub/orderForm/${orderFormId}/attachments/shippingData`, { + method: 'POST', + body: `{"selectedAddresses": [{ "postalCode": ${zipCode}, "country": ${country} ]}`, + headers: { + 'Content-Type': 'application/json', + }, + }).then((res) => res.json()) diff --git a/react/hooks/useShippingOptions.ts b/react/hooks/useShippingOptions.ts index 1af2797..a52a4e6 100644 --- a/react/hooks/useShippingOptions.ts +++ b/react/hooks/useShippingOptions.ts @@ -4,9 +4,14 @@ import { useRuntime, useSSR } from 'vtex.render-runtime' import { useIntl } from 'react-intl' import { usePixel } from 'vtex.pixel-manager' -import { getCountryCode, getZipCode } from '../utils/cookie' +import { getCountryCode, getOrderFormId, getZipCode } from '../utils/cookie' import messages from '../messages' -import { getAddress, getPickups, updateSession } from '../client' +import { + getAddress, + getPickups, + updateOrderForm, + updateSession, +} from '../client' declare let window: any @@ -104,6 +109,12 @@ const useShippingOptions = () => { setIsLoading(true) + const orderFormId = getOrderFormId() + + if (orderFormId) { + await updateOrderForm(inputZipCode, orderFormId, countryCode) + } + const { geoCoordinates: coordinates } = await getAddress( countryCode, inputZipCode, diff --git a/react/utils/cookie.ts b/react/utils/cookie.ts index 9640cb3..be1b139 100644 --- a/react/utils/cookie.ts +++ b/react/utils/cookie.ts @@ -50,3 +50,13 @@ export function getCountryCode() { return countryCode } + +export function getOrderFormId() { + const orderForm = localStorage.getItem('orderform') + + if (!orderForm) { + return + } + + return JSON.parse(orderForm || '{}').id +}