|
| 1 | +import { useState } from 'react'; |
| 2 | +import './EventStats.css'; |
| 3 | +import { useSelector } from 'react-redux'; |
| 4 | + |
| 5 | +const dummyData = [ |
| 6 | + { |
| 7 | + id: 1, |
| 8 | + type: 'Type of Event 1', |
| 9 | + attended: 20, |
| 10 | + enrolled: 25, |
| 11 | + time: 'Morning', |
| 12 | + location: 'Offline', |
| 13 | + }, |
| 14 | + { |
| 15 | + id: 2, |
| 16 | + type: 'Type of Event 2', |
| 17 | + attended: 19, |
| 18 | + enrolled: 20, |
| 19 | + time: 'Afternoon', |
| 20 | + location: 'Online', |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 3, |
| 24 | + type: 'Type of Event 3', |
| 25 | + attended: 12, |
| 26 | + enrolled: 18, |
| 27 | + time: 'Night', |
| 28 | + location: 'Offline', |
| 29 | + }, |
| 30 | + { |
| 31 | + id: 4, |
| 32 | + type: 'Type of Event 4', |
| 33 | + attended: 11, |
| 34 | + enrolled: 20, |
| 35 | + time: 'Morning', |
| 36 | + location: 'Online', |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 5, |
| 40 | + type: 'Type of Event 5', |
| 41 | + attended: 8, |
| 42 | + enrolled: 20, |
| 43 | + time: 'Afternoon', |
| 44 | + location: 'Offline', |
| 45 | + }, |
| 46 | + { id: 6, type: 'Type of Event 6', attended: 7, enrolled: 22, time: 'Night', location: 'Offline' }, |
| 47 | + { |
| 48 | + id: 7, |
| 49 | + type: 'Type of Event 7', |
| 50 | + attended: 4, |
| 51 | + enrolled: 20, |
| 52 | + time: 'Morning', |
| 53 | + location: 'Online', |
| 54 | + }, |
| 55 | +]; |
| 56 | + |
| 57 | +export default function PopularEvents() { |
| 58 | + const [timeFilter, setTimeFilter] = useState('All day'); |
| 59 | + const [typeFilter, setTypeFilter] = useState('All'); |
| 60 | + |
| 61 | + const calculatePercentage = (attended, enrolled) => Math.round((attended / enrolled) * 100); |
| 62 | + |
| 63 | + const getBarColor = percentage => { |
| 64 | + if (percentage > 60) return 'green'; |
| 65 | + if (percentage > 40) return 'orange'; |
| 66 | + return 'red'; |
| 67 | + }; |
| 68 | + |
| 69 | + const filteredData = dummyData.filter(event => { |
| 70 | + const timeMatch = timeFilter === 'All day' || event.time === timeFilter; |
| 71 | + const typeMatch = typeFilter === 'All' || event.location === typeFilter; |
| 72 | + return timeMatch && typeMatch; |
| 73 | + }); |
| 74 | + |
| 75 | + const mostPopularEvent = filteredData.reduce( |
| 76 | + (max, event) => |
| 77 | + calculatePercentage(event.attended, event.enrolled) > |
| 78 | + calculatePercentage(max.attended, max.enrolled) |
| 79 | + ? event |
| 80 | + : max, |
| 81 | + filteredData[0] || {}, |
| 82 | + ); |
| 83 | + |
| 84 | + const leastPopularEvent = filteredData.reduce( |
| 85 | + (min, event) => |
| 86 | + calculatePercentage(event.attended, event.enrolled) < |
| 87 | + calculatePercentage(min.attended, min.enrolled) |
| 88 | + ? event |
| 89 | + : min, |
| 90 | + filteredData[0] || {}, |
| 91 | + ); |
| 92 | + const darkMode = useSelector(state => state.theme.darkMode); |
| 93 | + return ( |
| 94 | + <div className={`popular-events-container ${darkMode ? 'popular-events-container-dark' : ''}`}> |
| 95 | + <div className={`header-container ${darkMode ? 'header-container-dark' : ''}`}> |
| 96 | + <h2 className={`popular-events-header ${darkMode ? 'popular-events-header-dark' : ''}`}> |
| 97 | + Most Popular Event |
| 98 | + </h2> |
| 99 | + <div className={`filters ${darkMode ? 'filters-dark' : ''}`}> |
| 100 | + <select value={timeFilter} onChange={e => setTimeFilter(e.target.value)}> |
| 101 | + <option value="All day">All day</option> |
| 102 | + <option value="Morning">Morning</option> |
| 103 | + <option value="Afternoon">Afternoon</option> |
| 104 | + <option value="Night">Night</option> |
| 105 | + </select> |
| 106 | + <select value={typeFilter} onChange={e => setTypeFilter(e.target.value)}> |
| 107 | + <option value="All">All</option> |
| 108 | + <option value="Offline">Offline</option> |
| 109 | + <option value="Online">Online</option> |
| 110 | + </select> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className={`stats ${darkMode ? 'stats-dark' : ''}`}> |
| 115 | + {filteredData.map(event => ( |
| 116 | + <div key={event.id} className="stat-item"> |
| 117 | + <div className={`stat-label ${darkMode ? 'stat-label-dark' : ''}`}>{event.type}</div> |
| 118 | + <div className="stat-bar"> |
| 119 | + <div |
| 120 | + className={`bar ${getBarColor( |
| 121 | + calculatePercentage(event.attended, event.enrolled), |
| 122 | + )}`} |
| 123 | + style={{ width: `${calculatePercentage(event.attended, event.enrolled)}%` }} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + <div className={`stat-value ${darkMode ? 'stat-value-dark' : ''}`}> |
| 127 | + {`${calculatePercentage(event.attended, event.enrolled)}% (${event.attended}/${ |
| 128 | + event.enrolled |
| 129 | + })`} |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ))} |
| 133 | + </div> |
| 134 | + <div className="summary"> |
| 135 | + <div className={`summary-item ${darkMode ? 'summary-item-dark' : ''}`}> |
| 136 | + <div className={`summary-title ${darkMode ? 'summary-title-dark' : ''}`}> |
| 137 | + Total Number of Events |
| 138 | + </div> |
| 139 | + <div className={`summary-value ${darkMode ? 'summary-value-dark' : ''}`}> |
| 140 | + {filteredData.length} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + <div className={`summary-item ${darkMode ? 'summary-item-dark' : ''}`}> |
| 144 | + <div className={`summary-title ${darkMode ? 'summary-title-dark' : ''}`}> |
| 145 | + Total Number of Event Enrollments |
| 146 | + </div> |
| 147 | + <div className={`summary-value ${darkMode ? 'summary-value-dark' : ''}`}> |
| 148 | + {filteredData.reduce((acc, event) => acc + event.enrolled, 0)} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + {filteredData.length > 0 && ( |
| 152 | + <> |
| 153 | + <div className={`summary-item ${darkMode ? 'summary-item-dark' : ''}`}> |
| 154 | + <div className={`summary-title ${darkMode ? 'summary-title-dark' : ''}`}> |
| 155 | + Most Popular Event |
| 156 | + </div> |
| 157 | + <div className={`summary-value ${darkMode ? 'summary-value-dark' : ''}`}> |
| 158 | + {mostPopularEvent.type || 'N/A'} |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + <div className={`summary-item ${darkMode ? 'summary-item-dark' : ''}`}> |
| 162 | + <div className={`summary-title ${darkMode ? 'summary-title-dark' : ''}`}> |
| 163 | + Least Popular Event |
| 164 | + </div> |
| 165 | + <div className={`summary-value ${darkMode ? 'summary-value-dark' : ''}`}> |
| 166 | + {leastPopularEvent.type || 'N/A'} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +} |
0 commit comments