@@ -7,10 +7,8 @@ import {
7
7
PointElement ,
8
8
Title ,
9
9
Tooltip ,
10
- } from 'chart.js'
11
- import {
12
- Line ,
13
- } from 'react-chartjs-2' ;
10
+ } from 'chart.js' ;
11
+ import { Line } from 'react-chartjs-2' ;
14
12
15
13
ChartJS . register (
16
14
LineElement ,
@@ -24,54 +22,31 @@ ChartJS.register(
24
22
const LineChart = ( { data } ) => {
25
23
const [ view , setView ] = useState ( 'day' ) ;
26
24
27
- // Null checking for data prop
28
- if ( ! data || ! Array . isArray ( data ) || data . length === 0 ) {
29
- console . error ( "Invalid or empty data provided to LineChart component" ) ;
30
- return < div > No data available for the chart.</ div > ;
31
- }
32
-
33
25
const convertDataForLineChart = useMemo ( ( ) => {
34
26
const countsPerDay = { } ;
27
+ const validDates = [ ] ;
35
28
36
- // Ensure data is an array before forEach
37
- if ( Array . isArray ( data ) ) {
38
- data . forEach ( ( report ) => {
39
- if ( report && typeof report === 'object' && 'reported_time' in report ) {
40
- try {
41
- const reportDate = new Date ( report . reported_time ) ;
42
- if ( ! isNaN ( reportDate . getTime ( ) ) ) {
43
- const reportedDate = reportDate . toLocaleDateString ( 'en-GB' , {
44
- day : '2-digit' ,
45
- month : '2-digit'
46
- } ) ;
47
- countsPerDay [ reportedDate ] = ( countsPerDay [ reportedDate ] || 0 ) + 1 ;
48
- } else {
49
- console . warn ( "Invalid date encountered:" , report . reported_time ) ;
50
- }
51
- } catch ( error ) {
52
- console . warn ( "Error processing report date:" , error ) ;
53
- }
54
- } else {
55
- console . warn ( "Encountered a report with missing or invalid reported_time" ) ;
56
- }
57
- } ) ;
58
- } else {
59
- console . error ( "Data is not an array in convertDataForLineChart" ) ;
60
- return { dates : [ ] , counts : [ ] } ;
61
- }
62
-
63
- const dates = [ ] ;
64
- const validDates = Array . isArray ( data ) ? data
65
- . filter ( report => report && typeof report === 'object' && 'reported_time' in report )
66
- . map ( report => {
29
+ data . forEach ( ( report , index ) => {
30
+ if ( report && typeof report === 'object' && 'reported_time' in report ) {
67
31
try {
68
- const date = new Date ( report . reported_time ) ;
69
- return isNaN ( date . getTime ( ) ) ? null : date ;
70
- } catch {
71
- return null ;
32
+ const reportDate = new Date ( report . reported_time ) ;
33
+ if ( ! isNaN ( reportDate . getTime ( ) ) ) {
34
+ const reportedDate = reportDate . toLocaleDateString ( 'en-GB' , {
35
+ day : '2-digit' ,
36
+ month : '2-digit'
37
+ } ) ;
38
+ countsPerDay [ reportedDate ] = ( countsPerDay [ reportedDate ] || 0 ) + 1 ;
39
+ validDates . push ( reportDate ) ;
40
+ } else {
41
+ console . warn ( `Invalid date encountered at index ${ index } :` , report . reported_time ) ;
42
+ }
43
+ } catch ( error ) {
44
+ console . warn ( `Error processing report date at index ${ index } :` , error ) ;
72
45
}
73
- } )
74
- . filter ( date => date !== null ) : [ ] ;
46
+ } else {
47
+ console . warn ( `Encountered a report with missing or invalid reported_time at index ${ index } ` ) ;
48
+ }
49
+ } ) ;
75
50
76
51
if ( validDates . length === 0 ) {
77
52
console . error ( "No valid dates found in the data" ) ;
@@ -80,6 +55,7 @@ const LineChart = ({ data }) => {
80
55
81
56
const minDate = new Date ( Math . min ( ...validDates ) ) ;
82
57
const maxDate = new Date ( Math . max ( ...validDates ) ) ;
58
+ const dates = [ ] ;
83
59
84
60
for ( let currentDate = new Date ( minDate ) ; currentDate <= maxDate ; currentDate . setDate ( currentDate . getDate ( ) + ( view === 'day' ? 1 : view === 'week' ? 7 : view === 'month' ? 30 : 365 ) ) ) {
85
61
const formattedDate = currentDate . toLocaleDateString ( 'en-GB' , {
@@ -99,8 +75,7 @@ const LineChart = ({ data }) => {
99
75
100
76
const { dates, counts } = convertDataForLineChart ;
101
77
102
- // Check if we have valid data after conversion
103
- if ( ! dates || ! counts || ! Array . isArray ( dates ) || ! Array . isArray ( counts ) || dates . length === 0 || counts . length === 0 ) {
78
+ if ( ! dates . length || ! counts . length ) {
104
79
console . error ( "No valid data after conversion in LineChart component" ) ;
105
80
return < div > Unable to generate chart due to insufficient data.</ div > ;
106
81
}
@@ -189,4 +164,4 @@ const LineChart = ({ data }) => {
189
164
)
190
165
}
191
166
192
- export default LineChart ;
167
+ export default LineChart ;
0 commit comments