2323 < div class ="form-container ">
2424 < div class ="form-card " id ="registration-form ">
2525 < h1 class ="page-title "> Join the Leaderboard</ h1 >
26-
26+
2727 < p class ="text-center " style ="margin-bottom: 2rem; color: #ccc; font-size: 1.1rem; ">
2828 Register to start tracking your LeetCode progress and compete with your peers!
2929 </ p >
3030
3131 < form id ="registrationForm ">
3232 < div class ="form-group ">
3333 < label for ="name " class ="form-label "> Full Name</ label >
34- < input
35- type ="text "
36- id ="name "
37- name ="name "
38- class ="form-input "
39- placeholder ="Enter your full name "
34+ < input
35+ type ="text "
36+ id ="name "
37+ name ="name "
38+ class ="form-input "
39+ placeholder ="Enter your full name "
4040 required
4141 >
4242 </ div >
4343
4444 < div class ="form-group ">
4545 < label for ="leetcodeId " class ="form-label "> LeetCode Username</ label >
46- < input
47- type ="text "
48- id ="leetcodeId "
49- name ="leetcodeId "
50- class ="form-input "
51- placeholder ="Enter your LeetCode username "
46+ < input
47+ type ="text "
48+ id ="leetcodeId "
49+ name ="leetcodeId "
50+ class ="form-input "
51+ placeholder ="Enter your LeetCode username "
5252 required
5353 >
5454 < small class ="form-help ">
@@ -57,7 +57,7 @@ <h1 class="page-title">Join the Leaderboard</h1>
5757 </ div >
5858
5959 < div style ="text-align: center; ">
60- < button type ="submit " class ="btn btn-primary " style ="margin: 0 auto; ">
60+ < button id =" registerBtn " type ="submit " class ="btn btn-primary " style ="margin: 0 auto; ">
6161 Register Now
6262 </ button >
6363 </ div >
@@ -81,7 +81,7 @@ <h2 class="success-title">Registration Successful!</h2>
8181 function showSuccess ( name ) {
8282 document . getElementById ( 'registration-form' ) . classList . add ( 'hidden' ) ;
8383 document . getElementById ( 'success-message' ) . classList . remove ( 'hidden' ) ;
84- document . getElementById ( 'welcome-message' ) . innerHTML =
84+ document . getElementById ( 'welcome-message' ) . innerHTML =
8585 `Welcome to the CodePVG LeetCode Leaderboard, ${ name } !<br>
8686 Your name will appear on the leaderboard after the next auto-sync.<br>
8787 <small>(See the About section for details)</small>` ;
@@ -94,15 +94,14 @@ <h2 class="success-title">Registration Successful!</h2>
9494 }
9595
9696 function extractUsername ( input ) {
97-
9897 let value = input . trim ( ) ;
99-
98+
10099 if ( value . includes ( 'leetcode.com/' ) || value . includes ( '://' ) ) {
101100 const patterns = [
102101 / l e e t c o d e \. c o m \/ u \/ ( [ A - Z a - z 0 - 9 _ - ] + ) / i,
103102 / l e e t c o d e \. c o m \/ ( [ A - Z a - z 0 - 9 _ - ] + ) / i
104103 ] ;
105-
104+
106105 for ( const pattern of patterns ) {
107106 const match = value . match ( pattern ) ;
108107 if ( match && match [ 1 ] ) {
@@ -122,21 +121,22 @@ <h2 class="success-title">Registration Successful!</h2>
122121 "Content-Type" : "application/json"
123122 }
124123 } )
125- . then ( response => response . json ( ) )
126- . then ( data => {
127- const errors = data . errors ;
128- return ! ( errors && errors . some ( err => err . message === "That user does not exist." ) ) ;
129- } )
130- . catch ( error => {
131- console . error ( "Fetch error:" , error ) ;
132- return false ;
133- } ) ;
124+ . then ( response => response . json ( ) )
125+ . then ( data => {
126+ const errors = data . errors ;
127+ return ! ( errors && errors . some ( err => err . message === "That user does not exist." ) ) ;
128+ } )
129+ . catch ( error => {
130+ console . error ( "Fetch error:" , error ) ;
131+ return false ;
132+ } ) ;
134133 }
135134
136135 document . addEventListener ( 'DOMContentLoaded' , ( ) => {
137136 const form = document . getElementById ( 'registrationForm' ) ;
138137 const leetcodeInput = document . getElementById ( 'leetcodeId' ) ;
139-
138+ const registerBtn = document . getElementById ( 'registerBtn' ) ;
139+
140140 leetcodeInput . addEventListener ( 'paste' , ( e ) => {
141141 e . preventDefault ( ) ;
142142 const pastedText = ( e . clipboardData || window . clipboardData ) . getData ( 'text' ) ;
@@ -147,53 +147,60 @@ <h2 class="success-title">Registration Successful!</h2>
147147 leetcodeInput . addEventListener ( 'input' , ( e ) => {
148148 const cursorPosition = e . target . selectionStart ;
149149 const originalLength = e . target . value . length ;
150-
150+
151151 e . target . value = extractUsername ( e . target . value ) ;
152-
152+
153153 const newLength = e . target . value . length ;
154154 const diff = originalLength - newLength ;
155155 e . target . setSelectionRange ( cursorPosition - diff , cursorPosition - diff ) ;
156156 } ) ;
157-
157+
158158 form . addEventListener ( 'submit' , async ( e ) => {
159159 e . preventDefault ( ) ;
160-
160+
161+ registerBtn . disabled = true ;
162+ registerBtn . innerText = "Registering..." ;
163+
161164 const formData = new FormData ( form ) ;
162165 const name = formData . get ( 'name' ) ;
163166 const leetcodeId = formData . get ( 'leetcodeId' ) ;
164167
165- if ( name && leetcodeId ) {
166- const isValid = await isValidId ( leetcodeId ) ;
168+ try {
169+ if ( name && leetcodeId ) {
170+ const isValid = await isValidId ( leetcodeId ) ;
167171
168- if ( ! isValid ) {
169- alert ( "Please enter a correct LeetCode Username!" ) ;
170- return ;
171- }
172+ if ( ! isValid ) {
173+ alert ( "Please enter a correct LeetCode Username!" ) ;
174+ registerBtn . disabled = false ;
175+ registerBtn . innerText = "Register Now" ;
176+ return ;
177+ }
178+
179+ const response = await fetch ( "https://lc-backend-lyq2.onrender.com/user" , {
180+ method : "POST" ,
181+ headers : {
182+ "Content-Type" : "application/json"
183+ } ,
184+ body : JSON . stringify ( {
185+ name : name . trim ( ) ,
186+ id : leetcodeId . trim ( )
187+ } )
188+ } ) ;
172189
173- fetch ( "https://lc-backend-lyq2.onrender.com/user" , {
174- method : "POST" ,
175- headers : {
176- "Content-Type" : "application/json"
177- } ,
178- body : JSON . stringify ( {
179- name : name . trim ( ) ,
180- id : leetcodeId . trim ( )
181- } )
182- } )
183- . then ( response => {
184190 if ( ! response . ok ) {
185191 throw new Error ( "Failed to register user" ) ;
186192 }
187- return response . json ( ) ;
188- } )
189- . then ( data => {
193+
194+ const data = await response . json ( ) ;
190195 console . log ( "User registered successfully:" , data ) ;
191196 showSuccess ( name ) ;
192- } )
193- . catch ( error => {
194- alert ( "An error occurred during registration. Please try again." ) ;
195- console . error ( error ) ;
196- } ) ;
197+ }
198+ } catch ( error ) {
199+ alert ( "An error occurred during registration. Please try again." ) ;
200+ console . error ( error ) ;
201+ } finally {
202+ registerBtn . disabled = false ;
203+ registerBtn . innerText = "Register Now" ;
197204 }
198205 } ) ;
199206 } ) ;
0 commit comments