Skip to content

Instantly share code, notes, and snippets.

@evuazeze
Last active November 20, 2018 17:50
Show Gist options
  • Save evuazeze/3a608a7b9672162846cf36d711915624 to your computer and use it in GitHub Desktop.
Save evuazeze/3a608a7b9672162846cf36d711915624 to your computer and use it in GitHub Desktop.
...
handleConnectionChange = (event) => {
if(event.type == "online"){
// Setup the request
var headers = new Headers();
// Set some Headers
headers.set('Accept', 'application/json');
// Get Data from indexedDB
var name = return idb.open('personal_details', 1).then(function(db) {
var tx = db.transaction(['form_data'], 'readonly');
var store = tx.objectStore('form_data');
return store.getAll();
})
// Post data to server
name
.then(function(data) {
formData.append('name', data['name']);
var response = fetch(<server post endpoint>, {
method: 'POST',
headers,
body: formData
});
// If Post succeeds, delete data from IndexedDB
response
.then(function(){
// Delete locally stored data after successful post to server
idb.open('restaurant', 1).then(function(db) {
const tx = db.transaction('form_data', 'readwrite');
const store = tx.objectStore('form_data');
store.clear();
})
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment