File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -5,3 +5,4 @@ export { BtnTertiary } from './Buttons/BtnTertiary'
5
5
export { BtnLink } from './Buttons/BtnLink'
6
6
export { NewTooltip } from './Tooltip/NewTooltip'
7
7
export { UserWay } from './UserWay/UserWay'
8
+ export { UserWayCookie } from './UserWay/UserWayCookie'
You can’t perform that action at this time.
0 commit comments