-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7978 from zsoh97/feat/iframe-messaging
feat: add frame messaging
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 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
21 changes: 21 additions & 0 deletions
21
frontend/src/features/public-form/utils/iframeMessaging.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,21 @@ | ||
export type PublicFormIFrameMessage = | ||
| { state: 'submitting' | 'submitError' } | ||
| { state: 'submitted'; submissionId: string } | ||
|
||
const TRUSTED_TARGET_ORIGINS = [ | ||
'https://pay.gov.sg', | ||
'https://exp.pay.gov.sg', | ||
'https://staging.pay.gov.sg', | ||
] | ||
|
||
export const postIFrameMessage = (message: PublicFormIFrameMessage): void => { | ||
// De-risk by wrapping in try-catch even though this is synchronous. This should | ||
// never block form submission. | ||
try { | ||
TRUSTED_TARGET_ORIGINS.forEach((origin) => { | ||
window.parent.postMessage(message, origin) | ||
}) | ||
} catch (error) { | ||
console.warn('Error while posting form state to iframe parent', error) | ||
} | ||
} |