Skip to content

Commit

Permalink
👌 Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rrjoson committed Oct 8, 2018
1 parent 005561a commit f3a9e94
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
38 changes: 38 additions & 0 deletions js/dbhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,42 @@ export default class DBHelper {
});
});
}

/**
* Update is_favorite on the restaurant
* @param restaurantId
* @param isFavorite
*/
static updateFavorite(restaurantId, isFavorite) {
const favorite = () => {
if (isFavorite) {
return false;
} else {
return true;
}
};

let restaurantObject = {
is_favorite: favorite()
};
const options = {
method: "PUT",
body: JSON.stringify(restaurantObject),
headers: new Headers({
"Content-Type": "application/json"
})
};

fetch(`${this.DATABASE_URL}/${restaurantId}`, options).then(() => {
const dbPromise = idb.open("restaurantsDB");
dbPromise.then(db => {
const tx = db.transaction("restaurants", "readwrite");
const store = tx.objectStore("restaurants");
store.get(restaurantId).then(restaurant => {
restaurant.is_favorite = isFavorite;
store.put(restaurant);
});
});
});
}
}
37 changes: 37 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ const createRestaurantHTML = restaurant => {
image.alt = restaurant.name;
li.append(image);

const favorite = document.createElement("button");

if (restaurant.is_favorite) {
favorite.innerHTML = "★";
favorite.setAttribute(
"aria-label",
"Remove " + restaurant.name + " from list of favorites."
);
} else {
favorite.innerHTML = "☆";
favorite.setAttribute(
"aria-label",
"Add " + restaurant.name + " to list of favorites."
);
}

favorite.onclick = () => {
if (restaurant.is_favorite) {
favorite.innerHTML = "☆";
favorite.setAttribute(
"aria-label",
"Add " + restaurant.name + " to list of favorites."
);
} else {
favorite.innerHTML = "★";
favorite.setAttribute(
"aria-label",
"Remove " + restaurant.name + " from list of favorites."
);
}

DBHelper.updateFavorite(restaurant.id, restaurant.is_favorite);
restaurant.is_favorite = !restaurant.is_favorite;
};

li.append(favorite);

const name = document.createElement("h3");
name.innerHTML = restaurant.name;
li.append(name);
Expand Down
15 changes: 13 additions & 2 deletions js/restaurant_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,19 @@ const getParameterByName = (name, url) => {
/**
* Submit and validate review form
*/
window.createReview = event => {
event.preventDefault();
window.createReview = () => {
// Access the form element...
var form = document.getElementById("review-form");

// ...and take over its submit event.
form.addEventListener("submit", function(event) {
event.preventDefault();

addReview();
});
};

const addReview = () => {
const author = document.getElementById("review-author").value;
const comment = document.getElementById("review-comment").value;
const rating = document.querySelector("#rating-values-list option:checked")
Expand Down

0 comments on commit f3a9e94

Please sign in to comment.