5757 .form-container textarea {
5858 height : 150px ;
5959 }
60- .form-container button {
61- background-color : # 2196F3 ;
62- color : white;
63- padding : 10px 20px ;
64- border : none;
65- cursor : pointer;
66- border-radius : 5px ;
67- font-size : 16px ;
68- margin-top : 10px ;
69- }
70- .form-container button : hover {
71- background-color : # 1976D2 ;
72- }
7360 </ style >
7461</ head >
7562< body >
@@ -86,23 +73,17 @@ <h2>Create New Page</h2>
8673 </ div >
8774
8875 < script >
89- const JSON_BIN_URL = 'https://api.jsonbin.io/v3/b/67c73db1e41b4d34e4a0bfb1/latest' ;
76+ const JSON_BIN_READ_URL = 'https://api.jsonbin.io/v3/b/67c73db1e41b4d34e4a0bfb1/latest' ;
77+ const JSON_BIN_WRITE_URL = 'https://api.jsonbin.io/v3/b/67c73db1e41b4d34e4a0bfb1' ;
9078
91- // Function to toggle visibility of the form
9279 function toggleForm ( ) {
9380 const formContainer = document . getElementById ( 'form-container' ) ;
9481 formContainer . style . display = formContainer . style . display === 'none' || formContainer . style . display === '' ? 'block' : 'none' ;
9582 }
9683
97- // Fetch and display the wiki pages
9884 async function fetchWiki ( ) {
9985 try {
100- const res = await fetch ( JSON_BIN_URL , {
101- method : 'GET'
102- } ) ;
103- if ( ! res . ok ) {
104- throw new Error ( `Failed to fetch wiki pages: ${ res . status } ` ) ;
105- }
86+ const res = await fetch ( JSON_BIN_READ_URL ) ;
10687 const data = await res . json ( ) ;
10788 const pages = data . record ;
10889 document . getElementById ( 'wiki' ) . innerHTML = pages . map ( ( page , index ) => `
@@ -118,84 +99,66 @@ <h3>${page.title}</h3>
11899 }
119100 }
120101
121- // Update the wiki after adding/editing/deleting pages
122102 async function updateWiki ( pages ) {
123103 try {
124- const res = await fetch ( JSON_BIN_URL , {
104+ const res = await fetch ( JSON_BIN_WRITE_URL , {
125105 method : 'PUT' ,
126- headers : {
127- 'Content-Type' : 'application/json'
128- } ,
106+ headers : { 'Content-Type' : 'application/json' } ,
129107 body : JSON . stringify ( { record : pages } )
130108 } ) ;
131- if ( ! res . ok ) {
132- throw new Error ( `Failed to update wiki: ${ res . status } ` ) ;
133- }
134- fetchWiki ( ) ; // Reload the wiki after the update
109+ fetchWiki ( ) ;
135110 } catch ( error ) {
136111 console . error ( 'Error updating wiki:' , error ) ;
137112 }
138113 }
139114
140- // Add a new page to the wiki
141115 async function addPage ( ) {
142116 const title = document . getElementById ( 'new-title' ) . value ;
143117 const content = document . getElementById ( 'new-content' ) . value ;
144-
145118 if ( ! title || ! content ) {
146119 alert ( 'Title and content cannot be empty.' ) ;
147120 return ;
148121 }
149-
150122 try {
151- const res = await fetch ( JSON_BIN_URL , {
152- method : 'GET'
153- } ) ;
123+ const res = await fetch ( JSON_BIN_READ_URL ) ;
154124 const data = await res . json ( ) ;
155125 const pages = data . record ;
156126 pages . push ( { title, content } ) ;
157- updateWiki ( pages ) ; // Update the wiki with the new page
127+ updateWiki ( pages ) ;
158128 } catch ( error ) {
159129 console . error ( 'Error adding page:' , error ) ;
160130 }
161131 }
162132
163- // Edit an existing page
164133 async function editPage ( index ) {
165134 const newContent = prompt ( 'Edit page content:' ) ;
166135 if ( newContent !== null ) {
167136 try {
168- const res = await fetch ( JSON_BIN_URL , {
169- method : 'GET'
170- } ) ;
137+ const res = await fetch ( JSON_BIN_READ_URL ) ;
171138 const data = await res . json ( ) ;
172139 const pages = data . record ;
173140 pages [ index ] . content = newContent ;
174- updateWiki ( pages ) ; // Update the wiki with the edited page
141+ updateWiki ( pages ) ;
175142 } catch ( error ) {
176143 console . error ( 'Error editing page:' , error ) ;
177144 }
178145 }
179146 }
180147
181- // Delete a page from the wiki
182148 async function deletePage ( index ) {
183149 if ( confirm ( 'Are you sure you want to delete this page?' ) ) {
184150 try {
185- const res = await fetch ( JSON_BIN_URL , {
186- method : 'GET'
187- } ) ;
151+ const res = await fetch ( JSON_BIN_READ_URL ) ;
188152 const data = await res . json ( ) ;
189153 const pages = data . record ;
190- pages . splice ( index , 1 ) ; // Remove the page from the array
191- updateWiki ( pages ) ; // Update the wiki with the remaining pages
154+ pages . splice ( index , 1 ) ;
155+ updateWiki ( pages ) ;
192156 } catch ( error ) {
193157 console . error ( 'Error deleting page:' , error ) ;
194158 }
195159 }
196160 }
197161
198- // Fetch the wiki when the page loads
199162 fetchWiki ( ) ;
200163 </ script >
201164</ body >
0 commit comments