Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iframe): add frame messaging for paysg #7979

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions frontend/src/features/public-form/PublicFormProvider.tsx
Original file line number Diff line number Diff line change
@@ -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'
@@ -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.
@@ -759,6 +762,7 @@ export const PublicFormProvider = ({
paymentData,
}) => {
trackSubmitForm(form)
postIFrameMessage({ state: 'submitted', submissionId })

if (paymentData) {
navigate(getPaymentPageUrl(formId, paymentData.paymentId))
@@ -781,6 +785,7 @@ export const PublicFormProvider = ({
},
)
.catch(async (error) => {
postIFrameMessage({ state: 'submitError' })
datadogLogs.logger.warn(`handleSubmitForm: ${error.message}`, {
meta: {
...logMeta,
@@ -823,6 +828,7 @@ export const PublicFormProvider = ({
paymentData,
}) => {
trackSubmitForm(form)
postIFrameMessage({ state: 'submitted', submissionId })
if (paymentData) {
navigate(getPaymentPageUrl(formId, paymentData.paymentId))
storePaymentMemory(paymentData.paymentId)
@@ -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`,
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)
}
}
Loading