-
Notifications
You must be signed in to change notification settings - Fork 0
AP-7184b # draftService.syncDrafts to download draft data in the ba…
#1023
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
Changes from all commits
3abc543
2432d6c
ffdcbed
f2499e7
d1b846f
4613e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,11 @@ import OneBlinkAppsError from './services/errors/oneBlinkAppsError' | |
| import { isOffline } from './offline-service' | ||
| import { getUsername } from './services/cognito' | ||
| import { getFormsKeyId, getCurrentFormsAppUser } from './auth-service' | ||
| import { getFormSubmissionDrafts, uploadDraftData } from './services/api/drafts' | ||
| import { | ||
| DRAFT_DATA_UNAVAILABLE_ERROR_TITLE, | ||
| getFormSubmissionDrafts, | ||
| uploadDraftData, | ||
| } from './services/api/drafts' | ||
| import { | ||
| getPendingQueueSubmissions, | ||
| deletePendingQueueSubmission, | ||
|
|
@@ -92,6 +96,7 @@ function generateLocalFormSubmissionDraftsFromDraftSubmissions( | |
| taskActionId: draftSubmission.taskCompletion?.taskAction.taskActionId, | ||
| draftSubmission, | ||
| versions: undefined, | ||
| downloadStatus: 'SUCCESS', | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -125,7 +130,9 @@ async function generatePublicLocalFormSubmissionDraftsFromStorage( | |
| ) | ||
|
|
||
| return _orderBy(localFormSubmissionDrafts, (localFormSubmissionDraft) => { | ||
| return localFormSubmissionDraft.draftSubmission?.createdAt | ||
| return localFormSubmissionDraft.downloadStatus === 'SUCCESS' | ||
| ? localFormSubmissionDraft.draftSubmission?.createdAt | ||
| : undefined | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -144,6 +151,22 @@ async function generateLocalFormSubmissionDraftsFromStorage( | |
| deletedDraftIds, | ||
| ) | ||
|
|
||
| async function broadcastUpdate() { | ||
| const draftsToBroadcast = Array.from(localFormSubmissionDraftsMap.values()) | ||
|
|
||
| const orderedDrafts = _orderBy( | ||
| draftsToBroadcast, | ||
| (localFormSubmissionDraft) => | ||
| getLatestFormSubmissionDraftVersion(localFormSubmissionDraft.versions) | ||
| ?.createdAt, | ||
| ) | ||
|
|
||
| await executeDraftsListeners(orderedDrafts) | ||
| } | ||
|
|
||
| // At this point we need to store the state of the drafts in localForage | ||
| const draftsToDownload: SubmissionTypes.FormSubmissionDraft[] = [] | ||
|
|
||
| for (const formSubmissionDraft of localDraftsStorage.syncedFormSubmissionDrafts) { | ||
| if ( | ||
| // Unsycned version of draft takes priority over the synced version | ||
|
|
@@ -153,33 +176,79 @@ async function generateLocalFormSubmissionDraftsFromStorage( | |
| // Remove any drafts deleted while offline | ||
| !deletedDraftIds.has(formSubmissionDraft.id) | ||
| ) { | ||
| draftsToDownload.push(formSubmissionDraft) | ||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...formSubmissionDraft, | ||
| downloadStatus: 'PENDING', | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| await broadcastUpdate() | ||
|
|
||
| // TODO Batch the downloads instead of sequentially | ||
| if (draftsToDownload.length) { | ||
| for (const formSubmissionDraft of draftsToDownload) { | ||
| const currentValue = localFormSubmissionDraftsMap.get( | ||
| formSubmissionDraft.id, | ||
| ) | ||
| if (currentValue) { | ||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...currentValue, | ||
| downloadStatus: 'DOWNLOADING', | ||
| }) | ||
| } | ||
| await broadcastUpdate() | ||
|
|
||
| const draftSubmission = await getDraftSubmission( | ||
| formSubmissionDraft, | ||
| ).catch((err) => { | ||
| console.warn( | ||
| `Could not fetch draft submission for draft: ${formSubmissionDraft.id}`, | ||
| err, | ||
| ) | ||
|
|
||
| if ( | ||
| err instanceof OneBlinkAppsError && | ||
| err.title === DRAFT_DATA_UNAVAILABLE_ERROR_TITLE | ||
| ) { | ||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...formSubmissionDraft, | ||
| downloadStatus: 'NOT_AVAILABLE', | ||
| }) | ||
| return | ||
| } | ||
|
Comment on lines
203
to
+220
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made it a separate task, a slight improvement. https://linear.app/oneblink/issue/AP-7331/apps-react-or-maybe-change-getdraftsubmission-to-return-undefined-if |
||
|
|
||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...formSubmissionDraft, | ||
| downloadStatus: 'ERROR', | ||
| downloadError: err.message, | ||
| }) | ||
|
|
||
| return undefined | ||
| }) | ||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...formSubmissionDraft, | ||
| draftSubmission, | ||
| }) | ||
| if (draftSubmission) { | ||
| localFormSubmissionDraftsMap.set(formSubmissionDraft.id, { | ||
| ...formSubmissionDraft, | ||
| draftSubmission: draftSubmission, | ||
| downloadStatus: 'SUCCESS', | ||
| }) | ||
| } | ||
|
|
||
| await broadcastUpdate() | ||
| } | ||
| } | ||
|
|
||
| const localFormSubmissionDrafts = Array.from( | ||
| localFormSubmissionDraftsMap.values(), | ||
| ) | ||
|
|
||
| return _orderBy(localFormSubmissionDrafts, (localFormSubmissionDraft) => { | ||
| return ( | ||
| localFormSubmissionDraft.draftSubmission?.createdAt || | ||
| return _orderBy( | ||
| localFormSubmissionDrafts, | ||
| (localFormSubmissionDraft) => | ||
| getLatestFormSubmissionDraftVersion(localFormSubmissionDraft.versions) | ||
| ?.createdAt | ||
| ) | ||
| }) | ||
| ?.createdAt, | ||
| ) | ||
| } | ||
|
|
||
| function errorHandler(error: Error): Error { | ||
|
|
@@ -800,24 +869,29 @@ async function syncDrafts({ | |
| localDraftsStorage.syncedFormSubmissionDrafts = formSubmissionDrafts | ||
| } | ||
|
|
||
| await setAndBroadcastDrafts(localDraftsStorage) | ||
|
|
||
| if (localDraftsStorage.syncedFormSubmissionDrafts.length) { | ||
| console.log( | ||
| 'Ensuring all draft data is available for offline use for synced drafts', | ||
| localDraftsStorage.syncedFormSubmissionDrafts, | ||
| ) | ||
| for (const formSubmissionDraft of localDraftsStorage.syncedFormSubmissionDrafts) { | ||
| await getDraftSubmission(formSubmissionDraft, abortSignal).catch( | ||
| (error) => { | ||
| console.warn('Could not download Draft Data as JSON', error) | ||
| }, | ||
| console.log('Downloading drafts in the background') | ||
| setAndBroadcastDrafts(localDraftsStorage) | ||
mymattcarroll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .then(async () => { | ||
| console.log('Finished syncing drafts.') | ||
| }) | ||
| .catch((error) => { | ||
| if (abortSignal?.aborted) { | ||
| console.log('Syncing drafts has been aborted') | ||
| return | ||
| } | ||
| console.warn( | ||
| 'Error while attempting download drafts in the background', | ||
| error, | ||
| ) | ||
| } | ||
| } | ||
| if (!(error instanceof OneBlinkAppsError)) { | ||
| Sentry.captureException(error) | ||
| } | ||
| }) | ||
| .finally(() => { | ||
| _isSyncingDrafts = false | ||
| }) | ||
|
|
||
| console.log('Finished syncing drafts.') | ||
| _isSyncingDrafts = false | ||
| // broadcast the drafts and download the draft data in the background | ||
| } catch (error) { | ||
| _isSyncingDrafts = false | ||
| if (abortSignal?.aborted) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.