diff --git a/src/controllers/MealController.jsx b/src/controllers/MealController.jsx index 130ba68..9c60017 100644 --- a/src/controllers/MealController.jsx +++ b/src/controllers/MealController.jsx @@ -30,17 +30,10 @@ export const MealController = ({ meal, getMeal, getRandomMeal }) => { const [isFav, setIsFav] = useState(false); useEffect(() => { - // console.log(user.email); - // console.log(idMeal); - // console.log(isFav); - console.log(fb); - - // const add2Fav = async (user, idMeal, isFav) => { - if (isAuthenticated) { - fb.add(user.email, idMeal, isFav); + // Not update fav status of the placeholder recipe. TODO: it's ugly... + if (idMeal !== "52837" && isAuthenticated) { + fb.addToFavs(user.email, idMeal, isFav); } - // }; - // add2Fav(user, idMeal, isFav).then((data) => console.log(data)); }, [user, idMeal, isFav, fb, isAuthenticated]); const item = { diff --git a/src/services/Firebase/firebase.js b/src/services/Firebase/firebase.js index b369aeb..cbc6cc2 100644 --- a/src/services/Firebase/firebase.js +++ b/src/services/Firebase/firebase.js @@ -13,28 +13,22 @@ const CONFIG = { measurementId: config.measurementId, }; -// Firebase initializes the Application and provides method to interact with -// Firebase services as auth and firestore. +/** + * Firebase initializes the Application and provides method to interact with + * Firebase services as auth and firestore. + */ export default class Firebase { constructor() { app.initializeApp(CONFIG); this.db = app.firestore(); + this.collection = this.db.collection("mealPlannerUsers"); } - // this should put email, idMeal, strMeal and isFav - add = async (email, id, fav) => { - await this.db - .collection("mealPlannerUsers") - .add({ email: email, idMeal: id, isfav: fav }) - .then((ref) => { - console.log("Added document with ID: ", ref.id); - }) - .catch((error) => console.error("Error adding document: ", error)); - }; - // get infos for user 'email' + /** + * Get infos for user 'email'. + */ getByEmail = async (email) => { - const query = await this.db - .collection("mealPlannerUsers") + const query = await this.collection .where("email", "==", email) .limit(1) .get(); @@ -42,11 +36,9 @@ export default class Firebase { return query.docs[0].data(); }; + // get user's favourite recipes getFavsByEmail = async (email) => { - const query = await this.db - .collection("mealPlannerUsers") - .where("email", "==", email) - .get(); + const query = await this.collection.where("email", "==", email).get(); return query.docs[0].collection("favs").get(); // .limit(1) @@ -61,4 +53,22 @@ export default class Firebase { // console.log(favs); // return favs; }; + + /** + * Create or update favourite status for an authenticated user. + */ + addToFavs = async (email, idMeal, isFav) => { + this.collection + .doc(email) + .collection("favs") + .doc(idMeal) + .set({ + email, + idMeal, + isFav, + timestamp: this.db.FieldValue.serverTimestamp(), + }) + // .then(() => console.log("Fav object created.")) + .catch((err) => console.error("Error adding favs to database", err)); + }; }