-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG][Checkout Widget] Feat: Checkout widget refactor iframe…
… params (#2168)
- Loading branch information
Showing
8 changed files
with
481 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/widgets/checkout/hooks/useAsyncMemo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Handle asynchronous operations with memoization. | ||
* It only re-executes the async function when dependencies change. | ||
*/ | ||
export const useAsyncMemo = <T>( | ||
asyncFn: () => Promise<T>, | ||
dependencies: any[], | ||
): T => { | ||
const [value, setValue] = useState<T>(); | ||
|
||
useEffect(() => { | ||
let isMounted = true; | ||
|
||
asyncFn().then((result) => { | ||
if (isMounted) setValue(result); | ||
}); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}, dependencies); | ||
|
||
return value as T; | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/widgets/checkout/utils/encode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Encodes a JSON object using base64 encoding. | ||
*/ | ||
export const encodeObject = async (value: Object): Promise<string> => { | ||
try { | ||
const str = JSON.stringify(value); | ||
const base64String = btoa(str); | ||
return encodeURIComponent(base64String); | ||
} catch (error) { | ||
throw new Error(`Compression failed: ${(error as Error).message}`); | ||
} | ||
}; | ||
|
||
/** | ||
* Decodes a string encoded using encodeObject. | ||
*/ | ||
export const decodeObject = async ( | ||
encodedValue: string, | ||
): Promise<Object> => { | ||
try { | ||
const decodedString = atob(decodeURIComponent(encodedValue)); | ||
return JSON.parse(decodedString); | ||
} catch (error) { | ||
throw new Error(`Decompression failed: ${(error as Error).message}`); | ||
} | ||
}; |
Oops, something went wrong.