Skip to content

Commit

Permalink
Adding the country and postalCode to orderForm
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoTadeuF committed Sep 27, 2024
1 parent bfd09db commit 4432001
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
13 changes: 13 additions & 0 deletions react/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
15 changes: 13 additions & 2 deletions react/hooks/useShippingOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions react/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4432001

Please sign in to comment.