Skip to content

Commit

Permalink
Moved idb back into sw.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kate Lovett committed Nov 3, 2018
1 parent 9749e01 commit 0f71ff6
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 27 deletions.
12 changes: 6 additions & 6 deletions app/js/dbhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DBHelper {
* Fetch all restaurants.
*/
static fetchRestaurants(callback) {
console.log(`In fetchRestaurants - callback: ${callback}`);
//console.log(`In fetchRestaurants - callback: ${callback}`);
let requestURL = DBHelper.DATABASE_URL;
// console.log(`Request URL: ${requestURL}`);
// Fetch restaurant data from server
Expand Down Expand Up @@ -304,7 +304,7 @@ class DBHelper {
console.log("In addToUpdateQueue catch, error: ", error.message);
})
// TODO : attempt push of updates
.then(DBHelper.pushUpdates());
//.then(DBHelper.pushUpdates());
}

/*
Expand Down Expand Up @@ -337,7 +337,7 @@ class DBHelper {
// Recursive call to push next update record. Will retrun in next call if empty.
.then(function() {
console.log("Record deleted, calling next...");
DBHelper.pushUpdates();
//DBHelper.pushUpdates();
})
})
})
Expand Down Expand Up @@ -372,7 +372,7 @@ class DBHelper {

/*
* Function to check idb for restaurant information
*/
*
static idbRestaurantRequest() {
return dbPromise.then(function(db) {
return db.transaction('restaurantData').objectStore('restaurantData').get(id);
Expand All @@ -393,7 +393,7 @@ class DBHelper {
/*
* Function to check idb for review information
*/
*
static idbReviewRequest() {
return dbPromise.then(function(db) {
return db.transaction('reviewData').objectStore('reviewData').index('restaurant_id').getAll(id);
Expand All @@ -420,6 +420,6 @@ class DBHelper {
return new Response(JSON.stringify(endResponse));
})
.catch(function(error) { console.log(`In apiFetch catch-reviewData, error: ${error.message}`); })
}
}*/

}
2 changes: 1 addition & 1 deletion app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ window.initMap = () => {
scrollwheel: false
});
updateRestaurants();
DBHelper.pushUpdates();
//DBHelper.pushUpdates();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion app/js/restaurant_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ window.initMap = () => {
DBHelper.mapMarkerForRestaurant(self.restaurant, self.map);
}
});
DBHelper.pushUpdates();
//DBHelper.pushUpdates();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion app/js/swregister.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Registering the Service Worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
navigator.serviceWorker.register('sw.js')
.then(function(registration) {
console.log('Registration successful, scope is: ', registration.scope);
})
Expand Down
66 changes: 53 additions & 13 deletions app/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
*/

// Include DBHelper functions for fetching restaurants and putting in indexedDB
self.importScripts('js/dbhelper.js', 'js/idb.js');
// self.importScripts('js/dbhelper.js', 'js/idb.js');
self.importScripts('js/idb.js');
// import idb from "idb";

// Cache Information
var myCache = 'restaurantReview_301';
var myCache = 'restaurantReview_302';
var cacheFiles = [
'/index.html',
'/restaurant.html',
'/css/styles.css',
'/img/',
'/js/dbhelper.js',
'/js/main.js',
'/js/restaurant_info.js',
'/js/swregister.js' ,
'/js/idb.js'
'index.html',
'restaurant.html',
'css/styles.css',
'img/',
'js/dbhelper.js',
'js/main.js',
'js/restaurant_info.js',
'js/swregister.js' ,
'js/idb.js'
];

// IDB Information
Expand Down Expand Up @@ -78,11 +80,49 @@ function apiFetch(event, id) {

// Restaurant Request
if(event.request.url.indexOf('restaurants') > -1) {
event.respondWith(DBHelper.idbRestaurantRequest());
event.respondWith(dbPromise.then(function(db) {
return db.transaction('restaurantData').objectStore('restaurantData').get(id);
})
.then(function(data) {
return (data && data.data) || fetch(event.request)
.then(function(response) {
return dbPromise
.then(function(db) {
db.transaction('restaurantData', 'readwrite').objectStore('restaurantData').put({ id: id, data: response.json() });
return response.json();
});
});
})
.then(function(endResponse) { return new Response(JSON.stringify(endResponse)); })
.catch(function(error) { console.log(`In DBHelper.idbRestaurantRequest, error: ${error.message}`); }));
}

// Review Request
event.respondWith(DBHelper.idbReviewRequest());
event.respondWith(dbPromise.then(function(db) {
return db.transaction('reviewData').objectStore('reviewData').index('restaurant_id').getAll(id);
})
.then(function(data) {
return (data && data.data) || fetch(event.request)
.then(function(response) {
return dbPromise
.then(function(db) {
let store = db.transaction('reviewData', 'readwrite').objectStore('reviewData');
let r = response.json();
r.forEach(function(review) {
store.put({id: review.id, 'restaurant_id': review.restaurant_id, data: review});
})
return r;
});
});
})
.then(function(endResponse) {
if(endResponse[0].data) {
let formatted = endResponse.map(review => review.data);
return new Response(JSON.stringify(formatted));
}
return new Response(JSON.stringify(endResponse));
})
.catch(function(error) { console.log(`In apiFetch catch-reviewData, error: ${error.message}`); }));
}

function cacheFetch(event, request) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/dbhelper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f71ff6

Please sign in to comment.