-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (63 loc) · 2.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// App.JS
// Listner to check if online status
window.addEventListener('online', function() {
apex.message.showPageSuccess('You are back online!');
uploadOfflineFormData();
});
// Listner to check if offline status
window.addEventListener('offline', function() {
$('#t_Alert_Success').remove();
apex.message.clearErrors();
apex.message.showErrors([{
type: 'error',
location: 'page',
message: 'You have lost connection'
}]);
});
// Process call to insert data offline data back to server
function submitIndexdbData(formData) {
apex.server.process( "syncInsertion", {
x01: formData.get('City'),
x02: formData.get('Name'),
x03: formData.get('Phone')
}, {
success: function(data) {
},
error: function( jqXHR, textStatus, errorThrown ) {
apex.page.submit();
$('#t_Alert_Success').remove();
apex.message.showPageSuccess("Submitted Offline Data")
}
});
}
// Cache management
function uploadOfflineFormData() {
const request = indexedDB.open('offline-forms', 1);
request.onerror = function(event) {
console.error('Error opening IndexedDB:', event.target.error);
};
request.onsuccess = function(event) {
const db = event.target.result;
const transaction = db.transaction(['forms'], 'readwrite');
const objectStore = transaction.objectStore('forms');
const getAllRequest = objectStore.getAll();
getAllRequest.onerror = function(event) {
console.error('Error retrieving form data from IndexedDB:', event.target.error);
};
getAllRequest.onsuccess = async function(event) {
const formDataArray = event.target.result;
for (const formDataEntry of formDataArray) {
const formData = new FormData();
Object.entries(formDataEntry).forEach(([key, value]) => {
formData.append(key, value);
});
try {
await submitIndexdbData(formData);
objectStore.delete(formDataEntry.id);
} catch (error) {
console.error('Error uploading form data:', error.message);
}
}
};
};
}