@@ -17,7 +17,15 @@ function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, u
1717
1818 const fromDate = useMemo ( ( ) => startDate . toLocaleDateString ( 'en-CA' ) , [ startDate ] ) ;
1919 const toDate = useMemo ( ( ) => endDate . toLocaleDateString ( 'en-CA' ) , [ endDate ] ) ;
20- const userList = useMemo ( ( ) => userProfiles . map ( ( { _id } ) => _id ) , [ userProfiles ] ) ;
20+ const userList = useMemo ( ( ) => {
21+ const list = userProfiles ?. map ( ( { _id } ) => _id ) || [ ] ;
22+ // eslint-disable-next-line no-console
23+ console . log ( 'TotalContributorsReport userList created:' , {
24+ userProfilesLength : userProfiles ?. length ,
25+ userListLength : list . length ,
26+ } ) ;
27+ return list ;
28+ } , [ userProfiles ] ) ;
2129
2230 // Fetch time entries for the selected period
2331 const loadTimeEntriesForPeriod = useCallback ( async ( controller ) => {
@@ -26,12 +34,59 @@ function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, u
2634 if ( ! url ) {
2735 return ;
2836 }
37+
38+ // Don't make API call if userList is empty
39+ if ( ! userList || userList . length === 0 ) {
40+ // eslint-disable-next-line no-console
41+ console . warn ( 'TotalContributorsReport: Skipping API call - userList is empty' , {
42+ userProfilesLength : userProfiles ?. length ,
43+ userListLength : userList ?. length ,
44+ } ) ;
45+ setTimeEntries ( [ ] ) ;
46+ setLoading ( false ) ;
47+ return ;
48+ }
49+
50+ // Check cache with date range key to ensure cache is valid for current date range
51+ const cacheKey = `TotalContributorsReport_${ fromDate } _${ toDate } ` ;
52+ const cachedData = localStorage . getItem ( cacheKey ) ;
53+ if ( cachedData ) {
54+ try {
55+ const parsedData = JSON . parse ( cachedData ) ;
56+ if ( parsedData && Array . isArray ( parsedData ) && parsedData . length > 0 ) {
57+ // eslint-disable-next-line no-console
58+ console . log ( 'TotalContributorsReport: Using cached data' , {
59+ cacheKey,
60+ dataLength : parsedData . length ,
61+ } ) ;
62+ setTimeEntries ( parsedData ) ;
63+ setLoading ( false ) ;
64+ return ;
65+ }
66+ } catch ( e ) {
67+ // eslint-disable-next-line no-console
68+ console . warn ( 'TotalContributorsReport: Failed to parse cached data' , e ) ;
69+ }
70+ }
71+
2972 try {
73+ // eslint-disable-next-line no-console
74+ console . log ( 'TotalContributorsReport API Request:' , {
75+ url,
76+ payload : { users : userList , fromDate, toDate } ,
77+ usersCount : userList ?. length ,
78+ timestamp : new Date ( ) . toISOString ( ) ,
79+ } ) ;
3080 const response = await axios . post (
3181 url ,
3282 { users : userList , fromDate, toDate } ,
3383 { signal : controller . signal }
3484 ) ;
85+ // eslint-disable-next-line no-console
86+ console . log ( 'TotalContributorsReport API Response:' , {
87+ dataLength : response . data ?. length ,
88+ timestamp : new Date ( ) . toISOString ( ) ,
89+ } ) ;
3590 const mappedTimeEntries = response . data . map ( entry => ( {
3691 userId : entry . personId ,
3792 hours : entry . hours ,
@@ -40,14 +95,26 @@ function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, u
4095 date : entry . dateOfWork ,
4196 } ) ) ;
4297 setTimeEntries ( mappedTimeEntries ) ;
98+
99+ // Cache the data with date range key
100+ if ( mappedTimeEntries . length > 0 ) {
101+ localStorage . setItem ( cacheKey , JSON . stringify ( mappedTimeEntries ) ) ;
102+ // eslint-disable-next-line no-console
103+ console . log ( 'TotalContributorsReport: Data cached' , { cacheKey, dataLength : mappedTimeEntries . length } ) ;
104+ } else {
105+ // eslint-disable-next-line no-console
106+ console . warn ( 'TotalContributorsReport: Empty response - clearing cache' , { cacheKey } ) ;
107+ localStorage . removeItem ( cacheKey ) ;
108+ }
43109 } catch ( error ) {
44110 // eslint-disable-next-line import/no-named-as-default-member
45111 if ( ! axios . isCancel ( error ) ) {
46- // Handle error silently or show user-friendly message
112+ // eslint-disable-next-line no-console
113+ console . error ( 'TotalContributorsReport API Error:' , error ) ;
47114 setTimeEntries ( [ ] ) ;
48115 }
49116 }
50- } , [ fromDate , toDate , userList ] ) ;
117+ } , [ fromDate , toDate , userList , userProfiles ] ) ;
51118
52119 // Group time entries by user and calculate total hours
53120 const sumByUser = useCallback ( ( entries ) => {
@@ -141,13 +208,23 @@ function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, u
141208
142209 // Load data when date range changes
143210 useEffect ( ( ) => {
211+ // Only make API call if userList has data
212+ if ( ! userList || userList . length === 0 ) {
213+ // eslint-disable-next-line no-console
214+ console . log ( 'TotalContributorsReport: Waiting for userProfiles to load...' , {
215+ userProfilesLength : userProfiles ?. length ,
216+ userListLength : userList ?. length ,
217+ } ) ;
218+ return ;
219+ }
220+
144221 setLoading ( true ) ;
145222 const controller = new AbortController ( ) ;
146223 loadTimeEntriesForPeriod ( controller ) . then ( ( ) => {
147224 setLoading ( false ) ;
148225 } ) ;
149226 return ( ) => controller . abort ( ) ;
150- } , [ loadTimeEntriesForPeriod ] ) ;
227+ } , [ loadTimeEntriesForPeriod , userList ] ) ;
151228
152229 // Process data when time entries are loaded
153230 useEffect ( ( ) => {
0 commit comments