From d9bd30c512144db8ccb7596137ee1c6391e9cbd2 Mon Sep 17 00:00:00 2001 From: kimberly ong Date: Thu, 17 Jun 2021 04:55:11 +0800 Subject: [PATCH] Reset to the previous head --- SearchPage/SearchScreen.js | 13 +-- SearchPage/components/FavouriteButton.js | 126 ++++++++++------------- SearchPage/components/ListCardInfo.js | 40 +++---- WishlistPage/WishlistScreen.js | 95 +++++------------ 4 files changed, 109 insertions(+), 165 deletions(-) diff --git a/SearchPage/SearchScreen.js b/SearchPage/SearchScreen.js index 14184fd..11c06d1 100644 --- a/SearchPage/SearchScreen.js +++ b/SearchPage/SearchScreen.js @@ -13,7 +13,6 @@ if (!firebase.apps.length) { const db = firebase.firestore().collection("sample-listings"); const dbSampleUsers = firebase.firestore().collection("sample-users"); -const dbWishlists = firebase.firestore().collection("sample-wishlists"); export const SearchScreen = (props) => { const [searchInput, setSearchInput] = useState(""); @@ -21,14 +20,14 @@ export const SearchScreen = (props) => { const [wishlists, setWishlists] = useState([]); useEffect(() => { - const unsubscribe = dbWishlists.onSnapshot((collection) => { - const updatedWishlist = collection.docs.map((doc) => { + const unsubscribe = dbSampleUsers.onSnapshot((collection) => { + const updatedUsers = collection.docs.map((doc) => { return { id: doc.id, ...doc.data(), }; }); - setWishlists(updatedWishlist); + setWishlists(updatedUsers[0].favourites); }); return () => { unsubscribe(); @@ -65,10 +64,8 @@ export const SearchScreen = (props) => { function renderItem({ item }) { const { id } = item; // Checks if user has wishlisted listing - const isListingWishlisted = wishlists.some( - (wishlist) => wishlist.id === id - ); - + const isListingWishlisted = wishlists.some((wishlist) => wishlist === id); + return ( { -// if (snapshot.exists()) { -// console.log(snapshot.val()); -// currWishList = snapshot.val(); -// } else { -// console.log("No data available"); -// } -// }) -// .catch((error) => { -// console.error(error); -// }); -// } +var currWishList = []; +if (currUser) { + dbRef + .child("users") + .child(currUser.uid) + .child("wishlist") + .get() + .then((snapshot) => { + if (snapshot.exists()) { + console.log(snapshot.val()); + currWishList = snapshot.val(); + } else { + console.log("No data available"); + } + }) + .catch((error) => { + console.error(error); + }); +} export const FavouriteButton = ({ id, isFavourite }) => { const [favourite, setFavourite] = useState(isFavourite); @@ -35,6 +35,23 @@ export const FavouriteButton = ({ id, isFavourite }) => { if (!favourite) { firebase.firestore().collection("sample-wishlists").add({ id: id }); + currWishList.push(id); + console.log("liked"); + if (currUser) { + firebase + .database() + .ref("users/" + currUser.uid) + .set({ + gmail: currUser.email, + profile_picture: currUser.photoURL, + username: currUser.displayName, + uid: currUser.uid, + wishlist: currWishList, + }) + .catch(function (e) { + console.log("upload data to firebase failed: " + e); + }); + } } else { firebase .firestore() @@ -44,59 +61,28 @@ export const FavouriteButton = ({ id, isFavourite }) => { .then((querySnapshot) => { querySnapshot.forEach((doc) => doc.ref.delete()); }); - } - }; - - // const handlePress = () => { - // setFavourite((favourite) => !favourite); - - // if (!favourite) { - // firebase.firestore().collection("sample-wishlists").add({ id: id }); - // currWishList.push(id); - // console.log("liked") - // if (currUser) { - // firebase.database() - // .ref("users/" + currUser.uid) - // .set({ - // gmail: currUser.email, - // profile_picture: currUser.photoURL, - // username: currUser.displayName, - // uid: currUser.uid, - // wishlist: currWishList - // }).catch(function(e) { - // console.log("upload data to firebase failed: " + e); - // }) - // } - // } else { - // firebase - // .firestore() - // .collection("sample-wishlists") - // .where("id", "==", id) - // .get() - // .then((querySnapshot) => { - // querySnapshot.forEach((doc) => doc.ref.delete()); - // }); + const index = currWishList.indexOf(id); + currWishList.splice(index, 1); - // const index = currWishList.indexOf(id) - // currWishList.splice(index, 1); - - // console.log("unliked list is " + currWishList) - // if (currUser) { - // firebase.database() - // .ref("users/" + currUser.uid) - // .set({ - // gmail: currUser.email, - // profile_picture: currUser.photoURL, - // username: currUser.displayName, - // uid: currUser.uid, - // wishlist: currWishList - // }).catch(function(e) { - // console.log("upload data to firebase failed: " + e); - // }) - // } - // } - // }; + console.log("unliked list is " + currWishList); + if (currUser) { + firebase + .database() + .ref("users/" + currUser.uid) + .set({ + gmail: currUser.email, + profile_picture: currUser.photoURL, + username: currUser.displayName, + uid: currUser.uid, + wishlist: currWishList, + }) + .catch(function (e) { + console.log("upload data to firebase failed: " + e); + }); + } + } + }; return ( diff --git a/SearchPage/components/ListCardInfo.js b/SearchPage/components/ListCardInfo.js index 4b15787..1d92684 100644 --- a/SearchPage/components/ListCardInfo.js +++ b/SearchPage/components/ListCardInfo.js @@ -13,25 +13,25 @@ console.log("current user is " + currUser); const dbRef = firebase.database().ref(); -// var currWishList = []; -// if (currUser) { -// dbRef -// .child("users") -// .child(currUser.uid) -// .child("wishlist") -// .get() -// .then((snapshot) => { -// if (snapshot.exists()) { -// console.log(snapshot.val()); -// currWishList = snapshot.val(); -// } else { -// console.log("No data available"); -// } -// }) -// .catch((error) => { -// console.error(error); -// }); -// } +var currWishList = []; +if (currUser) { + dbRef + .child("users") + .child(currUser.uid) + .child("wishlist") + .get() + .then((snapshot) => { + if (snapshot.exists()) { + console.log(snapshot.val()); + currWishList = snapshot.val(); + } else { + console.log("No data available"); + } + }) + .catch((error) => { + console.error(error); + }); +} export const ListCardInfo = (information) => { const { @@ -53,7 +53,7 @@ export const ListCardInfo = (information) => { {location} {`$${price}/day`} - + ); diff --git a/WishlistPage/WishlistScreen.js b/WishlistPage/WishlistScreen.js index 5a226d0..5c6fe0f 100644 --- a/WishlistPage/WishlistScreen.js +++ b/WishlistPage/WishlistScreen.js @@ -11,16 +11,13 @@ if (!firebase.apps.length) { firebase.app(); // if already initialized, use that one } -// const dbListings = firebase.firestore().collection("sample-listings"); -// const dbSampleUsers = firebase.firestore().collection("sample-users"); - -// const currUser = firebase.auth().currentUser; -// console.log("current user is " + currUser); +const dbListings = firebase.firestore().collection("sample-listings"); +const dbSampleUsers = firebase.firestore().collection("sample-users"); -// const dbRef = firebase.database().ref(); +const currUser = firebase.auth().currentUser; +console.log("current user is " + currUser); -const dbListings = firebase.firestore().collection("sample-listings"); -const dbWishlists = firebase.firestore().collection("sample-wishlists"); +const dbRef = firebase.database().ref(); export const WishlistScreen = ({ navigation }) => { const [listings, setListings] = useState([]); @@ -39,82 +36,46 @@ export const WishlistScreen = ({ navigation }) => { return () => { unsubscribe(); }; - }, []); + }); useEffect(() => { - const unsubscribe = dbWishlists.onSnapshot((collection) => { - const updatedWishlist = collection.docs.map((doc) => { + /* + const unsubscribe = dbSampleUsers.onSnapshot((collection) => { + const updatedUsers = collection.docs.map((doc) => { return { id: doc.id, ...doc.data(), }; }); - setWishlists(updatedWishlist); + + setWishlists(updatedUsers[0].favourites); }); return () => { unsubscribe(); }; - }, []); + */ + const unsubscribe = dbRef + .child("users") + .child(currUser.uid) + .child("wishlist") + .get() + .then((snapshot) => { + if (snapshot.exists()) { + console.log(snapshot.val()); + setWishlists(snapshot.val()); + } + }); + return () => { + unsubscribe; + }; + }); function renderData() { return listings.filter((listing) => - wishlists.some((wishlist) => wishlist.id === listing.id) + wishlists.some((wishlist) => wishlist === listing.id) ); } - // useEffect(() => { - // const unsubscribe = dbListings.onSnapshot((collection) => { - // const updatedListings = collection.docs.map((doc) => { - // return { - // id: doc.id, - // ...doc.data(), - // }; - // }); - // setListings(updatedListings); - // }); - // return () => { - // unsubscribe(); - // }; - // }); - - // useEffect(() => { - - // const unsubscribe = dbSampleUsers.onSnapshot((collection) => { - // const updatedUsers = collection.docs.map((doc) => { - // return { - // id: doc.id, - // ...doc.data(), - // }; - // }); - - // setWishlists(updatedUsers[0].favourites); - // }); - // return () => { - // unsubscribe(); - // }; - - // // const unsubscribe = dbRef - // // .child("users") - // // .child(currUser.uid) - // // .child("wishlist") - // // .get() - // // .then((snapshot) => { - // // if (snapshot.exists()) { - // // console.log(snapshot.val()); - // // setWishlists(snapshot.val()); - // // } - // // }); - // // return () => { - // // unsubscribe; - // // }; - // }); - - // function renderData() { - // return listings.filter((listing) => - // wishlists.some((wishlist) => wishlist === listing.id) - // ); - // } - return (