Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions SearchPage/SearchScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ 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("");
const [listings, setListings] = useState([]);
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();
Expand Down Expand Up @@ -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 (
<ListCard
key={id}
Expand Down
126 changes: 56 additions & 70 deletions SearchPage/components/FavouriteButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ const currUser = firebase.auth().currentUser;

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 FavouriteButton = ({ id, isFavourite }) => {
const [favourite, setFavourite] = useState(isFavourite);
Expand All @@ -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()
Expand All @@ -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 (
<TouchableOpacity onPress={handlePress}>
Expand Down
40 changes: 20 additions & 20 deletions SearchPage/components/ListCardInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,7 +53,7 @@ export const ListCardInfo = (information) => {
<Text>{location}</Text>
<View style={{ flexDirection: "row", justifyContent: "space-between" }}>
<Text>{`$${price}/day`}</Text>
<FavouriteButton id={id} isFavourite={isListingWishlisted} />
<FavouriteButton id={id} isFavourite={currWishList.includes(id)} />
</View>
</View>
);
Expand Down
95 changes: 28 additions & 67 deletions WishlistPage/WishlistScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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 (
<SafeAreaView style={styles.container}>
<FlatList
Expand Down