Skip to content

Commit

Permalink
[#958] Handle manual sync to prevent duplicate submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ifirmawan committed Jan 24, 2024
1 parent e236385 commit 8f15d89
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions app/src/pages/FormData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import { Button, Dialog, Text } from '@rneui/themed';
import { View, ActivityIndicator, StyleSheet } from 'react-native';
import { View, ActivityIndicator, StyleSheet, ToastAndroid } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import moment from 'moment';

Expand All @@ -10,6 +10,7 @@ import { crudDataPoints } from '../database/crud';
import { i18n, backgroundTask, api } from '../lib';
import { UIState, FormState } from '../store';
import { getCurrentTimestamp } from '../form/lib';
import crudJobs, { jobStatus } from '../database/crud/crud-jobs';

const convertMinutesToHHMM = (minutes) => {
const hours = Math.floor(minutes / 60);
Expand Down Expand Up @@ -133,24 +134,38 @@ const FormDataPage = ({ navigation, route }) => {
setShowConfirmationSyncDialog(true);
};

const handleOnSync = () => {
setShowConfirmationSyncDialog(false);
setData([]);
setSyncing(true);
backgroundTask
.syncFormSubmission()
.then(async () => {
await fetchData();
})
.catch((e) => {
console.error('[Manual SyncFormSubmission]: ', e);
})
.finally(() => {
UIState.update((s) => {
s.isManualSynced = true;
});
setSyncing(false);
});
const runSyncSubmision = async () => {
await backgroundTask.syncFormSubmission();
await fetchData();
UIState.update((s) => {
s.isManualSynced = true;
});
setSyncing(false);
};

const handleOnSync = async () => {
try {
setShowConfirmationSyncDialog(false);
setData([]);
setSyncing(true);
const activeJob = await crudJobs.getActiveJob();
if (activeJob) {
/**
* Delete the active job while it is still in pending status to prevent duplicate submissions.
*/
if (activeJob.status === jobStatus.PENDING) {
await crudJobs.deleteJob(activeJob.id);
await runSyncSubmision();
} else {
ToastAndroid.show(trans.autoSyncInProgress, ToastAndroid.LONG);
setSyncing(false);
}
} else {
await runSyncSubmision();
}
} catch (e) {
console.error('[Manual SyncFormSubmission]: ', e);
}
};

const handleOnAction = showSubmitted ? goToDetails : goToEditForm;
Expand Down

0 comments on commit 8f15d89

Please sign in to comment.