1-
21import { useEffect , useState } from 'react' ;
32import {
43 collection ,
@@ -22,30 +21,60 @@ export const useAffiliates = (campaignId?: string) => {
2221 const { user } = useAuth ( ) ;
2322
2423 useEffect ( ( ) => {
24+ console . log ( '🔄 useAffiliates effect triggered' ) ;
25+ console . log ( '👤 User:' , user ?. uid ) ;
26+ console . log ( '📋 CampaignId:' , campaignId ) ;
27+
2528 if ( ! user ) {
29+ console . log ( '❌ No user, clearing affiliates' ) ;
2630 setAffiliates ( [ ] ) ;
2731 setLoading ( false ) ;
2832 return ;
2933 }
3034
3135 const affiliatesRef = collection ( db , 'affiliates' ) ;
32- // Maintenant que l'index est créé, on peut utiliser orderBy
33- let q = query ( affiliatesRef , where ( 'userId' , '==' , user . uid ) , orderBy ( 'createdAt' , 'desc' ) ) ;
36+ let q ;
3437
3538 if ( campaignId ) {
36- q = query ( affiliatesRef , where ( 'campaignId' , '==' , campaignId ) , orderBy ( 'createdAt' , 'desc' ) ) ;
39+ console . log ( '🎯 Querying affiliates for specific campaign:' , campaignId ) ;
40+ q = query (
41+ affiliatesRef ,
42+ where ( 'campaignId' , '==' , campaignId ) ,
43+ where ( 'userId' , '==' , user . uid ) ,
44+ orderBy ( 'createdAt' , 'desc' )
45+ ) ;
46+ } else {
47+ console . log ( '🎯 Querying all affiliates for user:' , user . uid ) ;
48+ q = query (
49+ affiliatesRef ,
50+ where ( 'userId' , '==' , user . uid ) ,
51+ orderBy ( 'createdAt' , 'desc' )
52+ ) ;
3753 }
3854
55+ console . log ( '🔗 Setting up Firestore listener...' ) ;
3956 const unsubscribe = onSnapshot ( q , ( snapshot ) => {
40- const affiliatesData = snapshot . docs . map ( doc => ( {
41- id : doc . id ,
42- ...doc . data ( ) ,
43- createdAt : doc . data ( ) . createdAt ?. toDate ( ) ,
44- } ) ) as Affiliate [ ] ;
57+ console . log ( '📊 Firestore snapshot received' ) ;
58+ console . log ( '📄 Documents count:' , snapshot . docs . length ) ;
59+
60+ const affiliatesData = snapshot . docs . map ( doc => {
61+ const data = doc . data ( ) ;
62+ console . log ( '📋 Affiliate doc:' , { id : doc . id , data } ) ;
63+ return {
64+ id : doc . id ,
65+ ...data ,
66+ createdAt : data . createdAt ?. toDate ( ) ,
67+ } ;
68+ } ) as Affiliate [ ] ;
4569
46- // Plus besoin de tri côté client maintenant que l'index est en place
70+ console . log ( '✅ Processed affiliates:' , affiliatesData ) ;
4771 setAffiliates ( affiliatesData ) ;
4872 setLoading ( false ) ;
73+ } , ( error ) => {
74+ console . error ( '❌ Firestore listener error:' , error ) ;
75+ console . error ( '❌ Error code:' , error . code ) ;
76+ console . error ( '❌ Error message:' , error . message ) ;
77+ setLoading ( false ) ;
4978 } ) ;
5079
5180 return unsubscribe ;
0 commit comments