1- import React , { useState } from 'react' ;
2- import { CheckCircle , ChevronLeft , ChevronRight } from 'lucide-react' ;
1+ import React , { useState , useEffect } from 'react' ;
2+ import { CheckCircle , ChevronLeft , ChevronRight } from 'lucide-react' ;
3+
34import { useFetchTaskAssignments } from '@/app/hooks/useFetchTaskAssignment' ;
45
56const TaskSummary = ( ) => {
67 const { assignedTasks, loading, error } = useFetchTaskAssignments ( ) ;
78 const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
8- const tasksPerPage = 5 ;
9+ const tasksPerPage = 4 ;
910
1011 if ( error ) {
1112 return < div > Error: { error . message } </ div > ;
1213 }
13- const completedTasks = assignedTasks . filter ( task => task . status === 'completed' ) ;
14+
15+ const completedTasks = Array . from (
16+ new Map (
17+ assignedTasks
18+ . filter ( task => task . status === 'completed' )
19+ . map ( task => [ task . id , task ] )
20+ ) . values ( )
21+ ) ;
22+
1423 const totalTasks = assignedTasks . length ;
1524 const completedPercentage = totalTasks > 0 ? Math . round ( ( completedTasks . length / totalTasks ) * 100 ) : 0 ;
25+
26+ const totalPages = Math . max ( 1 , Math . ceil ( completedTasks . length / tasksPerPage ) ) ;
27+
28+ useEffect ( ( ) => {
29+ if ( currentPage > totalPages ) setCurrentPage ( totalPages ) ;
30+ } , [ completedTasks . length , totalPages ] ) ;
31+
1632 const indexOfLastTask = currentPage * tasksPerPage ;
1733 const indexOfFirstTask = indexOfLastTask - tasksPerPage ;
1834 const currentTasks = completedTasks . slice ( indexOfFirstTask , indexOfLastTask ) ;
19- const totalPages = Math . ceil ( completedTasks . length / tasksPerPage ) ;
35+
2036 const handleNextPage = ( ) => {
21- if ( currentPage < totalPages ) {
22- setCurrentPage ( currentPage + 1 ) ;
23- }
37+ if ( currentPage < totalPages ) setCurrentPage ( currentPage + 1 ) ;
2438 } ;
2539 const handlePrevPage = ( ) => {
26- if ( currentPage > 1 ) {
27- setCurrentPage ( currentPage - 1 ) ;
28- }
40+ if ( currentPage > 1 ) setCurrentPage ( currentPage - 1 ) ;
2941 } ;
42+
3043 return (
31- < div className = "w-full h-full bg-[#F3FBFD] rounded-2xl shadow-[12px_12px_32px_rgba(0,0,0,0.15)] px-5 sm:px-7 py-8" >
44+ < div className = "w-full h-full bg-[#F3FBFD] rounded-2xl shadow-[12px_12px_32px_rgba(0,0,0,0.15)] px-5 sm:px-7 py-8" >
3245 < h2 className = "text-2xl font-medium text-[#00353D] mb-6" > Task Summary</ h2 >
3346 < div >
3447 < div className = "flex justify-between items-center mb-2" >
3548 < span className = "text-lg font-medium text-[#00353D]" > { completedTasks . length } /{ totalTasks } Tasks Completed</ span >
3649 < span className = "text-lg font-medium text-[#00353D]" > { completedPercentage } %</ span >
3750 </ div >
3851 < div className = "w-full bg-gray-200 rounded-full h-4" >
39- < div
40- className = "bg-[#8BB2B5] h-4 rounded-full"
41- style = { { width : `${ completedPercentage } %` } }
42- > </ div >
52+ < div className = "bg-[#8BB2B5] h-4 rounded-full" style = { { width : `${ completedPercentage } %` } } > </ div >
4353 </ div >
4454 </ div >
4555 < div className = "mt-8" >
4656 < h3 className = "text-xl font-medium text-[#00353D] mb-4" > Recently Completed</ h3 >
4757 < ul className = "space-y-4" >
48- { currentTasks . map ( ( task ) => (
49- < li key = { task . id } className = "flex items-center p-4 bg-white rounded-lg shadow-sm" >
50- < CheckCircle className = "w-6 h-6 text-green-500 mr-4" />
51- < div className = "flex-1" >
52- < p className = "font-medium text-[#00353D]" > { task . title } </ p >
53- </ div >
54- </ li >
55- ) ) }
58+ { currentTasks . length === 0 ? (
59+ < li className = "text-[#00353D]" > No completed tasks yet.</ li >
60+ ) : (
61+ currentTasks . map ( ( task ) => (
62+ < li key = { task . id } className = "flex items-center p-4 bg-white rounded-lg shadow-sm" >
63+ < CheckCircle className = "w-6 h-6 text-green-500 mr-4" />
64+ < div className = "flex-1" >
65+ < p className = "font-medium text-[#00353D]" > { task . title } </ p >
66+ </ div >
67+ </ li >
68+ ) )
69+ ) }
5670 </ ul >
5771 </ div >
5872 < div className = "mt-8 flex justify-center items-center space-x-4" >
5973 < button
6074 onClick = { handlePrevPage }
6175 disabled = { currentPage === 1 }
62- className = "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-[#C3A041] hover:bg-[#B5913A] disabled:bg-gray-400"
76+ className = "inline-flex items-center cursor-pointer px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-[#C3A041] hover:bg-[#B5913A] disabled:bg-gray-400"
6377 >
6478 < ChevronLeft className = "w-5 h-5" />
6579 Previous
@@ -68,7 +82,7 @@ const TaskSummary = () => {
6882 < button
6983 onClick = { handleNextPage }
7084 disabled = { currentPage === totalPages }
71- className = "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-[#C3A041] hover:bg-[#B5913A] disabled:bg-gray-400"
85+ className = "inline-flex items-center cursor-pointer px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-[#C3A041] hover:bg-[#B5913A] disabled:bg-gray-400"
7286 >
7387 Next
7488 < ChevronRight className = "w-5 h-5" />
@@ -77,4 +91,5 @@ const TaskSummary = () => {
7791 </ div >
7892 ) ;
7993} ;
94+
8095export default TaskSummary ;
0 commit comments