@@ -5,34 +5,63 @@ import TopicProgress from './topicProgress';
55import styles from './progressCard.module.css' ;
66import ArrowDownIcon from '../../assets/arrow-down-icon.svg' ; // Example icon
77import ArrowUpIcon from '../../assets/arrow-up-icon.svg' ; // Example icon
8+ import useAuth from '@/hooks/useAuth' ;
9+ import { useQuery } from '@tanstack/react-query' ;
10+ import { getAllAttemptSummaries , getHistoryProgress } from '@/lib/api' ;
811
9- // Placeholder data TODO: replace with data fetched from API
10- const placeholderProgress = {
11- total_sessions_completed : 18 ,
12- total_successes : 15 ,
13- total_time_ms : 3600000 , // 1 hour in milliseconds
14- current_streak : 24 ,
15- } ;
12+ const ProgressCard = ( ) => {
13+ const [ isOpen , setIsOpen ] = useState ( false ) ; // Default to false
14+ const { user} = useAuth ( ) ;
1615
17- const placeholderRecentSessions = [
18- { id : 1 , user : 'Jane ', difficulty : 'Easy' , duration : 45 , status : 'Completed' } ,
19- { id : 2 , user : 'Thomas' , difficulty : 'Medium' , duration : 60 , status : 'Passed' } , // Assuming 'Passed' is a status
20- { id : 3 , user : 'Alex' , difficulty : 'Hard' , duration : 500 , status : 'Incomplete' } ,
21- ] ;
16+ const { data : userProgress , isLoading } = useQuery ( {
17+ queryKey : [ 'userProgress ', user ?. _id ] ,
18+ queryFn : ( ) => getHistoryProgress ( user ?. _id ) ,
19+ enabled : ! ! user ?. _id ,
20+ } ) ;
2221
23- const placeholderTopicProgress = {
24- 'Arrays' : 4 ,
25- 'Strings' : 6 ,
26- 'Binary Search' : 18 ,
27- 'Trees & Graphs' : 2 ,
28- 'Linked Lists' : 3 ,
29- 'Stack & Queue' : 1 ,
30- 'Hash Tables' : 2 ,
31- } ;
22+ const stats = userProgress ? {
23+ totalSessions : userProgress . total_sessions ,
24+ completed : userProgress . total_sessions_completed ,
25+ successRate : Math . round ( userProgress . success_rate * 100 ) , // Decimal to percentage
26+ dayStreak : userProgress . current_streak ,
27+ } : {
28+ totalSessions : 0 ,
29+ completed : 0 ,
30+ successRate : 0 ,
31+ dayStreak : 0 ,
32+ } ;
3233
34+ const { data : allSessions , isLoading : isLoadingSessions } = useQuery ( {
35+ queryKey : [ 'recentSessions' , user ?. _id ] ,
36+ queryFn : ( ) => getAllAttemptSummaries ( user ?. _id ) ,
37+ enabled : ! ! user ?. _id ,
38+ retry : 1 ,
39+ } ) ;
40+
41+ const recentSessions = allSessions ?. slice ( 0 , 3 ) . map ( ( session , index ) => ( {
42+ id : index + 1 ,
43+ user : session . partner_id ,
44+ difficulty : session . question_difficulty ,
45+ duration : session . time_taken_ms ? Math . round ( session . time_taken_ms / 60000 ) : 0 , // convert ms to minutes
46+ status : session . is_solved_successfully ? 'Passed' : 'Failed' ,
47+ } ) ) || [ ] ;
3348
34- const ProgressCard = ( ) => {
35- const [ isOpen , setIsOpen ] = useState ( false ) ; // Default to false
49+
50+ const topicProgress = allSessions ?. reduce ( ( acc , session ) => {
51+ session . question_topics . forEach ( topic => {
52+ acc [ topic ] = ( acc [ topic ] || 0 ) + 1 ;
53+ } ) ;
54+ return acc ;
55+ } , { } as Record < string , number > ) || { } ;
56+
57+ console . log ( '🔍 RAW allSessions from API:' , allSessions ) ;
58+ if ( allSessions && allSessions . length > 0 ) {
59+ console . log ( '🔍 First session question_topics:' , allSessions [ 0 ] . question_topics ) ;
60+ console . log ( '🔍 Type of question_topics:' , typeof allSessions [ 0 ] . question_topics ) ;
61+ console . log ( '🔍 Is Array?:' , Array . isArray ( allSessions [ 0 ] . question_topics ) ) ;
62+ }
63+
64+
3665
3766 return (
3867 < div className = { styles . progressCard } >
@@ -51,10 +80,10 @@ const ProgressCard = () => {
5180 { isOpen && (
5281 < div className = { styles . content } >
5382 < div className = { styles . topRow } >
54- < StatsGrid progress = { placeholderProgress } />
55- < RecentSessions sessions = { placeholderRecentSessions } />
83+ < StatsGrid stats = { stats } />
84+ < RecentSessions sessions = { recentSessions } />
5685 </ div >
57- < TopicProgress progressData = { placeholderTopicProgress } />
86+ < TopicProgress progressData = { topicProgress } />
5887 </ div >
5988 ) }
6089 </ div >
0 commit comments