get favs data from firebase to profile page

This commit is contained in:
Ruidy Nemausat 2020-04-15 17:22:48 +02:00
parent 6132ed34ca
commit 29087ca71f
7 changed files with 39 additions and 26 deletions

View file

@ -14,7 +14,6 @@ import MainRouter from "./controllers/MainRouter";
export const App = () => { export const App = () => {
const { loading } = useAuth0(); const { loading } = useAuth0();
const [searchString, setSearchString] = useState(""); const [searchString, setSearchString] = useState("");
const [searchResults, setSearchResults] = useState({ meals: [] }); const [searchResults, setSearchResults] = useState({ meals: [] });
// Default meal object. TODO: Find a better alternative // Default meal object. TODO: Find a better alternative
const mealDef = { const mealDef = {
@ -93,8 +92,8 @@ export const App = () => {
: getData(searchString, setSearchResults, "search"); : getData(searchString, setSearchResults, "search");
}; };
const handleChange = (ev) => { const handleChange = (e) => {
const { value } = ev.target; const { value } = e.target;
setSearchString(value); setSearchString(value);
}; };

View file

@ -8,8 +8,8 @@ export const ContactForm = ({ setIsSubmitted }) => {
const [phone, setPhone] = useState(""); const [phone, setPhone] = useState("");
const [message, setMessage] = useState(""); const [message, setMessage] = useState("");
const handleSubmit = (ev) => { const handleSubmit = (e) => {
ev.preventDefault(); e.preventDefault();
// confirmationMail(email); // confirmationMail(email);
// const body = `Sender: ${firstName} ${lastName}\nPhone: ${phone}\nMessage: ${message}`; // const body = `Sender: ${firstName} ${lastName}\nPhone: ${phone}\nMessage: ${message}`;
// notificationMail(email, `New message from ${firstName} ${lastName}`, body); // notificationMail(email, `New message from ${firstName} ${lastName}`, body);

View file

@ -1,18 +1,23 @@
import React from "react"; import React, { useEffect, useState } from "react";
import { useAuth0 } from "../utils/auth0-spa"; import { useAuth0 } from "../utils/auth0-spa";
import { PreLoader } from "../components/PreLoader"; import { PreLoader } from "../components/PreLoader";
import { ProfilePage } from "../pages/ProfilePage"; import { ProfilePage } from "../pages/ProfilePage";
import { useFirebase } from "../services/Firebase";
export const ProfileController = () => { export const ProfileController = () => {
const { loading, user } = useAuth0(); const { loading, user } = useAuth0();
const [data, setData] = useState();
const db = useFirebase();
// const fn = async () => await fire.getByEmail(user.email); useEffect(() => {
db.get(user.email).then((res) => setData(res));
}, [db, user.email]);
return loading || !user ? ( // is catched by PrivateRoute return loading || !user ? ( // is catched by PrivateRoute
<div className="container center-align"> <div className="container center-align">
<PreLoader /> <PreLoader />
</div> </div>
) : ( ) : (
<ProfilePage user={user} /> <ProfilePage user={user} data={data} />
); );
}; };

View file

@ -1,5 +1,4 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { Navbar } from "../components/Navbar"; import { Navbar } from "../components/Navbar";
import { SearchBar } from "../components/SearchBar"; import { SearchBar } from "../components/SearchBar";
import { Footer } from "../components/Footer"; import { Footer } from "../components/Footer";
@ -19,26 +18,26 @@ const MainLayout = ({
const links = ["categories", "contact"]; const links = ["categories", "contact"];
const openNavClick = (ev) => { const openNavClick = (e) => {
ev.preventDefault(); e.preventDefault();
setShowNav(true); setShowNav(true);
document.addEventListener("keydown", handleEscKey); document.addEventListener("keydown", handleEscKey);
// document.addEventListener("click", handleOutsideClick); // document.addEventListener("click", handleOutsideClick);
}; };
const closeNavClick = (ev) => { const closeNavClick = (e) => {
ev.preventDefault(); e.preventDefault();
setShowNav(false); setShowNav(false);
document.removeEventListener("keydown", handleEscKey); document.removeEventListener("keydown", handleEscKey);
}; };
const handleEscKey = (ev) => { const handleEscKey = (e) => {
if (ev.key === "Escape") { if (e.key === "Escape") {
setShowNav(false); setShowNav(false);
} }
}; };
// const handleOutsideClick = ev => { // const handleOutsideClick = e => {
// console.log(ev); // console.log(e);
// }; // };
return ( return (

View file

@ -1,6 +1,6 @@
import React from "react"; import React from "react";
export const ProfilePage = ({ user }) => { export const ProfilePage = ({ user, data }) => {
return ( return (
<div className="container"> <div className="container">
<div className="row"> <div className="row">
@ -14,9 +14,8 @@ export const ProfilePage = ({ user }) => {
<p> <p>
<b>Email: </b> <b>Email: </b>
{user.email} {user.email}
{/* <a href={`mailto:${user.email}`}>{user.email}</a> */} {JSON.stringify(data, null, 2)}
</p> </p>
{/* <code>{JSON.stringify(user, null, 2)}</code> */}
</div> </div>
</div> </div>
); );

View file

@ -20,7 +20,7 @@ export default class Firebase {
app.initializeApp(CONFIG); app.initializeApp(CONFIG);
this.db = app.firestore(); this.db = app.firestore();
} }
// this should put email, idMeal, strMeal and isFav
add = async (email, id, fav) => { add = async (email, id, fav) => {
await this.db await this.db
.collection("MealPlannerFavs") .collection("MealPlannerFavs")
@ -39,4 +39,15 @@ export default class Firebase {
const snapshot = query.docs[0]; const snapshot = query.docs[0];
return snapshot.data(); return snapshot.data();
}; };
// get all favs for user 'email'
get = async (email) => {
const query = await this.db
.collection("mealPlannerUsers")
.where("email", "==", email)
.get();
// .then((doc) => doc.data());
const snapshot = query.docs[0];
return snapshot.data();
};
} }

View file

@ -1,6 +1,6 @@
import { useState } from "react"; import { useState } from "react";
export const useInput = initialValue => { export const useInput = (initialValue) => {
const [value, setValue] = useState(initialValue); const [value, setValue] = useState(initialValue);
return { return {
@ -9,9 +9,9 @@ export const useInput = initialValue => {
reset: () => setValue(""), reset: () => setValue(""),
bind: { bind: {
value, value,
onChange: ev => { onChange: (e) => {
setValue(ev.target.value); setValue(e.target.value);
} },
} },
}; };
}; };