diff --git a/README.md b/README.md index 88041c7..c3dcbd2 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,9 @@ Capstone project for Udacity's **Mobile Web Specialist Nano-Degree**, part of th --- -## Current State / Branch : Phase 3 +## Current State : Final / Completed -Rubric requirements for this stage of the project include: - -* +![Image of Certificate of Completion](cert.png) --- diff --git a/app/js/dbhelper.js b/app/js/dbhelper.js index 56091fa..93f6463 100644 --- a/app/js/dbhelper.js +++ b/app/js/dbhelper.js @@ -27,33 +27,18 @@ class DBHelper { * Fetch all restaurants. */ static fetchRestaurants(callback) { - //console.log(`In fetchRestaurants - callback: ${callback}`); let requestURL = DBHelper.DATABASE_URL; - // console.log(`Request URL: ${requestURL}`); // Fetch restaurant data from server fetch(requestURL, {method: "GET"}) .then(function(response) { // If response returns ok, return json of restaurant data if (response.ok) return response.json(); - - // If not returned, there is an error - or offline mode - // throw new Error("Fetch response Error in fetchRestaurants"); }) .then(function(restaurantData) { callback(null, restaurantData); }) .catch(function(error) { console.log("In fetchRestaurants catch, error: ", error.message); - // // Retrieve data from IndexedDB - // idb.open('restaurantReviews', 1) - // .then(db => { - // return db.transaction("restaurantData") - // .objectStore("restaurantData") - // .getAll(); - // }) - // .then(restaurantData => { - // callback(null,restaurantData); - // }); }); } @@ -205,21 +190,14 @@ class DBHelper { .then(function(response) { console.log("response: ", response); if (response.ok) return response.json(); - - //throw new Error("Fetch response Error in fetchReviewsBy...ID") }) .then(function(reviewData) { // If fetch successful console.log("reviewData: ", reviewData); - //dbPromise.putReviews(reviewData); return reviewData; }) .catch(function(error) { console.log("In fetchReviewsBy...ID catch, error:", error.message); - // // Error handling - // console.log(`Error in fetch reviews by ID: ${error}, checking idb...`); - // if (idbReviews.length < 1) return null; - // return idbReviews; }); } @@ -232,7 +210,6 @@ class DBHelper { // Update all restaurant data dbPromise.then(function(db) { - //let restaurantStore = db.transaction("restaurantData", "readwrite").objectStore("restaurantData").get("-1") .then(function(value) { if(!value) { @@ -303,8 +280,7 @@ class DBHelper { .catch(function(error) { console.log("In addToUpdateQueue catch, error: ", error.message); }) - // TODO : attempt push of updates - //.then(DBHelper.pushUpdates()); + .then(DBHelper.pushUpdates()); } /* @@ -319,8 +295,6 @@ class DBHelper { // No updates, so get outta here! if(!cursor) return; let update = cursor.value.data; - - // check for bad records? See in testing let params = { body: JSON.stringify(update.body), method: update.method }; @@ -337,7 +311,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(); }) }) }) @@ -365,61 +339,6 @@ class DBHelper { console.log(`In saveReview - url: ${url}, method: ${method}, body: ${body}`); DBHelper.updateReviewCache(id, body); DBHelper.addToUpdateQueue(url, method, body); - //callback(null, null); - } - - // IDB Functionality ------ - - /* - * Function to check idb for restaurant information - * - static idbRestaurantRequest() { - return 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}`); }) } - /* - * 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); - }) - .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}`); }) - }*/ - } diff --git a/app/js/main.js b/app/js/main.js index ad6c77d..64c0666 100644 --- a/app/js/main.js +++ b/app/js/main.js @@ -192,19 +192,9 @@ favoriteClicked = (restaurant, button) => { let requestMethod = "PUT"; DBHelper.updateRestaurantCache(restaurant.id, {"is_favorite": !fav}); DBHelper.addToUpdateQueue(requestURL, requestMethod); - //return fetch(`${DBHelper.DATABASE_URL}/restaurants/${restaurant.id}/?is_favorite=${!fav}`, {method: 'PUT'}) - //.then(response => { - // if(!response.ok) return Promise.reject("Favorite could not be updated."); - // return response.json(); - //}).then(updatedRestaurant => { - // Update restaurant on idb - // dbPromise.putRestaurants(updatedRestaurant, true); - // Change state of toggle button - //console.log(`Exiting state: ${!fav}`); - button.setAttribute('aria-pressed', !fav); - button.innerHTML = !fav ? '♥' : '♡'; - button.onclick = event => favoriteClicked(restaurant, button); - //}); + button.setAttribute('aria-pressed', !fav); + button.innerHTML = !fav ? '♥' : '♡'; + button.onclick = event => favoriteClicked(restaurant, button); } /* diff --git a/app/js/restaurant_info.js b/app/js/restaurant_info.js index 4007b3c..ffa1380 100644 --- a/app/js/restaurant_info.js +++ b/app/js/restaurant_info.js @@ -18,7 +18,7 @@ window.initMap = () => { DBHelper.mapMarkerForRestaurant(self.restaurant, self.map); } }); - //DBHelper.pushUpdates(); + DBHelper.pushUpdates(); } /* @@ -84,8 +84,6 @@ fillRestaurantHTML = (restaurant = self.restaurant) => { if (restaurant.operating_hours) { fillRestaurantHoursHTML(); } - // fill reviews - //fillReviewsHTML(); console.log(`In fillRestaurantHTML, about to call fetch then fill with id: ${restaurant.id}`); DBHelper.fetchReviewsById(restaurant.id).then(fillReviewsHTML); } @@ -94,8 +92,6 @@ fillRestaurantHTML = (restaurant = self.restaurant) => { * Handling favorite button clicked. */ favoriteClicked = (restaurant, button) => { - //console.log(`Data: ${restaurant.name}, ${restaurant.is_favorite}, ${button}`); - //console.log(`favClicked. Entering state: ${button.getAttribute("aria-pressed")}`); // Get current fav state let fav = (button.getAttribute("aria-pressed") && button.getAttribute("aria-pressed") === "true") ? true : false; @@ -104,19 +100,9 @@ favoriteClicked = (restaurant, button) => { let requestMethod = "PUT"; DBHelper.updateRestaurantCache(restaurant.id, {"is_favorite": !fav}); DBHelper.addToUpdateQueue(requestURL, requestMethod); - //return fetch(`${DBHelper.DATABASE_URL}/${restaurant.id}/?is_favorite=${!fav}`, {method: 'PUT'}) - //.then(response => { - // if(!response.ok) return Promise.reject("Favorite could not be updated."); - // return response.json(); - //}).then(updatedRestaurant => { - // Update restaurant on idb - // dbPromise.putRestaurants(updatedRestaurant, true); - // Change state of toggle button - //console.log(`Exiting state: ${!fav}`); - button.setAttribute('aria-pressed', !fav); - button.innerHTML = !fav ? '♥' : '♡'; - button.onclick = event => favoriteClicked(restaurant, button); - //}); + button.setAttribute('aria-pressed', !fav); + button.innerHTML = !fav ? '♥' : '♡'; + button.onclick = event => favoriteClicked(restaurant, button); } /* @@ -191,7 +177,6 @@ createReviewHTML = (review) => { for (i = 0; i < review.rating; i++){ starString += "★ "; } - //console.log("starString ", starString); stars.innerHTML = starString; li.appendChild(stars); @@ -212,10 +197,8 @@ createReviewHTML = (review) => { fillBreadcrumb = (restaurant=self.restaurant) => { const breadcrumb = document.getElementById('breadcrumb'); const li = document.createElement('li'); - //const h2 = document.createElement('h2'); li.setAttribute('aria-current', 'page'); li.innerHTML = restaurant.name; - //li.appendChild(h2); breadcrumb.appendChild(li); } @@ -321,7 +304,6 @@ reviewForm = () => { handleSubmit = (e) => { // Takes care of submission cancelation e.preventDefault(); - //console.log("in handleSubmit"); let id = self.restaurant.id; let name = document.getElementById("name").value; let rating = document.getElementById("rating").value - 0; @@ -337,8 +319,8 @@ handleSubmit = (e) => { li.appendChild(newName); const date = document.createElement('p'); - //let now = Date.now(); - date.innerHTML = "Date";// TODO, sort this mess out : now.toLocaleDateString(); + let now = Date.now(); + date.innerHTML = now.toLocaleDateString(); li.appendChild(date); const newRating = document.createElement('p'); @@ -350,7 +332,6 @@ handleSubmit = (e) => { for (i = 0; i < rating; i++){ starString += "★ "; } - //console.log("starString ", starString); stars.innerHTML = starString; li.appendChild(stars); diff --git a/app/sw.js b/app/sw.js index be9418d..82948b9 100644 --- a/app/sw.js +++ b/app/sw.js @@ -3,9 +3,7 @@ */ // Include DBHelper functions for fetching restaurants and putting in indexedDB -// self.importScripts('js/dbhelper.js', 'js/idb.js'); - self.importScripts('js/idb.js'); -// import idb from "idb"; +self.importScripts('js/idb.js'); // Cache Information var myCache = 'restaurantReview_302'; @@ -21,15 +19,6 @@ var cacheFiles = [ 'js/idb.js' ]; -// IDB Information -//const dbPromise = idb.open('restaurantReviews', 3, upgradeDb => { -// switch(upgradeDb.oldVersion) { -// case 0: upgradeDb.createObjectStore('restaurantData', {keyPath: 'id'}); -// case 1: upgradeDb.createObjectStore('reviewData', {keypath: 'id'}).createIndex('restaurant_id', 'restaurant_id'); -// case 2: upgradeDb.createObjectStore('updateData', {keyPath: 'id', autoIncrement: true}); -// } -//}); - /* * Event Listener for install - caching the files */ @@ -143,24 +132,4 @@ function cacheFetch(event, request) { }) .catch(function(error) { console.log(`In cacheFetch catch, error: ${error.message}`); }); }) -} - -// /* -// * Event Listener for activate -// */ -// self.addEventListener('activate', event => { -// console.log('Event trigger - activate'); -// DBHelper.fetchRestaurants((error, restaurants) => { -// if (error) { -// callback(error, null); -// } else { -// const dbPromise = idb.open('restaurantReviews', 3, upgradeDb => { -// switch(upgradeDb.oldVersion) { -// case 0: upgradeDb.createObjectStore('restaurantData', {keyPath: 'id'}); -// case 1: upgradeDb.createObjectStore('reviewData', {keypath: 'id'}).createIndex('restaurant_id', 'restaurant_id'); -// case 2: upgradeDb.createObjectStore('updateData', {keyPath: 'id', autoIncrement: true}); -// } -// }); -// } -// }); -// }); +} \ No newline at end of file diff --git a/cert.png b/cert.png new file mode 100644 index 0000000..94e189f Binary files /dev/null and b/cert.png differ