@@ -13,6 +13,7 @@ const AttendanceDashboard = () => {
1313 const [ filterActive , setFilterActive ] = useState ( false ) ;
1414 const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
1515 const [ recordsPerPage ] = useState ( 10 ) ;
16+ const [ showingAbsences , setShowingAbsences ] = useState ( false ) ;
1617
1718 // Fetch all attendance data when component mounts
1819 useEffect ( ( ) => {
@@ -24,6 +25,7 @@ const AttendanceDashboard = () => {
2425 try {
2526 setLoading ( true ) ;
2627 setFilterActive ( false ) ;
28+ setShowingAbsences ( false ) ;
2729 const response = await axiosInstance . get ( '/hr/attendance' ) ;
2830 setAttendanceData ( response . data ) ;
2931 setLoading ( false ) ;
@@ -44,6 +46,7 @@ const AttendanceDashboard = () => {
4446 try {
4547 setLoading ( true ) ;
4648 setFilterActive ( true ) ;
49+ setShowingAbsences ( false ) ;
4750
4851 const response = await axiosInstance . get ( `/hr/attendance/employee/${ employeeId } ` ) ;
4952 setAttendanceData ( response . data ) ;
@@ -65,6 +68,7 @@ const AttendanceDashboard = () => {
6568 try {
6669 setLoading ( true ) ;
6770 setFilterActive ( true ) ;
71+ setShowingAbsences ( false ) ;
6872 const response = await axiosInstance . get ( '/hr/attendance/daterange' , {
6973 params : { startDate, endDate }
7074 } ) ;
@@ -77,12 +81,29 @@ const AttendanceDashboard = () => {
7781 }
7882 } ;
7983
84+ // Function to filter and show only absences
85+ const showOnlyAbsences = ( ) => {
86+ setLoading ( true ) ;
87+ setShowingAbsences ( true ) ;
88+
89+ // Filter the current data to show only absences
90+ // This avoids making another API call if we already have the data
91+ const filteredData = attendanceData . filter ( record =>
92+ record . status === 'Absent'
93+ ) ;
94+
95+ setAttendanceData ( filteredData ) ;
96+ setCurrentPage ( 1 ) ; // Reset to first page
97+ setLoading ( false ) ;
98+ } ;
99+
80100 // Function to reset filters
81101 const resetFilters = ( ) => {
82102 setStartDate ( '' ) ;
83103 setEndDate ( '' ) ;
84104 setEmployeeId ( '' ) ;
85105 setError ( null ) ;
106+ setShowingAbsences ( false ) ;
86107 fetchAllAttendance ( ) ;
87108 } ;
88109
@@ -98,6 +119,46 @@ const AttendanceDashboard = () => {
98119 return timeString ;
99120 } ;
100121
122+ // Function to generate pagination numbers with ellipses
123+ const getPaginationNumbers = ( currentPage , totalPages ) => {
124+ // Always show first page, last page, current page, and pages adjacent to current page
125+ const pageNumbers = [ ] ;
126+
127+ if ( totalPages <= 7 ) {
128+ // If we have 7 or fewer pages, show all page numbers
129+ for ( let i = 1 ; i <= totalPages ; i ++ ) {
130+ pageNumbers . push ( i ) ;
131+ }
132+ } else {
133+ // Always add first page
134+ pageNumbers . push ( 1 ) ;
135+
136+ // Determine start and end of page numbers around current page
137+ let startPage = Math . max ( 2 , currentPage - 1 ) ;
138+ let endPage = Math . min ( totalPages - 1 , currentPage + 1 ) ;
139+
140+ // Add ellipsis if needed before the middle pages
141+ if ( startPage > 2 ) {
142+ pageNumbers . push ( 'ellipsis-start' ) ;
143+ }
144+
145+ // Add middle pages
146+ for ( let i = startPage ; i <= endPage ; i ++ ) {
147+ pageNumbers . push ( i ) ;
148+ }
149+
150+ // Add ellipsis if needed after the middle pages
151+ if ( endPage < totalPages - 1 ) {
152+ pageNumbers . push ( 'ellipsis-end' ) ;
153+ }
154+
155+ // Always add last page
156+ pageNumbers . push ( totalPages ) ;
157+ }
158+
159+ return pageNumbers ;
160+ } ;
161+
101162 // Pagination calculation
102163 const indexOfLastRecord = currentPage * recordsPerPage ;
103164 const indexOfFirstRecord = indexOfLastRecord - recordsPerPage ;
@@ -125,6 +186,15 @@ const AttendanceDashboard = () => {
125186 < div className = "attendance-dashboard" >
126187 < header className = "dashboard-header" >
127188 < h1 > Attendance Dashboard</ h1 >
189+ < div className = "action-buttons" >
190+ < button
191+ className = { `absence-button ${ showingAbsences ? 'active' : '' } ` }
192+ onClick = { showOnlyAbsences }
193+ disabled = { loading || attendanceData . length === 0 }
194+ >
195+ Show Absences Only
196+ </ button >
197+ </ div >
128198 </ header >
129199
130200 < div className = "filter-section" >
@@ -182,7 +252,7 @@ const AttendanceDashboard = () => {
182252 </ div >
183253 </ div >
184254
185- { filterActive && (
255+ { ( filterActive || showingAbsences ) && (
186256 < button className = "reset-button" onClick = { resetFilters } >
187257 Reset Filters
188258 </ button >
@@ -192,12 +262,21 @@ const AttendanceDashboard = () => {
192262 { error && < div className = "error-message" > { error } </ div > }
193263
194264 < div className = "attendance-data" >
195- < h2 > Attendance Records</ h2 >
265+ < h2 >
266+ { showingAbsences
267+ ? 'Absence Records'
268+ : 'Attendance Records' }
269+ { showingAbsences && < span className = "record-count" > ({ attendanceData . length } )</ span > }
270+ </ h2 >
196271
197272 { loading ? (
198273 < div className = "loading" > Loading attendance data...</ div >
199274 ) : attendanceData . length === 0 ? (
200- < div className = "no-data" > No attendance records found</ div >
275+ < div className = "no-data" >
276+ { showingAbsences
277+ ? 'No absence records found'
278+ : 'No attendance records found' }
279+ </ div >
201280 ) : (
202281 < >
203282 < div className = "table-container" >
@@ -248,14 +327,20 @@ const AttendanceDashboard = () => {
248327 Previous
249328 </ button >
250329 < div className = "pagination-pages" >
251- { [ ...Array ( totalPages ) . keys ( ) ] . map ( number => (
252- < button
253- key = { number + 1 }
254- onClick = { ( ) => paginate ( number + 1 ) }
255- className = { `pagination-number ${ currentPage === number + 1 ? 'active' : '' } ` }
256- >
257- { number + 1 }
258- </ button >
330+ { getPaginationNumbers ( currentPage , totalPages ) . map ( ( number , index ) => (
331+ number === 'ellipsis-start' || number === 'ellipsis-end' ? (
332+ < span key = { `ellipsis-${ index } ` } className = "pagination-ellipsis" >
333+ …
334+ </ span >
335+ ) : (
336+ < button
337+ key = { number }
338+ onClick = { ( ) => paginate ( number ) }
339+ className = { `pagination-number ${ currentPage === number ? 'active' : '' } ` }
340+ >
341+ { number }
342+ </ button >
343+ )
259344 ) ) }
260345 </ div >
261346 < button
0 commit comments