probleme
This commit is contained in:
@@ -40,7 +40,8 @@
|
|||||||
<div class="results-grid">
|
<div class="results-grid">
|
||||||
<div class="card" each="{item in searchs.items}">
|
<div class="card" each="{item in searchs.items}">
|
||||||
<!-- Afficher l'étoile avec la musique immédiatement -->
|
<!-- Afficher l'étoile avec la musique immédiatement -->
|
||||||
<a if={authUser && this.searchs.type !== "artist"} onclick={() => item.isFavorite ? this.removeFavoris(item.id) : this.addFavoris(item.id)}>
|
{console.log(item.isFavorite)}
|
||||||
|
<a if={authUser && this.searchs.type !== "artist"} onclick={() => item.isFavorite ? this.removeFavoris(item.id, item.type) : this.addFavoris(item.id, item.type)}>
|
||||||
<button><i class={item.isFavorite ? "fa-solid fa-star" : "fa-regular fa-star"}></i></button>
|
<button><i class={item.isFavorite ? "fa-solid fa-star" : "fa-regular fa-star"}></i></button>
|
||||||
</a>
|
</a>
|
||||||
<a href={ "#/release-details/" + item.type + "/" + item.id}>
|
<a href={ "#/release-details/" + item.type + "/" + item.id}>
|
||||||
@@ -93,7 +94,7 @@
|
|||||||
const result = await window.discogsearch(query, type, page);
|
const result = await window.discogsearch(query, type, page);
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
const favoritePromises = result.results.map(item => window.isFavorite(item.id));
|
const favoritePromises = result.results.map(item => window.isFavorite(item.id, item.type));
|
||||||
const favoritesResults = await Promise.all(favoritePromises);
|
const favoritesResults = await Promise.all(favoritePromises);
|
||||||
|
|
||||||
const enrichedItems = result.results.map((item, index) => ({
|
const enrichedItems = result.results.map((item, index) => ({
|
||||||
@@ -123,22 +124,19 @@
|
|||||||
this.update();
|
this.update();
|
||||||
},
|
},
|
||||||
|
|
||||||
addFavoris(id) {
|
addFavoris(id, type) {
|
||||||
window.favorite(id)
|
window.favorite(id, type);
|
||||||
this.searchs.items = this.searchs.items.map(item =>
|
this.searchs.items = this.searchs.items.map(item =>
|
||||||
item.id === id ? { ...item, isFavorite: true } : item
|
item.id === id ? { ...item, isFavorite: true } : item
|
||||||
);
|
);
|
||||||
|
|
||||||
this.update();
|
this.update();
|
||||||
},
|
},
|
||||||
|
|
||||||
async removeFavoris(id) {
|
async removeFavoris(id, type) {
|
||||||
await window.removeFavorite(id);
|
await window.removeFavorite(id, type);
|
||||||
|
|
||||||
this.searchs.items = this.searchs.items.map(item =>
|
this.searchs.items = this.searchs.items.map(item =>
|
||||||
item.id === id ? { ...item, isFavorite: false } : item
|
item.id === id ? { ...item, isFavorite: false } : item
|
||||||
);
|
);
|
||||||
|
|
||||||
this.update();
|
this.update();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,11 @@
|
|||||||
<div if={!state.loading && state.favorites.items.length > 0}>
|
<div if={!state.loading && state.favorites.items.length > 0}>
|
||||||
<div class="results-grid">
|
<div class="results-grid">
|
||||||
<div class="card" each={item in state.favorites.items}>
|
<div class="card" each={item in state.favorites.items}>
|
||||||
<a onclick={() => removeFavoris(item.id)}>
|
<a onclick={() => removeFavoris(item.id, item.type)}>
|
||||||
<button><i class="fa-solid fa-star"></i></button>
|
<button><i class="fa-solid fa-star"></i></button>
|
||||||
</a>
|
</a>
|
||||||
<a href={"#/release-details/" +(item.isMaster ? "master" : "release") + "/" + item.id}>
|
<a href={"#/release-details/" + item.type + "/" + item.id}>
|
||||||
<h4>{item.title}</h4>
|
<h4>{item.artists[0].name} – {item.title}</h4>
|
||||||
<img src={item.thumb} alt="cover" />
|
<img src={item.thumb} alt="cover" />
|
||||||
<div if={item.type !== 'artist'}>
|
<div if={item.type !== 'artist'}>
|
||||||
<p>{item.year}</p>
|
<p>{item.year}</p>
|
||||||
@@ -40,34 +40,32 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
async onMounted() {
|
async onMounted() {
|
||||||
const ids = await window.getFavorites();
|
const favs = await window.getFavorites();
|
||||||
|
|
||||||
const items = [];
|
const items = [];
|
||||||
for (const id of ids) {
|
|
||||||
const release = await window.getReleaseDetails(id);
|
for (const fav of favs) {
|
||||||
items.push(release);
|
let data;
|
||||||
|
if (fav.Type === "master") {
|
||||||
|
data = await window.getMasterDetails(fav.releaseId);
|
||||||
|
} else {
|
||||||
|
data = await window.getReleaseDetails(fav.releaseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
items.push({
|
||||||
|
...data,
|
||||||
|
type: fav.Type
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const masterPromises = items.map(item => isMaster(item.id, item.master_id));
|
this.state.favorites.items = items;
|
||||||
const masterResults = await Promise.all(masterPromises);
|
|
||||||
|
|
||||||
const enrichedItems = result.results.map((item, index) => ({
|
|
||||||
...item,
|
|
||||||
isMaster: masterResults[index]
|
|
||||||
}));
|
|
||||||
|
|
||||||
this.state.favorites.items = enrichedItems;
|
|
||||||
this.state.loading = false;
|
this.state.loading = false;
|
||||||
console.log(this.state)
|
console.log(this.state)
|
||||||
this.update();
|
this.update();
|
||||||
},
|
},
|
||||||
|
|
||||||
ismaster(id , master_id){
|
async removeFavoris(id, type) {
|
||||||
return id === master_id;
|
await window.removeFavorite(id, type);
|
||||||
},
|
|
||||||
|
|
||||||
async removeFavoris(id) {
|
|
||||||
await window.removeFavorite(id);
|
|
||||||
this.state.favorites.items = this.state.favorites.items.filter(item => item.id !== id);
|
this.state.favorites.items = this.state.favorites.items.filter(item => item.id !== id);
|
||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ window.observeAuthState = function(callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export async function favorite(releaseId) {
|
export async function favorite(releaseId, type) {
|
||||||
const storedUid = localStorage.getItem("uid");
|
const storedUid = localStorage.getItem("uid");
|
||||||
|
|
||||||
const usersRef = collection(db, "users");
|
const usersRef = collection(db, "users");
|
||||||
@@ -80,6 +80,7 @@ export async function favorite(releaseId) {
|
|||||||
|
|
||||||
await addDoc(favoritesRef, {
|
await addDoc(favoritesRef, {
|
||||||
releaseId: releaseId,
|
releaseId: releaseId,
|
||||||
|
Type: type,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,10 +108,16 @@ export async function getFavorites() {
|
|||||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||||
const snapshot = await getDocs(favoritesRef);
|
const snapshot = await getDocs(favoritesRef);
|
||||||
|
|
||||||
return snapshot.docs.map(doc => doc.data().releaseId);
|
return snapshot.docs.map(doc => {
|
||||||
|
const data = doc.data();
|
||||||
|
return {
|
||||||
|
releaseId: data.releaseId,
|
||||||
|
Type: data.Type
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isFavorite(releaseId) {
|
export async function isFavorite(releaseId, type) {
|
||||||
const storedUid = localStorage.getItem("uid");
|
const storedUid = localStorage.getItem("uid");
|
||||||
if (!storedUid) return false;
|
if (!storedUid) return false;
|
||||||
|
|
||||||
@@ -124,13 +131,17 @@ export async function isFavorite(releaseId) {
|
|||||||
const userDocId = userDoc.id;
|
const userDocId = userDoc.id;
|
||||||
|
|
||||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||||
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId));
|
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId), where("Type", "==", type));
|
||||||
const favSnapshot = await getDocs(favQuery);
|
const favSnapshot = await getDocs(favQuery);
|
||||||
|
|
||||||
return !favSnapshot.empty;
|
if (!favSnapshot.empty) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeFavorite(releaseId) {
|
export async function removeFavorite(releaseId, type) {
|
||||||
const storedUid = localStorage.getItem("uid");
|
const storedUid = localStorage.getItem("uid");
|
||||||
|
|
||||||
const usersRef = collection(db, "users");
|
const usersRef = collection(db, "users");
|
||||||
@@ -141,7 +152,7 @@ export async function removeFavorite(releaseId) {
|
|||||||
const userDocId = userDoc.id;
|
const userDocId = userDoc.id;
|
||||||
|
|
||||||
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
const favoritesRef = collection(db, "users", userDocId, "favorites");
|
||||||
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId));
|
const favQuery = query(favoritesRef, where("releaseId", "==", releaseId), where("Type", "==", type));
|
||||||
const favSnapshot = await getDocs(favQuery);
|
const favSnapshot = await getDocs(favQuery);
|
||||||
|
|
||||||
await deleteDoc(favSnapshot.docs[0].ref);
|
await deleteDoc(favSnapshot.docs[0].ref);
|
||||||
|
|||||||
Reference in New Issue
Block a user