Skip to content

Commit b6f322a

Browse files
Added generate bar data as a utility to reduce code duplication
1 parent feb7b57 commit b6f322a

5 files changed

Lines changed: 40 additions & 62 deletions

File tree

src/components/Reports/TotalReport/TotalContributorsReport.jsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import './TotalReport.css';
55
import TotalReportBarGraph from './TotalReportBarGraph';
66
import Loading from '../../common/Loading';
77
import EditableInfoModal from '../../UserProfile/EditableModal/EditableInfoModal';
8+
import { generateBarData as generateBarDataUtil } from './generateBarData';
89

910
function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, userRole }) {
1011
const [contributors, setContributors] = useState([]);
@@ -169,25 +170,7 @@ function TotalContributorsReport({ startDate, endDate, userProfiles, darkMode, u
169170

170171
// Generate bar chart data
171172
const generateBarData = useCallback((groupedDate, isYear = false) => {
172-
if (isYear) {
173-
const startMonth = startDate.getMonth();
174-
const endMonth = endDate.getMonth();
175-
const sumData = groupedDate.map(range => ({
176-
label: range.timeRange,
177-
value: range.usersOfTime.length,
178-
months: 12,
179-
}));
180-
if (sumData.length > 1) {
181-
sumData[0].months = 12 - startMonth;
182-
sumData[sumData.length - 1].months = endMonth + 1;
183-
}
184-
const filteredData = sumData.filter(data => data.value > 0);
185-
return filteredData;
186-
}
187-
return groupedDate.map(range => ({
188-
label: range.timeRange,
189-
value: range.usersOfTime.length,
190-
}));
173+
return generateBarDataUtil(groupedDate, isYear, startDate, endDate, 'usersOfTime');
191174
}, [startDate, endDate]);
192175

193176
// Check if we should show monthly/yearly summaries

src/components/Reports/TotalReport/TotalPeopleReport.jsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button } from 'reactstrap';
77
import ReactTooltip from 'react-tooltip';
88
import TotalReportBarGraph from './TotalReportBarGraph';
99
import Loading from '../../common/Loading';
10+
import { generateBarData as generateBarDataUtil } from './generateBarData';
1011

1112
function TotalPeopleReport(props) {
1213
const { startDate, endDate, userProfiles, darkMode } = props;
@@ -243,25 +244,7 @@ function TotalPeopleReport(props) {
243244

244245
const generateBarData = useCallback(
245246
(groupedDate, isYear = false) => {
246-
if (isYear) {
247-
const startMonth = startDate.getMonth();
248-
const endMonth = endDate.getMonth();
249-
const sumData = groupedDate.map(range => ({
250-
label: range.timeRange,
251-
value: range.usersOfTime.length,
252-
months: 12,
253-
}));
254-
if (sumData.length > 1) {
255-
sumData[0].months = 12 - startMonth;
256-
sumData[sumData.length - 1].months = endMonth + 1;
257-
}
258-
const filteredData = sumData.filter(data => data.value > 0);
259-
return filteredData;
260-
}
261-
return groupedDate.map(range => ({
262-
label: range.timeRange,
263-
value: range.usersOfTime.length,
264-
}));
247+
return generateBarDataUtil(groupedDate, isYear, startDate, endDate, 'usersOfTime');
265248
},
266249
[startDate, endDate],
267250
);

src/components/Reports/TotalReport/TotalProjectReport.jsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button } from 'reactstrap';
77
import ReactTooltip from 'react-tooltip';
88
import TotalReportBarGraph from './TotalReportBarGraph';
99
import Loading from '../../common/Loading';
10+
import { generateBarData as generateBarDataUtil } from './generateBarData';
1011

1112
function TotalProjectReport(props) {
1213
const { startDate, endDate, userProfiles, projects, darkMode } = props;
@@ -235,25 +236,7 @@ function TotalProjectReport(props) {
235236

236237
const generateBarData = useCallback(
237238
(groupedDate, isYear = false) => {
238-
if (isYear) {
239-
const startMonth = startDate.getMonth();
240-
const endMonth = endDate.getMonth();
241-
const sumData = groupedDate.map(range => ({
242-
label: range.timeRange,
243-
value: range.projectsOfTime.length,
244-
months: 12,
245-
}));
246-
if (sumData.length > 1) {
247-
sumData[0].months = 12 - startMonth;
248-
sumData[sumData.length - 1].months = endMonth + 1;
249-
}
250-
const filteredData = sumData.filter(data => data.value > 0);
251-
return filteredData;
252-
}
253-
return groupedDate.map(range => ({
254-
label: range.timeRange,
255-
value: range.projectsOfTime.length,
256-
}));
239+
return generateBarDataUtil(groupedDate, isYear, startDate, endDate, 'projectsOfTime');
257240
},
258241
[startDate, endDate],
259242
);

src/components/Reports/TotalReport/TotalTeamReport.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import './TotalReport.css';
55
import { Button } from 'reactstrap';
66
import ReactTooltip from 'react-tooltip';
77
import Loading from '../../common/Loading';
8+
import { generateBarData as generateBarDataUtil } from './generateBarData';
89

910
const LazyTotalReportBarGraph = React.lazy(() => import('./TotalReportBarGraph'));
1011

@@ -237,11 +238,8 @@ function TotalTeamReport(props) {
237238
}, []);
238239
};
239240

240-
const generateBarData = groupedData => {
241-
return groupedData.map(range => ({
242-
label: `${range.timeRange}`,
243-
value: range.teamsOfTime.length,
244-
}));
241+
const generateBarData = (groupedData, isYear = false) => {
242+
return generateBarDataUtil(groupedData, isYear, startDate, endDate, 'teamsOfTime');
245243
};
246244
// Filter teams by end date to remove those created after the selected period
247245
const filterTeamByEndDate = (teams, endDateTime) => {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Shared utility function to generate bar chart data for reports
3+
* @param {Array} groupedDate - Array of grouped data by time range
4+
* @param {boolean} isYear - Whether this is yearly data
5+
* @param {Date} startDate - Start date of the period
6+
* @param {Date} endDate - End date of the period
7+
* @param {string} dataProperty - Property name to access the data array (e.g., 'usersOfTime', 'projectsOfTime', 'teamsOfTime')
8+
* @returns {Array} Array of bar chart data objects with label and value
9+
*/
10+
export const generateBarData = (groupedDate, isYear, startDate, endDate, dataProperty) => {
11+
if (isYear) {
12+
const startMonth = startDate.getMonth();
13+
const endMonth = endDate.getMonth();
14+
const sumData = groupedDate.map(range => ({
15+
label: range.timeRange,
16+
value: range[dataProperty]?.length || 0,
17+
months: 12,
18+
}));
19+
if (sumData.length > 1) {
20+
sumData[0].months = 12 - startMonth;
21+
sumData[sumData.length - 1].months = endMonth + 1;
22+
}
23+
const filteredData = sumData.filter(data => data.value > 0);
24+
return filteredData;
25+
}
26+
return groupedDate.map(range => ({
27+
label: range.timeRange,
28+
value: range[dataProperty]?.length || 0,
29+
}));
30+
};
31+

0 commit comments

Comments
 (0)