Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `jobService.registerJobsListener()` and `jobService.notifyJobSubmitted()` for reacting to job submissions
- sms MFA setup support to `useMfa()` and `<MultiFactorAuthentication />`
- `mfaService` for MFA setup APIs and MFA requirement helpers
- export `LoadingWithMessage` component
Expand All @@ -17,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `submissionService.submit()` to notify job listeners when a submission includes a `jobId`
- **[BREAKING]** `LoginAttemptResponse` to replace `mfaCodeCallback` with `mfa.codeCallback` and `mfa.method`
- **[BREAKING]** `useLogin()` to remove `isMfaCodeRequired`; use `mfaMethod` as the source of truth for an active MFA step
- **[BREAKING]** `useMfa()` to require an `mfaMethod` parameter for `beginMfaSetup(mfaMethod)`
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/apps/job-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,63 @@ import tenants from './tenants'
import { SubmissionTypes } from '@oneblink/types'
import Sentry from './Sentry'

export type JobListenerEvent = {
action: 'SUBMITTED'
jobId: string
}

export type JobsListener = (event: JobListenerEvent) => unknown

const jobsListeners: Array<JobsListener> = []

/**
* Register a listener function that will be called when jobs state should be
* updated after a submission.
*
* #### Example
*
* ```js
* const listener = ({ action, jobId }) => {
* if (action === 'SUBMITTED') {
* // remove job from local state...
* }
* }
* const deregister = jobService.registerJobsListener(listener)
*
* // When no longer needed, remember to deregister the listener
* deregister()
* ```
*
* @param listener
* @returns
*/
export function registerJobsListener(listener: JobsListener): () => void {
jobsListeners.push(listener)

return () => {
const index = jobsListeners.indexOf(listener)
if (index !== -1) {
jobsListeners.splice(index, 1)
}
}
}

function executeJobsListeners(event: JobListenerEvent) {
for (const jobsListener of jobsListeners) {
jobsListener(event)
}
}

/**
* Notify listeners that a job has been submitted and should no longer appear in
* the jobs list.
*
* @param jobId
*/
export function notifyJobSubmitted(jobId: string) {
executeJobsListeners({ action: 'SUBMITTED', jobId })
}

async function removePendingSubmissions(
jobList: SubmissionTypes.FormsAppJob[],
) {
Expand Down
12 changes: 12 additions & 0 deletions src/apps/services/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import serverValidateForm from './server-validation'
import OneBlinkAppsError from './errors/oneBlinkAppsError'
import { uploadFormSubmission } from './api/submissions'
import { syncDrafts } from '../draft-service'
import { notifyJobSubmitted } from '../job-service'

function notifyJobSubmittedIfApplicable(formSubmission: FormSubmission) {
if (formSubmission.jobId) {
notifyJobSubmitted(formSubmission.jobId)
}
}

type SubmissionParams = {
formSubmission: FormSubmission
Expand Down Expand Up @@ -106,6 +113,7 @@ export default async function submit({
'Offline or always submitting via pending queue - saving submission to pending queue..',
)
await addFormSubmissionToPendingQueue(formSubmission)
notifyJobSubmittedIfApplicable(formSubmission)
return Object.assign({}, formSubmission, {
isOffline: isOffline(),
isInPendingQueue: true,
Expand Down Expand Up @@ -144,6 +152,7 @@ export default async function submit({
'Attachments still uploading - saving submission to pending queue..',
)
await addFormSubmissionToPendingQueue(formSubmission)
notifyJobSubmittedIfApplicable(formSubmission)
return Object.assign({}, formSubmission, {
isOffline: false,
isInPendingQueue: true,
Expand Down Expand Up @@ -260,6 +269,8 @@ export default async function submit({
await removePrefillFormData(formSubmission.preFillFormDataId)
}

notifyJobSubmittedIfApplicable(formSubmission)

return formSubmissionResult
} catch (error: OneBlinkAppsError | unknown) {
if (error instanceof OneBlinkAppsError) {
Expand Down Expand Up @@ -290,6 +301,7 @@ export default async function submit({
error,
)
await addFormSubmissionToPendingQueue(formSubmission)
notifyJobSubmittedIfApplicable(formSubmission)
return Object.assign({}, formSubmission, {
isOffline: true,
isInPendingQueue: true,
Expand Down
Loading