Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace submissionService

Helper functions for handling form submissions

import { submissionService } from '@oneblink/apps'

Index

Type aliases

BaseFormSubmission: { definition: Form; submission: S3SubmissionData["submission"] }

Type declaration

  • definition: Form

    The form definition when the draft was saved

  • submission: S3SubmissionData["submission"]

    The submission data

DraftSubmission: NewDraftSubmission & { formsAppId: number; keyId?: string }
FormSubmission: DraftSubmission & NewFormSubmission & { draftId: string | null; externalId: string | null; jobId: string | null; preFillFormDataId: string | null; previousFormSubmissionApprovalId?: string; taskCompletion?: { redirectUrl: string; task: Task; taskAction: TaskAction; taskGroup: TaskGroup | undefined; taskGroupInstance: TaskGroupInstance | undefined } }
FormSubmissionResult: FormSubmission & { downloadSubmissionPdfUrl?: string; isInPendingQueue: boolean; isOffline: boolean; isUploadingAttachments: boolean; payment: { amount: number; hostedFormUrl: string; paymentFormUrl: string | undefined; paymentReceiptUrl: string; submissionEvent: FormPaymentEvent } | null; scheduling: { bookingUrl: string; submissionEvent: SchedulingSubmissionEvent } | null; submissionId: string | null; submissionTimestamp: string | null }
NewDraftSubmission: BaseFormSubmission & { backgroundUpload?: boolean; lastElementUpdated?: FormElement }
NewFormSubmission: BaseFormSubmission & { captchaTokens: string[] }
PendingFormSubmission: Omit<FormSubmission, "submission"> & { error?: string; isSubmitting?: boolean; pendingTimestamp: string }
PendingQueueAction: "SUBMIT_STARTED" | "SUBMIT_FAILED" | "SUBMIT_SUCCEEDED" | "ADDITION" | "DELETION"
PendingQueueListener: (results: PendingFormSubmission[], action: PendingQueueAction) => unknown

Type declaration

ProgressListener: (progress: ProgressListenerEvent) => void

Type declaration

ProgressListenerEvent: { progress: number; total: number }

Type declaration

  • progress: number
  • total: number
SubmissionParams: { abortSignal?: AbortSignal; formSubmission: FormSubmission; isPendingQueueEnabled: boolean; onProgress?: ProgressListener; paymentFormUrl: string | undefined; paymentReceiptUrl: string | undefined; schedulingUrlConfiguration?: { schedulingCancelUrl: string; schedulingReceiptUrl: string }; shouldRunExternalIdGeneration: boolean; shouldRunServerValidation: boolean }

Type declaration

  • Optional abortSignal?: AbortSignal
  • formSubmission: FormSubmission
  • isPendingQueueEnabled: boolean
  • Optional onProgress?: ProgressListener
  • paymentFormUrl: string | undefined
  • paymentReceiptUrl: string | undefined
  • Optional schedulingUrlConfiguration?: { schedulingCancelUrl: string; schedulingReceiptUrl: string }
    • schedulingCancelUrl: string
    • schedulingReceiptUrl: string
  • shouldRunExternalIdGeneration: boolean
  • shouldRunServerValidation: boolean

Functions

  • deletePendingQueueSubmission(pendingTimestamp: string): Promise<void>
  • Delete a PendingFormSubmission before it is processed based on the pendingTimestamp property.

    Example

    const pendingTimestamp = '2020-07-29T01:03:26.573Z'
    await submissionService.deletePendingQueueSubmission(pendingTimestamp)

    Parameters

    • pendingTimestamp: string

    Returns Promise<void>

  • executeCancelAction(options: { definition: Form; externalId: null | string; taskCompletion?: { redirectUrl: string; task: Task; taskAction: TaskAction; taskGroup: undefined | TaskGroup; taskGroupInstance: undefined | TaskGroupInstance } }, push: (url: string) => void): Promise<void>
  • Action to cancel completing a form, currently goes back in the browser history or attempts to close the browser tab if there is no history.

    Example

    const options = {
    definition: OneBlinkForm,
    externalId: 'external-id-set-by-developer',
    }
    // Only used for relative URLs
    const pushRelativePath = (path) => {
    window.location.href = path
    }
    try {
    await submissionService.executeCancelAction(options, pushRelativePath)
    } catch (error) {
    // Handle error while closing browser tab.
    // Display message to user to close it manually
    }

    Parameters

    • options: { definition: Form; externalId: null | string; taskCompletion?: { redirectUrl: string; task: Task; taskAction: TaskAction; taskGroup: undefined | TaskGroup; taskGroupInstance: undefined | TaskGroupInstance } }
      • definition: Form
      • externalId: null | string
      • Optional taskCompletion?: { redirectUrl: string; task: Task; taskAction: TaskAction; taskGroup: undefined | TaskGroup; taskGroupInstance: undefined | TaskGroupInstance }
        • redirectUrl: string

          The URL to redirect the user to after completing the task via form submission

        • task: Task

          The task

        • taskAction: TaskAction

          The task action

        • taskGroup: undefined | TaskGroup

          The task group

        • taskGroupInstance: undefined | TaskGroupInstance

          The task group instance

    • push: (url: string) => void
        • (url: string): void
        • Parameters

          • url: string

          Returns void

    Returns Promise<void>

  • executePostSubmissionAction(submissionResult: FormSubmissionResult, push: (url: string) => void): Promise<void>
  • Execute the post submission action for a form after a successful form submission.

    Example

    const formSubmissionResult = {
    submissionId: '89c6e98e-f56f-45fc-84fe-c4fc62331d34',
    submissionTimestamp: '2020-07-29T01:03:26.573Z'
    formsAppId: 1,
    submission: {
    form: 'data',
    goes: 'here'
    },
    definition: OneBlinkForm,
    payment: {
    hostedFormUrl: 'https://payment.com/transaction'
    },
    draftId: '2974602c-2c5b-4b46-b086-87ee9b2aa233',
    jobId: 'bb37d1da-9cda-4950-a36a-22f58b25de3a',
    preFillFormDataId: '7763f828-4aaf-49dc-9c1b-e2eeea8fa990',
    externalId: 'external-id-set-by-developer',
    }
    // Only used for relative URLs
    const pushRelativePath = (path) => {
    window.location.href = path
    }
    try {
    await submissionService.executePostSubmissionAction(formSubmissionResult, pushRelativePath)
    } catch (error) {
    // Handle error while closing browser tab.
    // Display message to user to close it manually
    }

    Parameters

    • submissionResult: FormSubmissionResult
    • push: (url: string) => void
        • (url: string): void
        • Parameters

          • url: string

          Returns void

    Returns Promise<void>

  • getSubmissionData(__namedParameters: { abortSignal?: AbortSignal; formId: number; submissionId: string }): Promise<S3SubmissionData>
  • Retrieve submission data for a known formId and submissionId

    Example

    const formId = 1
    const submissionId = 'fba95b9d-5a9c-463d-ab68-867f431e4120'
    const credentials = await formSubmissionService.getSubmissionData({
    formId,
    submissionId,
    })

    Parameters

    • __namedParameters: { abortSignal?: AbortSignal; formId: number; submissionId: string }
      • Optional abortSignal?: AbortSignal
      • formId: number
      • submissionId: string

    Returns Promise<S3SubmissionData>

  • goBackOrCloseWindow(): Promise<void>
  • Go back in the browser history or attempts to close the browser tab if there is no history.

    Example

    try {
    await submissionService.goBackOrCloseWindow()
    } catch (error) {
    // Handle error while closing browser tab.
    // Display message to user to close it manually
    }

    Returns Promise<void>

  • processPendingQueue(__namedParameters: { shouldRunExternalIdGeneration: boolean; shouldRunServerValidation: boolean }): Promise<void>
  • Force processing the pending queue. This must be called to process the pending queue and is best used when the application comes back online.

    Example

    await submissionService.processPendingQueue()
    

    Parameters

    • __namedParameters: { shouldRunExternalIdGeneration: boolean; shouldRunServerValidation: boolean }
      • shouldRunExternalIdGeneration: boolean
      • shouldRunServerValidation: boolean

    Returns Promise<void>

  • registerPendingQueueAttachmentProgressListener(attachmentId: string, listener: ProgressListener): () => void
  • Register a listener function that will be passed a progress event when an attachment for an item in the pending queue is being processed.

    Example

    const listener = async ({ progress }) => {
    // update the UI to reflect the progress here...
    }
    const deregister =
    await submissionService.registerPendingQueueAttachmentProgressListener(
    attachment.id,
    listener,
    )

    // When no longer needed, remember to deregister the listener
    deregister()

    Parameters

    Returns () => void

      • (): void
      • Register a listener function that will be passed a progress event when an attachment for an item in the pending queue is being processed.

        Example

        const listener = async ({ progress }) => {
        // update the UI to reflect the progress here...
        }
        const deregister =
        await submissionService.registerPendingQueueAttachmentProgressListener(
        attachment.id,
        listener,
        )

        // When no longer needed, remember to deregister the listener
        deregister()

        Returns void

  • Register a listener function that will be passed an array of PendingFormSubmissions when the pending queue is modified.

    Example

    const listener = async (pendingSubmissions) => {
    // use pending submissions here...
    }
    const deregister =
    await submissionService.registerPendingQueueListener(listener)

    // When no longer needed, remember to deregister the listener
    deregister()

    Parameters

    Returns () => void

      • (): void
      • Register a listener function that will be passed an array of PendingFormSubmissions when the pending queue is modified.

        Example

        const listener = async (pendingSubmissions) => {
        // use pending submissions here...
        }
        const deregister =
        await submissionService.registerPendingQueueListener(listener)

        // When no longer needed, remember to deregister the listener
        deregister()

        Returns void

  • registerPendingQueueProgressListener(pendingTimestamp: string, listener: ProgressListener): () => void
  • Register a listener function that will be passed a progress event when an item in the pending queue is being processed.

    Example

    const listener = async ({ progress }) => {
    // update the UI to reflect the progress here...
    }
    const deregister =
    await submissionService.registerPendingQueueProgressListener(
    pendingQueueItem.pendingTimestamp,
    listener,
    )

    // When no longer needed, remember to deregister the listener
    deregister()

    Parameters

    Returns () => void

      • (): void
      • Register a listener function that will be passed a progress event when an item in the pending queue is being processed.

        Example

        const listener = async ({ progress }) => {
        // update the UI to reflect the progress here...
        }
        const deregister =
        await submissionService.registerPendingQueueProgressListener(
        pendingQueueItem.pendingTimestamp,
        listener,
        )

        // When no longer needed, remember to deregister the listener
        deregister()

        Returns void

  • Submit a FormSubmission. Offline submissions will be added to a pending queue and be processed using the processPendingQueue() function. FormSubmissions with payment submission events will return the FormSubmissionResult with a payment property set, this should be used to redirect the user to the payment URL. Will also handle cleaning up auto save data (if the autoSaveKey property is passed), locally stored drafts and prefill data.

    Example

    const formSubmission = {
    formsAppId: 1,
    submission: {
    form: 'data',
    goes: 'here',
    },
    definition: OneBlinkForm,
    captchaTokens: [],
    draftId: '2974602c-2c5b-4b46-b086-87ee9b2aa233',
    jobId: 'bb37d1da-9cda-4950-a36a-22f58b25de3a',
    preFillFormDataId: '7763f828-4aaf-49dc-9c1b-e2eeea8fa990',
    externalId: 'external-id-set-by-developer',
    }

    // Pass paymentReceiptUrl if submission may require a payment
    const paymentReceiptUrl = `${window.location.origin}/payment-receipt`

    // Pass schedulingBookingUrlConfiguration if submission utilise scheduling
    const schedulingBookingUrlConfiguration = {
    schedulingReceiptUrl: 'https://my-website.com/receipt',
    schedulingCancelUrl: 'https://my-website.com/cancel',
    }
    const submissionResult = await submissionService.submit({
    formSubmission,
    paymentReceiptUrl,
    schedulingBookingUrlConfiguration,
    })

    if (submissionResult.scheduling) {
    // Redirect user to booking form
    window.location.href = submissionResult.scheduling.bookingUrl
    return
    }

    if (submissionResult.payment) {
    // Redirect user to payment form
    window.location.href = submissionResult.payment.hostedFormUrl
    return
    }

    if (submissionResult.isOffline) {
    if (submissionResult.isInPendingQueue) {
    // Display message to user that the submission
    // has been added to the pending queue
    } else {
    // Display message to user that this submission can
    // not be processed while offline (most likely because it requires a payment)
    }
    return
    }

    // submissionResult.submissionId and submissionResult.submissionTimestamp
    // will be set if the submission was successful

    Parameters

    Returns Promise<FormSubmissionResult>