From f3631468b34e91b0d02267615c7aa259d8131c7b Mon Sep 17 00:00:00 2001 From: Jan Silva Date: Mon, 5 Dec 2022 15:14:57 +0100 Subject: [PATCH] Export getFavorites to favorites Service --- src/pages/Home/Home.css | 6 +++--- src/pages/Home/Home.js | 28 ++++++++-------------------- src/services/favorites.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 src/services/favorites.js diff --git a/src/pages/Home/Home.css b/src/pages/Home/Home.css index d0b4ebe..3c9c97e 100644 --- a/src/pages/Home/Home.css +++ b/src/pages/Home/Home.css @@ -11,8 +11,8 @@ border-radius: 12px; } -.profile p { - font-size: 2rem; +.profile p, .avatar p { + font-size: 1.5rem; font-family: var(--font-raleway); font-weight: 500; color: #a1a1a1; @@ -59,7 +59,7 @@ height: 0; position: relative; } -.avatar-img img{ +.avatar-img input,.avatar-img img{ width:100%; height:100%; position: absolute; diff --git a/src/pages/Home/Home.js b/src/pages/Home/Home.js index fcf548f..5d4a4a1 100644 --- a/src/pages/Home/Home.js +++ b/src/pages/Home/Home.js @@ -7,13 +7,11 @@ import Loader from 'components/Loader/Loader.jsx'; import { useSession } from "providers/Session"; import {useState, useEffect} from 'react'; import {useNavigate} from "react-router-dom"; +import {getFavorites} from "services/favorites.js"; /** remove me **/ -import firebase from "firebase/compat/app"; -import "firebase/compat/firestore"; const Home = () => { - const db = firebase.firestore(); const { user, loginMethod, logoutMethod } = useSession(); const [state, setState] = useState({ loading: true }); const [favorites, setFavorites] = useState([]); @@ -36,25 +34,15 @@ const Home = () => { navigate('/add'); } - const getFavorites = async () => { - db.collection("users") - .doc(user.uid) - .get() - .then((doc) => { - if (doc.exists) { - setFavorites(doc.data()['favorites']); - console.log("< GET FAVORITES > ", doc.data()); - } - }) - .catch((e) => { - console.warn("< GET FAVORITES : ERROR > ", e); - }); - }; - useEffect(() => { - getFavorites().then(() => { + if (user.displayName !== null) { + getFavorites(user).then((movies) => { + setFavorites(movies); + setState({ ...state, loading: false}); + }) + } else { setState({ ...state, loading: false}); - }) + } }, [user]); diff --git a/src/services/favorites.js b/src/services/favorites.js new file mode 100644 index 0000000..4b6762c --- /dev/null +++ b/src/services/favorites.js @@ -0,0 +1,37 @@ +import firebase from "firebase/compat/app"; +import "firebase/compat/firestore"; + +const getFavorites = (user) => + new Promise( async (resolve) => { + const db = firebase.firestore(); + db.collection("users") + .doc(user.uid) + .get() + .then((doc) => { + if (doc.exists) { + let favorites = doc.data()['favorites'] + resolve(favorites); + console.log("< GET FAVORITES > ", favorites); + } + }) + .catch((e) => { + console.warn("< GET FAVORITES : ERROR > ", e); + }); + }); + +const addFavorites = (user, favorites) => + new Promise( async (resolve) => { + const db = firebase.firestore(); + db.collection("users") + .doc(user.uid) + .set({ + favorites: favorites + }) + .then((response) => { + console.log("< ADD FAVORITES : DONE > ", favorites); + resolve(favorites); + }) + .catch((e) => console.warn("< ADD FAVORITES : ERROR > ", e)); + }); + +export {getFavorites, addFavorites};