@@ -2,16 +2,8 @@ import React, { useEffect, useState } from "react";
2
2
import { Table , Tag , Typography , Badge , Button , message , Empty } from 'antd' ;
3
3
import type { ColumnsType } from 'antd/es/table' ;
4
4
import styles from "./RecentAttemptsTable.module.css" ;
5
- import AuthClientStore from "data/auth/AuthClientStore" ;
6
-
7
- interface HistoryEntry {
8
- id : string ;
9
- key : string ;
10
- date : string ;
11
- title : string ;
12
- difficulty : 'Easy' | 'Medium' | 'Hard' ;
13
- topics : string [ ] ;
14
- }
5
+ import { HistoryEntry } from "domain/entities/HistoryEntry" ;
6
+ import { historyUseCases } from "domain/usecases/HistoryUseCases" ;
15
7
16
8
export const RecentAttemptsTable : React . FC = ( ) => {
17
9
const [ recentAttemptsData , setRecentAttemptsData ] = useState < HistoryEntry [ ] > ( [ ] ) ;
@@ -25,33 +17,8 @@ export const RecentAttemptsTable: React.FC = () => {
25
17
const fetchRecentAttempts = async ( ) => {
26
18
setLoading ( true ) ;
27
19
try {
28
- const token = AuthClientStore . getAccessToken ( ) ;
29
- const response = await fetch ( 'http://localhost:3002/api/history' , {
30
- method : 'GET' ,
31
- headers : {
32
- 'Content-Type' : 'application/json' ,
33
- 'Authorization' : `Bearer ${ token } ` ,
34
- } ,
35
- } ) ;
36
-
37
- if ( ! response . ok ) {
38
- throw new Error ( `Error fetching data: ${ response . statusText } ` ) ;
39
- }
40
-
41
- const data = await response . json ( ) ;
42
- console . log ( data ) ;
43
-
44
- // Transform data to match RecentAttempt interface
45
- const formattedData : HistoryEntry [ ] = data . map ( ( entry : any ) => ( {
46
- key : entry . _id ,
47
- id : entry . _id ,
48
- date : new Date ( entry . attemptStartedAt ) . toLocaleDateString ( ) ,
49
- title : entry . question . title || 'Unknown Title' ,
50
- difficulty : entry . question . difficulty || 'Easy' ,
51
- topics : entry . question . categories . map ( ( cat : any ) => cat . name ) || [ ] ,
52
- } ) ) ;
53
-
54
- setRecentAttemptsData ( formattedData ) ;
20
+ const data = await historyUseCases . getAllCategories ( ) ;
21
+ setRecentAttemptsData ( data ) ;
55
22
} catch ( error ) {
56
23
if ( error instanceof Error ) {
57
24
console . error ( "Failed to fetch recent attempts:" , error . message ) ;
@@ -65,22 +32,9 @@ export const RecentAttemptsTable: React.FC = () => {
65
32
}
66
33
} ;
67
34
68
- // Function to handle clearing all attempts
69
35
const handleClearAllAttempts = async ( ) => {
70
36
try {
71
- const token = AuthClientStore . getAccessToken ( ) ;
72
- const response = await fetch ( 'http://localhost:3002/api/history/all' , {
73
- method : 'DELETE' ,
74
- headers : {
75
- 'Content-Type' : 'application/json' ,
76
- 'Authorization' : `Bearer ${ token } ` ,
77
- } ,
78
- } ) ;
79
-
80
- if ( ! response . ok ) {
81
- throw new Error ( `Error clearing attempts: ${ response . statusText } ` ) ;
82
- }
83
-
37
+ await historyUseCases . deleteAllUserHistories ( ) ;
84
38
message . success ( "All attempts cleared successfully" ) ;
85
39
setRecentAttemptsData ( [ ] ) ;
86
40
} catch ( error ) {
@@ -94,31 +48,14 @@ export const RecentAttemptsTable: React.FC = () => {
94
48
}
95
49
} ;
96
50
97
- // Function to handle deleting selected attempts
98
51
const handleDeleteSelectedAttempts = async ( ) => {
99
52
try {
100
53
if ( selectedRowKeys . length === 0 ) {
101
54
message . info ( "No attempts selected" ) ;
102
55
return ;
103
56
}
104
-
105
- const token = AuthClientStore . getAccessToken ( ) ;
106
- const response = await fetch ( 'http://localhost:3002/api/history' , {
107
- method : 'DELETE' ,
108
- headers : {
109
- 'Content-Type' : 'application/json' ,
110
- 'Authorization' : `Bearer ${ token } ` ,
111
- } ,
112
- body : JSON . stringify ( { ids : selectedRowKeys } ) ,
113
- } ) ;
114
-
115
- if ( ! response . ok ) {
116
- throw new Error ( `Error deleting attempts: ${ response . statusText } ` ) ;
117
- }
118
-
57
+ await historyUseCases . deleteSelectedUserHistories ( selectedRowKeys . map ( ( key ) => key . toString ( ) ) ) ;
119
58
message . success ( `${ selectedRowKeys . length } attempt(s) deleted successfully` ) ;
120
-
121
- // Remove deleted attempts from the state
122
59
setRecentAttemptsData ( ( prevData ) =>
123
60
prevData . filter ( ( attempt ) => ! selectedRowKeys . includes ( attempt . key ) )
124
61
) ;
@@ -139,7 +76,7 @@ export const RecentAttemptsTable: React.FC = () => {
139
76
title : 'Date' ,
140
77
dataIndex : 'date' ,
141
78
key : 'date' ,
142
- sorter : ( a , b ) => new Date ( a . date ) . getTime ( ) - new Date ( b . date ) . getTime ( ) ,
79
+ sorter : ( a , b ) => new Date ( a . attemptCompletedAt ) . getTime ( ) - new Date ( b . attemptCompletedAt ) . getTime ( ) ,
143
80
} ,
144
81
{
145
82
title : 'Title' ,
0 commit comments