Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shipping_info cookie #12

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

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

## [0.5.0] - 2024-12-18

### Added

- Popover to open the location drawer;
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() + 30 * 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
Loading