Last active
November 20, 2018 17:50
-
-
Save evuazeze/3a608a7b9672162846cf36d711915624 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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