Skip to content

Commit

Permalink
Export getFavorites to favorites Service
Browse files Browse the repository at this point in the history
  • Loading branch information
janjitsu committed Dec 5, 2022
1 parent 3fe31d4 commit f363146
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/pages/Home/Home.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +59,7 @@
height: 0;
position: relative;
}
.avatar-img img{
.avatar-img input,.avatar-img img{
width:100%;
height:100%;
position: absolute;
Expand Down
28 changes: 8 additions & 20 deletions src/pages/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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]);


Expand Down
37 changes: 37 additions & 0 deletions src/services/favorites.js
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit f363146

Please sign in to comment.