Skip to content

Commit

Permalink
add shipping_info cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
hiagolcm committed Dec 18, 2024
1 parent 87be6e3 commit d9a5ad7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.4.2] - 2024-11-06
### Fixed

- sometimes zip code is not updated due to a problem in `__RUNTIME__.segmentToken`

## [0.4.2] - 2024-11-06

### Fixed

Expand All @@ -20,7 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Drawer opens behind the sidebar


## [0.4.0] - 2024-10-04

### Fixed
Expand Down
20 changes: 15 additions & 5 deletions react/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { SHIPPING_INFO_COOKIE } from './constants'
import { setCookie } from './utils/cookie'

export const getAddress = (
countryCode: string,
zipCode: string,
Expand All @@ -11,16 +14,23 @@ export const updateSession = (
zipCode: string,
geoCoordinates: number[],
pickup?: Pickup
) =>
fetch('/api/sessions', {
) => {
const facetsValue = `zip-code=${zipCode};coordinates=${geoCoordinates.join(
','
)}${pickup ? `;pickupPoint=${pickup.pickupPoint.id}` : ''}`

// __RUNTIME__.segmentToken is not reliable for the facets. It might not be updated. For this reason we must try to get the info from our custom cookie first
// Replacing ";" by ":" because ";" is not allowed in cookies
setCookie(SHIPPING_INFO_COOKIE, facetsValue.replace(';', ':'))

return fetch('/api/sessions', {
method: 'POST',
body: `{"public":{"facets":{"value":"zip-code=${zipCode};coordinates=${geoCoordinates.join(
','
)}${pickup ? `;pickupPoint=${pickup.pickupPoint.id}` : ''}"}}}`,
body: `{"public":{"facets":{"value":"${facetsValue}"}}}`,
headers: {
'Content-Type': 'application/json',
},
})
}

export const getPickups = (
countryCode: string,
Expand Down
1 change: 1 addition & 0 deletions react/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const DELIVER_DRAWER_PIXEL_EVENT_ID = 'shipping-option-deliver-to'
export const STORE_DRAWER_PIXEL_EVENT_ID = 'shipping-option-store'
export const SHIPPING_INFO_COOKIE = 'shipping_info'
31 changes: 26 additions & 5 deletions react/utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SHIPPING_INFO_COOKIE } from '../constants'

export function getCookie(name: string) {
const value = `; ${document.cookie}`
const parts = value.split(`; ${name}=`)
Expand All @@ -9,20 +11,39 @@ export function getCookie(name: string) {
return undefined
}

export function setCookie(name: string, val: string) {
const date = new Date()
const value = val

date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000)

document.cookie = `${name}=${value}; expires=${date.toUTCString()}; path=/`
}

export function getFacetsData(facetsDataTarget: string) {
const segment = (window as any)?.__RUNTIME__.segmentToken
// __RUNTIME__.segmentToken is not reliable for the facets. It might not be updated. For this reason we must try to get the info from our custom cookie first

if (!segment) {
return
}
let facets = getCookie(SHIPPING_INFO_COOKIE)

if (!facets) {
const segment =
getCookie(SHIPPING_INFO_COOKIE) ??
(window as any)?.__RUNTIME__.segmentToken

if (!segment) {
return
}

const { facets } = JSON.parse(atob(segment))
facets = JSON.parse(atob(segment)).facets
}

if (!facets) {
return
}

// In case the facets came from the shipping_info cookie we must replace ":" by ";" because ";" is not allowed in cookies.
const facetsTarget = facets
.replace(':', ';')
.split(';')
.find((facet: string) => facet.indexOf(facetsDataTarget) > -1)

Expand Down

0 comments on commit d9a5ad7

Please sign in to comment.