mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
ProfilePage displays favourite recipes
This commit is contained in:
parent
93b76aecdb
commit
e664753691
6 changed files with 37 additions and 29 deletions
|
|
@ -1,12 +1,12 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
export const CardEntry = ({ item }) => {
|
export const CardEntry = ({ item, className = "col s12 m6" }) => {
|
||||||
const { idMeal, strMeal, strMealThumb } = item;
|
const { idMeal, strMeal, strMealThumb } = item;
|
||||||
return (
|
return (
|
||||||
<Link to={`${idMeal}`}>
|
<Link to={`${idMeal}`}>
|
||||||
<li>
|
<li>
|
||||||
<div className="col s12 m6">
|
<div className={className}>
|
||||||
<div className="card hoverable">
|
<div className="card hoverable">
|
||||||
<div className="card-image">
|
<div className="card-image">
|
||||||
<img src={strMealThumb} alt={strMeal} />
|
<img src={strMealThumb} alt={strMeal} />
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import { ContactPage } from "../pages/Contact";
|
||||||
import { NotFoundPage } from "../pages/NotFoundPage";
|
import { NotFoundPage } from "../pages/NotFoundPage";
|
||||||
|
|
||||||
import { PrivateRoute } from "../components/PrivateRoute";
|
import { PrivateRoute } from "../components/PrivateRoute";
|
||||||
|
import TestPage from "../pages/TestPage";
|
||||||
|
|
||||||
const MainRouter = ({
|
const MainRouter = ({
|
||||||
buttonUrl,
|
buttonUrl,
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,9 @@ export const MealController = ({ meal, getMeal, getRandomMeal }) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Not update fav status of the placeholder recipe. TODO: it's ugly...
|
// Not update fav status of the placeholder recipe. TODO: it's ugly...
|
||||||
if (idMeal !== "52837" && isAuthenticated) {
|
if (idMeal !== "52837" && isAuthenticated) {
|
||||||
fb.addToFavs(user.email, idMeal, isFav);
|
fb.addToFavs(user.email, idMeal, strMeal, strMealThumb, isFav);
|
||||||
}
|
}
|
||||||
}, [user, idMeal, isFav, fb, isAuthenticated]);
|
}, [user, idMeal, strMeal, strMealThumb, isFav, fb, isAuthenticated]);
|
||||||
|
|
||||||
const item = {
|
const item = {
|
||||||
mealName: strMeal,
|
mealName: strMeal,
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,13 @@ import { useFirebase } from "../services/Firebase";
|
||||||
|
|
||||||
export const ProfileController = () => {
|
export const ProfileController = () => {
|
||||||
const { loading, user } = useAuth0();
|
const { loading, user } = useAuth0();
|
||||||
const [data, setData] = useState();
|
const [favs, setFavs] = useState([]);
|
||||||
const db = useFirebase();
|
const db = useFirebase();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
db.getByEmail(user.email).then((res) => setData(res));
|
db.getByEmail(user.email).then((res) => {
|
||||||
// db.getFavsByEmail(user.email).then((res) => setData(res));
|
setFavs(res.favs);
|
||||||
|
});
|
||||||
}, [db, user.email]);
|
}, [db, user.email]);
|
||||||
|
|
||||||
return loading || !user ? ( // is catched by PrivateRoute
|
return loading || !user ? ( // is catched by PrivateRoute
|
||||||
|
|
@ -19,6 +20,6 @@ export const ProfileController = () => {
|
||||||
<PreLoader />
|
<PreLoader />
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<ProfilePage user={user} data={data} />
|
<ProfilePage user={user} data={favs} />
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { CardEntry } from "../components/CardEntry";
|
||||||
|
|
||||||
export const ProfilePage = ({ user, data }) => {
|
export const ProfilePage = ({ user, data }) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -12,12 +13,12 @@ export const ProfilePage = ({ user, data }) => {
|
||||||
/>
|
/>
|
||||||
<h2 className="col s9">{user.name}</h2>
|
<h2 className="col s9">{user.name}</h2>
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<div className="row">
|
||||||
<b>Email: </b>
|
<b>Email: </b>
|
||||||
{user.email}
|
{user.email}
|
||||||
<br />
|
<br />
|
||||||
{JSON.stringify(data, null, 2)}
|
<ul>{data && data.map((d, i) => <CardEntry key={i} item={d} />)}</ul>
|
||||||
</p>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ const CONFIG = {
|
||||||
measurementId: config.measurementId,
|
measurementId: config.measurementId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const FAVS = "favs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Firebase initializes the Application and provides method to interact with
|
* Firebase initializes the Application and provides method to interact with
|
||||||
* Firebase services as auth and firestore.
|
* Firebase services as auth and firestore.
|
||||||
|
|
@ -28,45 +30,48 @@ export default class Firebase {
|
||||||
* Get infos for user 'email'.
|
* Get infos for user 'email'.
|
||||||
*/
|
*/
|
||||||
getByEmail = async (email) => {
|
getByEmail = async (email) => {
|
||||||
const query = await this.collection
|
const infos = await this.collection
|
||||||
.where("email", "==", email)
|
.where("email", "==", email)
|
||||||
.limit(1)
|
.limit(1)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
return query.docs[0].data();
|
const favs = await this.getFavsByEmail(email);
|
||||||
|
|
||||||
|
return { infos, favs };
|
||||||
};
|
};
|
||||||
|
|
||||||
// get user's favourite recipes
|
/**
|
||||||
|
* Get user's favourite recipes
|
||||||
|
* */
|
||||||
getFavsByEmail = async (email) => {
|
getFavsByEmail = async (email) => {
|
||||||
const query = await this.collection.where("email", "==", email).get();
|
let favs = [];
|
||||||
return query.docs[0].collection("favs").get();
|
const query = await this.collection
|
||||||
|
.doc(email)
|
||||||
|
.collection(FAVS)
|
||||||
|
// .orderBy("timestamp", "desc")
|
||||||
|
.limit(10)
|
||||||
|
.get();
|
||||||
|
|
||||||
// .limit(1)
|
query.forEach((doc) => favs.push(doc.data()));
|
||||||
// .get();
|
|
||||||
|
|
||||||
// return query.docs[0].data();
|
return favs;
|
||||||
// const user = await this.getByEmail(email);
|
|
||||||
// const query = user.collection("favs").get();
|
|
||||||
|
|
||||||
// const favs = [];
|
|
||||||
// query.docs.forEach((doc) => favs.push(doc.data()));
|
|
||||||
// console.log(favs);
|
|
||||||
// return favs;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create or update favourite status for an authenticated user.
|
* Create or update favourite status for an authenticated user.
|
||||||
*/
|
*/
|
||||||
addToFavs = async (email, idMeal, isFav) => {
|
addToFavs = async (email, idMeal, strMeal, strMealThumb, isFav) => {
|
||||||
this.collection
|
this.collection
|
||||||
.doc(email)
|
.doc(email)
|
||||||
.collection("favs")
|
.collection(FAVS)
|
||||||
.doc(idMeal)
|
.doc(idMeal)
|
||||||
.set({
|
.set({
|
||||||
email,
|
email,
|
||||||
idMeal,
|
idMeal,
|
||||||
|
strMeal,
|
||||||
|
strMealThumb,
|
||||||
isFav,
|
isFav,
|
||||||
timestamp: this.db.FieldValue.serverTimestamp(),
|
// timestamp: app.FieldValue.serverTimestamp(),
|
||||||
})
|
})
|
||||||
// .then(() => console.log("Fav object created."))
|
// .then(() => console.log("Fav object created."))
|
||||||
.catch((err) => console.error("Error adding favs to database", err));
|
.catch((err) => console.error("Error adding favs to database", err));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue