Skip to content

Commit

Permalink
Code clean-up!✨ Added Certification of Completion! Woot!🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Kate Lovett committed Nov 9, 2018
1 parent 0f71ff6 commit 01b89f5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 158 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand Down
85 changes: 2 additions & 83 deletions app/js/dbhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// });
});
}

Expand Down Expand Up @@ -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;
});
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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());
}

/*
Expand All @@ -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 };

Expand All @@ -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();
})
})
})
Expand Down Expand Up @@ -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}`); })
}*/

}
16 changes: 3 additions & 13 deletions app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '&#9829;' : '&#9825;';
button.onclick = event => favoriteClicked(restaurant, button);
//});
button.setAttribute('aria-pressed', !fav);
button.innerHTML = !fav ? '&#9829;' : '&#9825;';
button.onclick = event => favoriteClicked(restaurant, button);
}

/*
Expand Down
31 changes: 6 additions & 25 deletions 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 Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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 ? '&#9829;' : '&#9825;';
button.onclick = event => favoriteClicked(restaurant, button);
//});
button.setAttribute('aria-pressed', !fav);
button.innerHTML = !fav ? '&#9829;' : '&#9825;';
button.onclick = event => favoriteClicked(restaurant, button);
}

/*
Expand Down Expand Up @@ -191,7 +177,6 @@ createReviewHTML = (review) => {
for (i = 0; i < review.rating; i++){
starString += "&#9733 ";
}
//console.log("starString ", starString);
stars.innerHTML = starString;
li.appendChild(stars);

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -350,7 +332,6 @@ handleSubmit = (e) => {
for (i = 0; i < rating; i++){
starString += "&#9733 ";
}
//console.log("starString ", starString);
stars.innerHTML = starString;
li.appendChild(stars);

Expand Down
35 changes: 2 additions & 33 deletions app/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
*/
Expand Down Expand Up @@ -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});
// }
// });
// }
// });
// });
}
Binary file added cert.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01b89f5

Please sign in to comment.