Skip to content

Commit a147216

Browse files
JaviMunitaJaviera Munita
and
Javiera Munita
authored
feat(PDYE-988): función cookie (#651)
Co-authored-by: Javiera Munita <javieramunita@MacBook-Air-de-Javiera.local>
1 parent 9e303a5 commit a147216

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export function UserWayCookie(): void {
2+
const cookieName = 'userwayData'
3+
4+
const userwayDataCookie = document.cookie
5+
.split('; ')
6+
.find((row) => row.startsWith(`${cookieName}=`))
7+
?.split('=')[1]
8+
9+
if (userwayDataCookie) {
10+
try {
11+
const userwayData = JSON.parse(decodeURIComponent(userwayDataCookie))
12+
for (const [key, value] of Object.entries(userwayData)) {
13+
localStorage.setItem(key, value as string)
14+
}
15+
console.log('UserWay data restored to localStorage:', userwayData)
16+
} catch (error) {
17+
console.error('Error parsing UserWay data from cookie:', error)
18+
}
19+
}
20+
21+
window.addEventListener('beforeunload', () => {
22+
const userwayData: Record<string, string | null> = {}
23+
for (const key in localStorage) {
24+
if (key.startsWith('userway-')) {
25+
userwayData[key] = localStorage.getItem(key)
26+
}
27+
}
28+
29+
const serializedData = JSON.stringify(userwayData)
30+
const expiryDate = new Date()
31+
expiryDate.setTime(expiryDate.getTime() + 24 * 60 * 60 * 1000)
32+
33+
document.cookie = `${cookieName}=${encodeURIComponent(
34+
serializedData
35+
)};expires=${expiryDate.toUTCString()};path=/`
36+
})
37+
}

src/molecules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { BtnTertiary } from './Buttons/BtnTertiary'
55
export { BtnLink } from './Buttons/BtnLink'
66
export { NewTooltip } from './Tooltip/NewTooltip'
77
export { UserWay } from './UserWay/UserWay'
8+
export { UserWayCookie } from './UserWay/UserWayCookie'

0 commit comments

Comments
 (0)