@@ -65,6 +65,21 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
65
65
const userEmail = user ?. email ;
66
66
const [ openSnackbar , setOpenSnackbar ] = React . useState ( false ) ;
67
67
const [ snackbarMessage , setSnackbarMessage ] = React . useState ( "" ) ;
68
+ const [ remainingTime , setRemainingTime ] = React . useState ( 0 ) ; // State for the countdown timer
69
+ const [ timerId , setTimerId ] = React . useState < NodeJS . Timeout | null > ( null ) ; // State to keep track of the timer
70
+
71
+
72
+ const startCountdown = ( ) => {
73
+ setRemainingTime ( 30 ) ; // Start the timer at 30 seconds
74
+ setIsMatching ( true ) ;
75
+ // Clear any existing timer
76
+ if ( timerId ) clearInterval ( timerId ) ;
77
+ // Start a new timer
78
+ const id = setInterval ( ( ) => {
79
+ setRemainingTime ( ( time ) => time - 1 ) ;
80
+ } , 1000 ) ;
81
+ setTimerId ( id ) ;
82
+ } ;
68
83
69
84
const handleConnect = async ( ) => {
70
85
const preferences = {
@@ -88,8 +103,24 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
88
103
89
104
socket . emit ( "startMatching" , preferences ) ;
90
105
setIsMatching ( true ) ;
106
+ startCountdown ( ) ;
91
107
} ;
92
108
109
+ React . useEffect ( ( ) => {
110
+ if ( remainingTime === 0 && timerId ) {
111
+ clearInterval ( timerId ) ;
112
+ setIsMatching ( false ) ; // Update isMatching state to false as matching ends
113
+ setSnackbarMessage ( "Matching timed out after 30 seconds." ) ;
114
+ setOpenSnackbar ( true ) ;
115
+ }
116
+ } , [ remainingTime , timerId ] ) ;
117
+
118
+ React . useEffect ( ( ) => {
119
+ return ( ) => {
120
+ if ( timerId ) clearInterval ( timerId ) ;
121
+ } ;
122
+ } , [ timerId ] ) ;
123
+
93
124
const generateConsistentRandomIndex = ( seed : any , arrayLength : number ) => {
94
125
return seed % arrayLength ;
95
126
} ;
@@ -127,6 +158,8 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
127
158
getCategories ( ) ;
128
159
} , [ ] ) ;
129
160
161
+
162
+
130
163
React . useEffect ( ( ) => {
131
164
socket . on ( "matchFound" , async ( matchedUserPreferences ) => {
132
165
console . log ( "Match Found:" , matchedUserPreferences ) ;
@@ -203,7 +236,10 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
203
236
</ select >
204
237
</ div >
205
238
{ isMatching ? (
206
- < div > Loading...</ div >
239
+ < div > Loading...
240
+ < p > Time remaining: { remainingTime } seconds</ p >
241
+ </ div >
242
+
207
243
) : (
208
244
< Button sx = { { mt : "5%" } } variant = "contained" onClick = { handleConnect } >
209
245
Connect
@@ -229,4 +265,4 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
229
265
) ;
230
266
} ) ;
231
267
232
- export default MatchingForm ;
268
+ export default MatchingForm ;
0 commit comments