Skip to content

Commit

Permalink
Merge pull request #7978 from zsoh97/feat/iframe-messaging
Browse files Browse the repository at this point in the history
feat: add frame messaging
  • Loading branch information
KenLSM authored Dec 6, 2024
2 parents 9dd7191 + 31c7eb5 commit 4b38cc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions frontend/src/features/public-form/PublicFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {

import { FormNotFound } from './components/FormNotFound'
import { decryptAttachment, decryptSubmission } from './utils/decryptSubmission'
import { postIFrameMessage } from './utils/iframeMessaging'
import { usePublicAuthMutations, usePublicFormMutations } from './mutations'
import { PublicFormContext, SubmissionData } from './PublicFormContext'
import { useEncryptedSubmission, usePublicFormView } from './queries'
Expand Down Expand Up @@ -626,6 +627,8 @@ export const PublicFormProvider = ({
}
}

postIFrameMessage({ state: 'submitting' })

switch (form.responseMode) {
case FormResponseMode.Email: {
// Using mutateAsync so react-hook-form goes into loading state.
Expand Down Expand Up @@ -759,6 +762,7 @@ export const PublicFormProvider = ({
paymentData,
}) => {
trackSubmitForm(form)
postIFrameMessage({ state: 'submitted', submissionId })

if (paymentData) {
navigate(getPaymentPageUrl(formId, paymentData.paymentId))
Expand All @@ -781,6 +785,7 @@ export const PublicFormProvider = ({
},
)
.catch(async (error) => {
postIFrameMessage({ state: 'submitError' })
datadogLogs.logger.warn(`handleSubmitForm: ${error.message}`, {
meta: {
...logMeta,
Expand Down Expand Up @@ -823,6 +828,7 @@ export const PublicFormProvider = ({
paymentData,
}) => {
trackSubmitForm(form)
postIFrameMessage({ state: 'submitted', submissionId })
if (paymentData) {
navigate(getPaymentPageUrl(formId, paymentData.paymentId))
storePaymentMemory(paymentData.paymentId)
Expand All @@ -844,6 +850,7 @@ export const PublicFormProvider = ({
},
)
.catch(async (error) => {
postIFrameMessage({ state: 'submitError' })
// TODO(#5826): Remove when we have resolved the Network Error
datadogLogs.logger.warn(
`handleSubmitForm: submit with virus scan`,
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/features/public-form/utils/iframeMessaging.ts
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)
}
}

0 comments on commit 4b38cc6

Please sign in to comment.