Skip to content

Commit feb7b57

Browse files
Added caching to load all data at the start
1 parent c3f3db5 commit feb7b57

3 files changed

Lines changed: 266 additions & 19 deletions

File tree

src/components/Reports/TotalReport/TotalContributorsReport.jsx

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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(() => {

src/components/Reports/TotalReport/TotalPeopleReport.jsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,37 @@ function TotalPeopleReport(props) {
5757
return;
5858
}
5959

60+
// Check cache with date range key to ensure cache is valid for current date range
61+
const cacheKey = `TotalPeopleReport_${fromDate}_${toDate}`;
62+
const cachedData = localStorage.getItem(cacheKey);
63+
if (cachedData) {
64+
try {
65+
const parsedData = JSON.parse(cachedData);
66+
if (parsedData && Array.isArray(parsedData) && parsedData.length > 0) {
67+
// eslint-disable-next-line no-console
68+
console.log('TotalPeopleReport: Using cached data', {
69+
cacheKey,
70+
dataLength: parsedData.length,
71+
});
72+
setAllTimeEntries(parsedData);
73+
setTotalPeopleReportDataLoading(false);
74+
setTotalPeopleReportDataReady(true);
75+
return;
76+
}
77+
} catch (e) {
78+
// eslint-disable-next-line no-console
79+
console.warn('TotalPeopleReport: Failed to parse cached data', e);
80+
}
81+
}
82+
6083
try {
6184
// eslint-disable-next-line no-console
6285
console.log('TotalPeopleReport API Request:', {
6386
url,
6487
payload: { users: userList, fromDate, toDate },
6588
usersCount: userList?.length,
6689
userProfilesCount: userProfiles?.length,
90+
timestamp: new Date().toISOString(),
6791
});
6892
const res = await axios.post(
6993
url,
@@ -74,22 +98,48 @@ function TotalPeopleReport(props) {
7498
console.log('TotalPeopleReport API Response:', {
7599
dataLength: res.data?.length,
76100
sampleData: res.data?.slice(0, 2),
101+
timestamp: new Date().toISOString(),
102+
responseTime: new Date().toISOString(),
77103
});
104+
78105
const timeEntries = res.data.map(entry => ({
79106
userId: entry.personId,
80107
hours: entry.hours,
81108
minutes: entry.minutes,
82109
isTangible: entry.isTangible,
83110
date: entry.dateOfWork,
84111
}));
112+
113+
// Log if response seems incomplete (very few entries for many users)
114+
if (timeEntries.length > 0 && userList.length > 100 && timeEntries.length < 10) {
115+
// eslint-disable-next-line no-console
116+
console.warn('TotalPeopleReport: Response may be incomplete', {
117+
timeEntriesCount: timeEntries.length,
118+
usersCount: userList.length,
119+
ratio: (timeEntries.length / userList.length * 100).toFixed(2) + '%',
120+
message: 'If data seems incomplete, backend may still be processing. Try refreshing in a few minutes.',
121+
});
122+
}
123+
85124
setAllTimeEntries(timeEntries);
125+
126+
// Cache the data with date range key
127+
if (timeEntries.length > 0) {
128+
localStorage.setItem(cacheKey, JSON.stringify(timeEntries));
129+
// eslint-disable-next-line no-console
130+
console.log('TotalPeopleReport: Data cached', { cacheKey, dataLength: timeEntries.length });
131+
} else {
132+
// eslint-disable-next-line no-console
133+
console.warn('TotalPeopleReport: Empty response - clearing cache', { cacheKey });
134+
localStorage.removeItem(cacheKey);
135+
}
86136
} catch (error) {
87137
// eslint-disable-next-line no-console
88138
console.error('TotalPeopleReport API Error:', error);
89139
setTotalPeopleReportDataLoading(false);
90140
}
91141
},
92-
[fromDate, toDate, userList],
142+
[fromDate, toDate, userList, userProfiles],
93143
);
94144

95145
const sumByUser = useCallback((objectArray, property) => {

0 commit comments

Comments
 (0)