From 6d33ec5727d4562f9fab69a62c9eacf3780422a7 Mon Sep 17 00:00:00 2001 From: Felix-Vimalaratnam Date: Fri, 28 Mar 2025 16:27:07 +0100 Subject: [PATCH] projet --- components/app.riot | 93 +++++++++++++++++++++++++-------- components/favorites.riot | 78 +++++++++++++++++++++------ components/login.riot | 2 +- components/register.riot | 2 +- components/release-details.riot | 3 +- services/discogsService.js | 4 +- services/firebaseService.js | 93 +++++++++++++++++++++++++++------ 7 files changed, 216 insertions(+), 59 deletions(-) diff --git a/components/app.riot b/components/app.riot index aa28c04..27c1ffc 100644 --- a/components/app.riot +++ b/components/app.riot @@ -1,8 +1,9 @@ @@ -17,11 +18,28 @@ + + +

Résultats ({searchs.pagination.items.total} résultats trouvés)

-
+ addFavoris(item.id)}> {item.title} cover @@ -55,33 +73,53 @@ base: 'https://dwarves.iut-fbleau.fr/~felix-vi/SAE/', route: 'search', authUser: null, + favoritesIds: [], searchs: { items: [], pagination: [], - type: 'release', + type: null, }, - async search(e) { - e.preventDefault() - const type = e.target.type.value - const query = e.target.query.value - if (!query) return - const result = await window.discogsearch(query, type) - this.searchs.items = result.results - this.searchs.pagination = result.pagination - this.searchs.type = type - this.update() + async search(e, page = 1) { + if (e) e.preventDefault(); + + const type = e?.target?.type?.value || this.searchs.type; + const query = e?.target?.query?.value || this.searchs.query; + if (!query) return; + + const result = await window.discogsearch(query, type, page); + + const enrichedItems = []; + for (const item of result.results) { + const isFav = await window.isFavorite(item.id); + enrichedItems.push({ ...item, isFavorite: isFav }); // évite de muter directement item + } + + this.searchs.items = enrichedItems; + this.searchs.pagination = result.pagination; + this.searchs.query = query; + this.searchs.type = type; + + this.update(); + }, + + changePage(page) { + this.search(null, page); }, - onMounted() { - observeAuthState(user => { - this.authUser = user - this.update() + async onMounted() { + observeAuthState(async user => { + this.authUser = user; + this.update(); }); }, addFavoris(id) { window.favorite(id) + }, + + Logout(){ + window.logout() } } @@ -145,14 +183,19 @@ button { color: white; - background: transparent; + background-color: #2196f3; border: none; font-weight: bold; cursor: pointer; + padding: 0.5em 1em; + border-radius: 5px; + margin: 0.5em; + transition: background-color 0.3s ease; } - + button:hover { - text-decoration: underline; + background-color: #1769aa; + text-decoration: none; } nav { @@ -163,6 +206,14 @@ justify-content: space-between; align-items: center; } + + .pagination { + display: flex; + justify-content: center; + align-items: center; + gap: 1rem; + margin-bottom: 2rem; + } nav a { margin: 0 10px; diff --git a/components/favorites.riot b/components/favorites.riot index 1b22add..38b0542 100644 --- a/components/favorites.riot +++ b/components/favorites.riot @@ -1,33 +1,79 @@

Mes favoris

-
    -
  • {item.title}
  • -
+ +
+ +

Aucun favori trouvé.

+ + \ No newline at end of file diff --git a/components/login.riot b/components/login.riot index e57dfce..fdbaaf3 100644 --- a/components/login.riot +++ b/components/login.riot @@ -11,7 +11,7 @@ e.preventDefault(); const email = e.target.email.value; const password = e.target.password.value; - login(email, password); + await login(email, password); window.location.href = '#'; } export default { diff --git a/components/register.riot b/components/register.riot index aa9d213..f0fb337 100644 --- a/components/register.riot +++ b/components/register.riot @@ -12,7 +12,7 @@ e.preventDefault(); const email = e.target.email.value; const password = e.target.password.value; - window.sign(email, password); + await window.sign(email, password); window.location.href = '#'; }, onMounted() { diff --git a/components/release-details.riot b/components/release-details.riot index 60d58e1..a9ed9fd 100644 --- a/components/release-details.riot +++ b/components/release-details.riot @@ -1,7 +1,8 @@

{state.release.title}

-

-{state.release.year}

+

{state.release.formats?.[0]?.name}-{state.release.year}

+

{state.release.community?.rating?.average}({state.release.community?.rating?.count}vote)

{state.release.title} diff --git a/services/discogsService.js b/services/discogsService.js index 97a78d0..09480cf 100644 --- a/services/discogsService.js +++ b/services/discogsService.js @@ -4,8 +4,8 @@ const key = 'NWmMhlPAbPlVnaDqVGyX'; const secret = 'hZPaoBiGiSwlCjARrbOICOpDuITwyJAm'; const perPage = 100; -export async function discogsearch(query, type) { - const url = `${baseUrlsearch}?q=${query}&type=${type}&per_page=${perPage}&key=${key}&secret=${secret}`; +export async function discogsearch(query, type, page = 1) { + const url = `${baseUrlsearch}?q=${query}&type=${type}&per_page=${perPage}&page=${page}&key=${key}&secret=${secret}`; const response = await fetch(url); return await response.json(); } diff --git a/services/firebaseService.js b/services/firebaseService.js index 90bd1b0..1197e68 100644 --- a/services/firebaseService.js +++ b/services/firebaseService.js @@ -16,6 +16,12 @@ import { setDoc } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js"; +import { + query, + where, + getDocs +} from "https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js"; + const firebaseConfig = { apiKey: "AIzaSyBXKB6AGbTN95lOLrIVpWrSIK_uL_C6GUA", authDomain: "music-app-riotjs.firebaseapp.com", @@ -31,17 +37,22 @@ const auth = getAuth(app); const db = getFirestore(app); export async function login(email, password) { - await signInWithEmailAndPassword(auth, email, password); + const userC = await signInWithEmailAndPassword(auth, email, password); + localStorage.setItem("uid", userC.user.uid); + return userC; } export async function sign(email, password) { const userCred = await createUserWithEmailAndPassword(auth, email, password); - await adduserdata(userCred.user.email); + await adduserdata(userCred.user.email, userCred.user.uid); + localStorage.setItem("uid", userC.user.uid); + return userCred; } -export async function adduserdata(email_user) { +export async function adduserdata(email_user, uid) { await addDoc(collection(db, "users"), { - email: email_user + email: email_user, + uid: uid }); } @@ -53,32 +64,80 @@ window.observeAuthState = function(callback) { onAuthStateChanged(auth, callback); }; + export async function favorite(releaseId) { - const user = auth.currentUser; - if (!user) return; - await addDoc(collection(db, "users", user.email, "favorites"), { - id: releaseId + const storedUid = localStorage.getItem("uid"); + + + + + const usersRef = collection(db, "users"); + const q = query(usersRef, where("uid", "==", storedUid)); + const querySnapshot = await getDocs(q); + + + const userDoc = querySnapshot.docs[0]; + const userDocId = userDoc.id; + + const favoritesRef = collection(db, "users", userDocId, "favorites"); + + await addDoc(favoritesRef, { + releaseId: releaseId, }); } + export async function getFavorites() { - const user = auth.currentUser; - if (!user) { - console.warn("Aucun utilisateur connecté."); + const storedUid = localStorage.getItem("uid"); + + if (!storedUid) { + console.warn("Aucun UID trouvé dans localStorage."); return []; } - const userDocRef = doc(db, "users", user.email); - const favsCollection = collection(userDocRef, "favorites"); + const usersRef = collection(db, "users"); + const q = query(usersRef, where("uid", "==", storedUid)); + const querySnapshot = await getDocs(q); - const snapshot = await getDocs(favsCollection); - const favorites = snapshot.docs.map(doc => doc.data()); + if (querySnapshot.empty) { + console.warn("Aucun document utilisateur trouvé avec cet UID."); + return []; + } - return favorites; + const userDoc = querySnapshot.docs[0]; + const userDocId = userDoc.id; + + const favoritesRef = collection(db, "users", userDocId, "favorites"); + const snapshot = await getDocs(favoritesRef); + + return snapshot.docs.map(doc => doc.data().releaseId); +} + +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); + + if (userSnapshot.empty) return false; + + 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 } window.login = login; window.sign = sign; window.logout = logout; window.favorite = favorite; -window.getFavorites = getFavorites; \ No newline at end of file +window.getFavorites = getFavorites; +window.isFavorite = isFavorite; \ No newline at end of file