99
99
display : none;
100
100
}
101
101
}
102
+
103
+ .settings-form {
104
+ margin-top : 20px ;
105
+ text-align : center;
106
+ }
107
+
108
+ .settings-form input {
109
+ padding : 10px ;
110
+ margin : 5px ;
111
+ font-size : 18px ;
112
+ }
113
+
114
+ .settings-form button {
115
+ padding : 10px 20px ;
116
+ background-color : # 50e3c2 ;
117
+ color : white;
118
+ border : none;
119
+ cursor : pointer;
120
+ }
121
+
122
+ .settings-form button : hover {
123
+ background-color : # 4ba99d ;
124
+ }
102
125
</ style >
103
126
</ head >
104
127
< body >
105
128
< nav class ="menu-container ">
106
- <!-- Logo -->
107
129
< a href ="https://scratch-coding-hut.github.io " class ="menu-logo ">
108
130
< img src ="Images/Coding-Hut-Logo.png " alt ="Coding Hut Logo " width ="50 " height ="50 " />
109
131
</ a >
110
132
111
- <!-- Menu for Mobile -->
112
133
< span class ="hamburger " onclick ="toggleMenu() "> ☰</ span >
113
134
114
- <!-- Menu Items -->
115
135
< div class ="menu ">
116
136
< ul >
117
137
< li > < a href ="https://scratch-coding-hut.github.io "> Home</ a > </ li >
@@ -130,8 +150,16 @@ <h1 id="user" style="text-align: center"><b>Coding Hut</b></h1>
130
150
< h2 style ="text-align: center "> Welcome to your dashboard</ h2 >
131
151
< p id ="data " style ="text-align: center "> COMING SOON</ p >
132
152
153
+ < div class ="settings-form ">
154
+ < h3 > Update Your Settings</ h3 >
155
+ < form id ="settings-form ">
156
+ < input type ="email " id ="email " placeholder ="Update your email " />
157
+ < input type ="number " id ="points " placeholder ="Update your points " />
158
+ < button type ="submit "> Save Settings</ button >
159
+ </ form >
160
+ </ div >
161
+
133
162
< script >
134
- // Fetch user data and update UI
135
163
async function loadUserData ( ) {
136
164
const username = localStorage . getItem ( "username" ) ;
137
165
@@ -152,13 +180,52 @@ <h2 style="text-align: center">Welcome to your dashboard</h2>
152
180
}
153
181
}
154
182
155
- // Menu toggle for mobile
183
+ async function updateUserSettings ( event ) {
184
+ event . preventDefault ( ) ;
185
+
186
+ const username = localStorage . getItem ( "username" ) ;
187
+ const email = document . getElementById ( "email" ) . value ;
188
+ const points = document . getElementById ( "points" ) . value ;
189
+
190
+ if ( ! email && ! points ) {
191
+ alert ( "Please enter at least one value to update." ) ;
192
+ return ;
193
+ }
194
+
195
+ const updatedData = { } ;
196
+ if ( email ) updatedData . email = email ;
197
+ if ( points ) updatedData . points = parseInt ( points ) ;
198
+
199
+ try {
200
+ const res = await fetch ( `https://scratch-coding-hut-data.onrender.com/${ username } ` , {
201
+ method : "PUT" ,
202
+ headers : {
203
+ "Content-Type" : "application/json" ,
204
+ } ,
205
+ body : JSON . stringify ( updatedData ) ,
206
+ } ) ;
207
+
208
+ const data = await res . json ( ) ;
209
+
210
+ if ( data ) {
211
+ document . getElementById ( "data" ) . textContent = JSON . stringify ( data ) ;
212
+ alert ( "Settings updated successfully!" ) ;
213
+ } else {
214
+ alert ( "Error updating settings." ) ;
215
+ }
216
+ } catch ( error ) {
217
+ console . error ( "Error updating data:" , error ) ;
218
+ alert ( "Failed to update settings." ) ;
219
+ }
220
+ }
221
+
222
+ document . getElementById ( "settings-form" ) . addEventListener ( "submit" , updateUserSettings ) ;
223
+
156
224
function toggleMenu ( ) {
157
225
var menu = document . querySelector ( ".menu" ) ;
158
226
menu . style . display = menu . style . display === "flex" ? "none" : "flex" ;
159
227
}
160
228
161
- // Load user data on page load
162
229
document . addEventListener ( "DOMContentLoaded" , loadUserData ) ;
163
230
</ script >
164
231
</ body >
0 commit comments