fonction des etoiles
This commit is contained in:
@@ -39,7 +39,7 @@
|
||||
<h3>Résultats ({searchs.pagination.items.total} résultats trouvés)</h3>
|
||||
<div class="results-grid">
|
||||
<div class="card" each="{item in searchs.items}">
|
||||
<a if={authUser} onclick={() => addFavoris(item.id)}><i class={item.isFavorite ? "fa-solid fa-star" : "fa-regular fa-star"}></i></a>
|
||||
<a if={authUser} onclick={() => item.isFavorite ? this.removeFavoris(item.id) : this.addFavoris(item.id)}><button><i class={item.isFavorite ? "fa-solid fa-star" : "fa-regular fa-star"}></i></button></a>
|
||||
<a href={ "#/release-details/" + item.id}>
|
||||
{item.title}
|
||||
<img src="{item.cover_image} " alt="cover " />
|
||||
@@ -105,6 +105,7 @@
|
||||
|
||||
changePage(page) {
|
||||
this.search(null, page);
|
||||
this.update();
|
||||
},
|
||||
|
||||
async onMounted() {
|
||||
@@ -116,10 +117,26 @@
|
||||
|
||||
addFavoris(id) {
|
||||
window.favorite(id)
|
||||
this.searchs.items = this.searchs.items.map(item =>
|
||||
item.id === id ? { ...item, isFavorite: true } : item
|
||||
);
|
||||
|
||||
this.update();
|
||||
},
|
||||
|
||||
async removeFavoris(id) {
|
||||
await window.removeFavorite(id);
|
||||
|
||||
this.searchs.items = this.searchs.items.map(item =>
|
||||
item.id === id ? { ...item, isFavorite: false } : item
|
||||
);
|
||||
|
||||
this.update();
|
||||
},
|
||||
|
||||
Logout(){
|
||||
window.logout()
|
||||
this.update();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -45,7 +45,7 @@ export async function login(email, password) {
|
||||
export async function sign(email, password) {
|
||||
const userCred = await createUserWithEmailAndPassword(auth, email, password);
|
||||
await adduserdata(userCred.user.email, userCred.user.uid);
|
||||
localStorage.setItem("uid", userC.user.uid);
|
||||
localStorage.setItem("uid", userCred.user.uid);
|
||||
return userCred;
|
||||
}
|
||||
|
||||
@@ -68,9 +68,6 @@ window.observeAuthState = function(callback) {
|
||||
export async function favorite(releaseId) {
|
||||
const storedUid = localStorage.getItem("uid");
|
||||
|
||||
|
||||
|
||||
|
||||
const usersRef = collection(db, "users");
|
||||
const q = query(usersRef, where("uid", "==", storedUid));
|
||||
const querySnapshot = await getDocs(q);
|
||||
@@ -78,7 +75,6 @@ export async function favorite(releaseId) {
|
||||
|
||||
const userDoc = querySnapshot.docs[0];
|
||||
const userDocId = userDoc.id;
|
||||
console.log(userDoc);
|
||||
|
||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||
|
||||
@@ -118,7 +114,6 @@ export async function isFavorite(releaseId) {
|
||||
const storedUid = localStorage.getItem("uid");
|
||||
if (!storedUid) return false;
|
||||
|
||||
// Recherche l'utilisateur avec le bon uid
|
||||
const usersRef = collection(db, "users");
|
||||
const userQuery = query(usersRef, where("uid", "==", storedUid));
|
||||
const userSnapshot = await getDocs(userQuery);
|
||||
@@ -128,12 +123,31 @@ export async function isFavorite(releaseId) {
|
||||
const userDoc = userSnapshot.docs[0];
|
||||
const userDocId = userDoc.id;
|
||||
|
||||
// Recherche si le releaseId existe déjà dans les favoris
|
||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId));
|
||||
const favSnapshot = await getDocs(favQuery);
|
||||
|
||||
return !favSnapshot.empty; // true = en favori, false = pas en favori
|
||||
return !favSnapshot.empty;
|
||||
}
|
||||
|
||||
export async function removeFavorite(releaseId) {
|
||||
const storedUid = localStorage.getItem("uid");
|
||||
|
||||
const usersRef = collection(db, "users");
|
||||
const userQuery = query(usersRef, where("uid", "==", storedUid));
|
||||
const userSnapshot = await getDocs(userQuery);
|
||||
|
||||
const userDoc = userSnapshot.docs[0];
|
||||
const userDocId = userDoc.id;
|
||||
|
||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId));
|
||||
const favSnapshot = await getDocs(favQuery);
|
||||
|
||||
const favDoc = favSnapshot.docs[0];
|
||||
await setDoc(doc(favoritesRef, favDoc.id), {}, { merge: false });
|
||||
|
||||
console.log(`Le favori avec releaseId ${releaseId} a été supprimé.`);
|
||||
}
|
||||
|
||||
window.login = login;
|
||||
@@ -141,4 +155,5 @@ window.sign = sign;
|
||||
window.logout = logout;
|
||||
window.favorite = favorite;
|
||||
window.getFavorites = getFavorites;
|
||||
window.isFavorite = isFavorite;
|
||||
window.isFavorite = isFavorite;
|
||||
window.removeFavorite = removeFavorite;
|
||||
Reference in New Issue
Block a user