@@ -10,8 +10,21 @@ function App() {
10
10
const [ input , setInput ] = useState ( "" ) ;
11
11
const [ loading , setLoading ] = useState ( true ) ;
12
12
const [ error , setError ] = useState < string | null > ( null ) ;
13
+ const [ errorCount , setErrorCount ] = useState ( 1 ) ;
13
14
const [ onlineUsers , setOnlineUsers ] = useState < number > ( 1 ) ;
14
15
16
+ // Function to handle errors with counter
17
+ const handleError = ( errorMessage : string ) => {
18
+ if ( error === errorMessage ) {
19
+ // Same error occurred again, increment counter
20
+ setErrorCount ( prev => prev + 1 ) ;
21
+ } else {
22
+ // New error, reset counter and set new error
23
+ setError ( errorMessage ) ;
24
+ setErrorCount ( 1 ) ;
25
+ }
26
+ } ;
27
+
15
28
// Calculate progress
16
29
const totalTodos = todos . length ;
17
30
const completedTodos = todos . filter ( todo => todo . completed ) . length ;
@@ -27,8 +40,9 @@ function App() {
27
40
// Check if the response is an object with a todos property
28
41
setTodos ( Array . isArray ( data ) ? data : ( data . todos || [ ] ) ) ;
29
42
setError ( null ) ;
43
+ setErrorCount ( 1 ) ;
30
44
} catch ( err ) {
31
- setError ( "Failed to fetch todos. Please try again." ) ;
45
+ handleError ( "Failed to fetch todos. Please try again." ) ;
32
46
console . error ( err ) ;
33
47
} finally {
34
48
setLoading ( false ) ;
@@ -41,20 +55,28 @@ function App() {
41
55
socketService . connect ( ) ;
42
56
43
57
// Listen for todo updates
44
- const unsubscribe = socketService . on ( 'todos:update' , ( updatedTodos : Todo [ ] ) => {
45
- setTodos ( updatedTodos ) ;
58
+ const unsubscribeTodos = socketService . on ( 'todos:update' , ( updatedTodos : Todo [ ] | { todos : Todo [ ] } ) => {
59
+ console . log ( 'Received todos update:' , updatedTodos ) ;
60
+ if ( Array . isArray ( updatedTodos ) ) {
61
+ setTodos ( updatedTodos ) ;
62
+ } else if ( updatedTodos && 'todos' in updatedTodos ) {
63
+ setTodos ( updatedTodos . todos ) ;
64
+ }
46
65
} ) ;
47
66
48
67
// Listen for user count updates
49
- socketService . on ( 'users:count' , ( count : number ) => {
68
+ const unsubscribeUsers = socketService . on ( 'users:count' , ( count : number ) => {
69
+ console . log ( 'Received users count update:' , count ) ;
50
70
setOnlineUsers ( count ) ;
51
71
} ) ;
52
72
53
73
// Cleanup on unmount
54
74
return ( ) => {
55
- unsubscribe ( ) ;
75
+ unsubscribeTodos ( ) ;
76
+ unsubscribeUsers ( ) ;
56
77
socketService . disconnect ( ) ;
57
78
} ;
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
58
80
} , [ ] ) ;
59
81
60
82
const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
@@ -64,7 +86,7 @@ function App() {
64
86
await createTodo ( input . trim ( ) ) ;
65
87
setInput ( "" ) ;
66
88
} catch ( err ) {
67
- setError ( "Failed to create todo. Please try again." ) ;
89
+ handleError ( "Failed to create todo. Please try again." ) ;
68
90
console . error ( err ) ;
69
91
}
70
92
}
@@ -86,7 +108,7 @@ function App() {
86
108
87
109
await updateTodo ( id , { completed : ! todoToUpdate . completed } ) ;
88
110
} catch ( err ) {
89
- setError ( "Failed to update todo. Please try again." ) ;
111
+ handleError ( "Failed to update todo. Please try again." ) ;
90
112
console . error ( err ) ;
91
113
}
92
114
} ;
@@ -95,7 +117,7 @@ function App() {
95
117
try {
96
118
await deleteTodo ( id ) ;
97
119
} catch ( err ) {
98
- setError ( "Failed to delete todo. Please try again." ) ;
120
+ handleError ( "Failed to delete todo. Please try again." ) ;
99
121
console . error ( err ) ;
100
122
}
101
123
} ;
@@ -137,8 +159,13 @@ function App() {
137
159
< h2 className = "text-xl font-bold mb-4 text-white" > Your Tasks</ h2 >
138
160
139
161
{ error && (
140
- < div className = "bg-red-900/50 border border-red-700 text-white p-3 rounded mb-4" >
141
- { error }
162
+ < div className = "bg-red-900/50 border border-red-700 text-white p-3 rounded mb-4 flex justify-between items-center" >
163
+ < span > { error } </ span >
164
+ { errorCount > 1 && (
165
+ < span className = "bg-red-700 text-white text-xs font-medium px-2 py-1 rounded-full" >
166
+ { errorCount }
167
+ </ span >
168
+ ) }
142
169
</ div >
143
170
) }
144
171
0 commit comments