22
33'use client' ;
44
5- import { useState , useEffect } from 'react' ;
6- import { useSelector } from 'react-redux' ;
7- import axios from 'axios' ;
5+ import { useState , useMemo } from 'react' ;
86import { PieChart , Pie , Cell , Tooltip , Legend , ResponsiveContainer } from 'recharts' ;
97import { ENDPOINTS } from '~/utils/URL' ;
10- import { events as mockEvents } from './mockData' ;
8+ import { events } from './mockData' ;
119import styles from './AttendanceNoShowCharts.module.css' ;
10+ import { useSelector } from 'react-redux' ;
1211
1312const attendanceColors = [ '#0088FE' , '#FF8042' , '#FFBB28' ] ;
1413const noShowColors = [ '#00C49F' , '#FF0000' ] ;
@@ -145,7 +144,9 @@ const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, per
145144} ;
146145
147146const CustomTooltip = ( { active, payload } ) => {
147+ console . log ( active , payload ) ;
148148 if ( active && payload && payload . length ) {
149+ console . log ( 'Tooltip payload:' , payload ) ;
149150 return (
150151 < div className = { styles . chartTooltip } >
151152 < p className = { styles . chartTooltipItem } > { `${ payload [ 0 ] . name } : ${ payload [ 0 ] . value } ` } </ p >
@@ -156,76 +157,14 @@ const CustomTooltip = ({ active, payload }) => {
156157} ;
157158
158159function AttendanceNoShowCharts ( ) {
159- const [ events , setEvents ] = useState ( mockEvents ) ;
160- const [ selectedEvent , setSelectedEvent ] = useState ( null ) ;
161- const [ loading , setLoading ] = useState ( true ) ;
160+ const [ selectedId , setSelectedId ] = useState ( events [ 0 ] . id ) ;
161+ const [ loading , setLoading ] = useState ( false ) ;
162162 const [ error , setError ] = useState ( null ) ;
163163 const darkMode = useSelector ( state => state . theme . darkMode ) ;
164-
165- // Fetch events data from backend
166- useEffect ( ( ) => {
167- const fetchEvents = async ( ) => {
168- setLoading ( true ) ;
169- setError ( null ) ;
170- try {
171- const response = await axios . get ( ENDPOINTS . EVENT_ATTENDANCE_STATS ) ;
172-
173- // Check if response has data in expected format
174- if ( response . data && Array . isArray ( response . data ) ) {
175- // Transform API data to match mock data format if needed
176- const transformedEvents = response . data . map ( transformEvent ) ;
177-
178- setEvents ( transformedEvents ) ;
179- if ( transformedEvents . length > 0 ) {
180- setSelectedEvent ( transformedEvents [ 0 ] ) ;
181- }
182- } else if ( response . data && response . data . events && Array . isArray ( response . data . events ) ) {
183- // Handle nested response structure
184- const transformedEvents = response . data . events . map ( transformEvent ) ;
185-
186- setEvents ( transformedEvents ) ;
187- if ( transformedEvents . length > 0 ) {
188- setSelectedEvent ( transformedEvents [ 0 ] ) ;
189- }
190- } else {
191- // If data format doesn't match, fall back to mock data
192- throw new Error ( 'Unexpected data format from API' ) ;
193- }
194- } catch ( err ) {
195- // Fall back to mock data if API is unavailable or returns error
196- // eslint-disable-next-line no-console
197- console . warn ( 'Failed to fetch events from API, using mock data:' , err . message ) ;
198- // Calculate status for mock events
199- const mockEventsWithStatus = mockEvents . map ( event => ( {
200- ...event ,
201- status : calculateEventStatus ( event ) || event . status ,
202- } ) ) ;
203- setEvents ( mockEventsWithStatus ) ;
204- if ( mockEventsWithStatus . length > 0 ) {
205- setSelectedEvent ( mockEventsWithStatus [ 0 ] ) ;
206- }
207- setError ( 'Using mock data - API endpoint not available' ) ;
208- } finally {
209- setLoading ( false ) ;
210- }
211- } ;
212-
213- fetchEvents ( ) ;
214- } , [ ] ) ;
215-
216- // Update selectedEvent when events change
217- useEffect ( ( ) => {
218- if ( events . length > 0 && ! selectedEvent ) {
219- setSelectedEvent ( events [ 0 ] ) ;
220- }
221- } , [ events , selectedEvent ] ) ;
164+ const selectedEvent = useMemo ( ( ) => events . find ( e => e . id === selectedId ) , [ selectedId ] ) ;
222165
223166 const handleEventChange = e => {
224- const selectedEventId = e . target . value ;
225- const newSelectedEvent = events . find ( event => event . id === selectedEventId ) ;
226- if ( newSelectedEvent ) {
227- setSelectedEvent ( newSelectedEvent ) ;
228- }
167+ setSelectedId ( e . target . value ) ;
229168 } ;
230169
231170 const calculatePercentage = ( value , total ) => {
@@ -479,14 +418,19 @@ function AttendanceNoShowCharts() {
479418 ) }
480419
481420 { /* No-Show Chart */ }
482- < div className = { styles . chartWrapper } >
483- < h3 className = { styles . chartSectionTitle } >
484- Registration vs Attendance
485- { currentStatus === 'In Progress' && (
486- < span className = { styles . chartLiveBadge } > (Live)</ span >
487- ) }
421+ < div style = { { width : '100%' } } >
422+ < h3
423+ style = { {
424+ fontSize : '18px' ,
425+ fontWeight : '500' ,
426+ color : '#111827' ,
427+ marginBottom : '12px' ,
428+ textAlign : 'center' ,
429+ } }
430+ >
431+ No-Show Breakdown
488432 </ h3 >
489- < div className = { styles . chartContainer } >
433+ < div style = { { height : '300px' , width : '100%' } } >
490434 < ResponsiveContainer width = "100%" height = "100%" >
491435 < PieChart >
492436 < Pie
0 commit comments