1313 }
1414
1515 body {
16- font-family :
1716 font-family : 'Arial' , sans-serif;
1817 background : # fff4f4 ;
1918 color : # 333 ;
@@ -124,16 +123,22 @@ <h2>Edit Wiki Post</h2>
124123
125124 // Fetch the wiki to edit
126125 const fetchWiki = async ( ) => {
127- const response = await fetch ( `${ backendUrl } /wikis` ) ;
128- const wikis = await response . json ( ) ;
129- const wiki = wikis . find ( w => w . title === wikiTitle ) ;
130-
131- if ( ! wiki ) {
132- alert ( 'Wiki not found!' ) ;
133- window . location . href = 'index.html' ;
134- } else {
135- wikiTitleInput . value = wiki . title ;
136- wikiContentInput . value = wiki . content ;
126+ try {
127+ const response = await fetch ( `${ backendUrl } /wikis` ) ;
128+ const wikis = await response . json ( ) ;
129+ console . log ( 'Fetched wikis:' , wikis ) ; // Log the fetched wikis
130+ const wiki = wikis . find ( w => w . title === wikiTitle ) ;
131+
132+ if ( ! wiki ) {
133+ alert ( 'Wiki not found!' ) ;
134+ window . location . href = 'Wiki/wiki' ;
135+ } else {
136+ wikiTitleInput . value = wiki . title ;
137+ wikiContentInput . value = wiki . content ;
138+ }
139+ } catch ( error ) {
140+ console . error ( 'Error fetching wiki:' , error ) ;
141+ alert ( 'Error fetching wiki data. Please try again later.' ) ;
137142 }
138143 } ;
139144
@@ -149,21 +154,28 @@ <h2>Edit Wiki Post</h2>
149154 return ;
150155 }
151156
152- const response = await fetch ( `${ backendUrl } /wikis` , {
153- method : 'PUT' ,
154- headers : { 'Content-Type' : 'application/json' } ,
155- body : JSON . stringify ( {
156- title : updatedTitle ,
157- content : updatedContent ,
158- owner : username
159- } )
160- } ) ;
161-
162- if ( response . ok ) {
163- alert ( 'Wiki updated successfully!' ) ;
164- window . location . href = 'Wiki/wiki' ; // Redirect back to the main page after saving
165- } else {
166- alert ( 'Failed to update wiki!' ) ;
157+ try {
158+ const response = await fetch ( `${ backendUrl } /wikis` , {
159+ method : 'PUT' ,
160+ headers : { 'Content-Type' : 'application/json' } ,
161+ body : JSON . stringify ( {
162+ title : updatedTitle ,
163+ content : updatedContent ,
164+ owner : username
165+ } )
166+ } ) ;
167+
168+ if ( response . ok ) {
169+ alert ( 'Wiki updated successfully!' ) ;
170+ window . location . href = 'Wiki/wiki.html' ; // Redirect to Wiki/wiki.html after saving
171+ } else {
172+ const errorDetails = await response . json ( ) ;
173+ console . error ( 'Failed to update wiki:' , errorDetails ) ;
174+ alert ( 'Failed to update wiki. Please try again.' ) ;
175+ }
176+ } catch ( error ) {
177+ console . error ( 'Error updating wiki:' , error ) ;
178+ alert ( 'Error updating wiki. Please check your connection or try again later.' ) ;
167179 }
168180 } ) ;
169181
0 commit comments