@@ -190,9 +190,6 @@ <h2>Create a Wiki Post</h2>
190
190
</ form >
191
191
</ div >
192
192
193
- < div class ="wikis-container ">
194
- < b > Make sure to read and follow the < a href ="https://scratch-coding-hut.netlify.app/wiki/wiki-guidelines "> Scratch Coding Hut Wiki Guidelines</ a > </ b >
195
-
196
193
< div class ="wikis-container ">
197
194
< h2 > Wiki Posts</ h2 >
198
195
< ul id ="wikiList "> </ ul >
@@ -204,13 +201,9 @@ <h2>Wiki Posts</h2>
204
201
const wikiList = document . getElementById ( 'wikiList' ) ;
205
202
const backendUrl = 'https://thejsurlback.onrender.com/api' ; // Replace with your backend URL
206
203
207
- // Retrieve username from localStorage
204
+ // Retrieve username from localStorage (this is optional, users can view wikis when logged out)
208
205
const username = localStorage . getItem ( 'username' ) ;
209
- if ( ! username ) {
210
- alert ( 'Sorry, You must be logged in to interact with the wiki. Go to https://scratch-coding-hut.netlify.app/account to log in.' ) ;
211
- return ; // Stop execution if no username is found
212
- }
213
-
206
+
214
207
// Fetch and display all wikis
215
208
const fetchWikis = async ( ) => {
216
209
const response = await fetch ( `${ backendUrl } /wikis` ) ;
@@ -224,8 +217,8 @@ <h3>${wiki.title}</h3>
224
217
<p>${ wiki . content } </p>
225
218
<small>Owner: ${ wiki . owner } </small>
226
219
<div class="button-container">
227
- ${ isOwnerOrAdmin ( wiki . owner ) ? `<button class="edit" onclick="editWiki(${ wiki . id } , '${ wiki . title } ', ' ${ wiki . content } ')">Edit</button>` : '' }
228
- ${ isOwnerOrAdmin ( wiki . owner ) ? `<button class="delete" onclick="deleteWiki(${ wiki . id } )">Delete</button>` : '' }
220
+ ${ username && isOwnerOrAdmin ( wiki . owner ) ? `<button class="edit" onclick="editWiki('${ wiki . title } ')">Edit</button>` : '' }
221
+ ${ username && isOwnerOrAdmin ( wiki . owner ) ? `<button class="delete" onclick="deleteWiki(${ wiki . id } )">Delete</button>` : '' }
229
222
</div>
230
223
` ;
231
224
wikiList . appendChild ( li ) ;
@@ -274,30 +267,15 @@ <h3>${wiki.title}</h3>
274
267
}
275
268
} ;
276
269
277
- // Edit a wiki
278
- window . editWiki = async ( id , title , content ) => {
270
+ // Redirect to edit page
271
+ window . editWiki = ( title ) => {
279
272
if ( ! username ) {
280
273
alert ( 'You must be logged in to edit a wiki. Go to https://scratch-coding-hut.netlify.app/account to log in.' ) ;
281
274
return ;
282
275
}
283
276
284
- // Open the title and content in editable textareas
285
- const newTitle = prompt ( 'New title:' , title ) ;
286
- const newContent = prompt ( 'New content:' , content ) ;
287
-
288
- if ( newTitle && newContent ) {
289
- const response = await fetch ( `${ backendUrl } /wikis/${ id } ` , {
290
- method : 'PUT' ,
291
- headers : { 'Content-Type' : 'application/json' } ,
292
- body : JSON . stringify ( { title : newTitle , content : newContent , owner : username } ) , // Send username from localStorage
293
- } ) ;
294
-
295
- if ( response . ok ) {
296
- fetchWikis ( ) ; // Refresh wiki list
297
- } else {
298
- alert ( 'Not authorized to edit this wiki or wiki not found' ) ;
299
- }
300
- }
277
+ // Redirect to the edit page with the 'edit' parameter
278
+ window . location . href = `edit.html?edit=${ encodeURIComponent ( title ) } ` ;
301
279
} ;
302
280
303
281
// Initial fetch of wikis
0 commit comments