|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Scratch Coding Hut | Wiki</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + font-family: Arial, sans-serif; |
| 10 | + padding: 20px; |
| 11 | + } |
| 12 | + |
| 13 | + h1 { |
| 14 | + color: #333; |
| 15 | + } |
| 16 | + |
| 17 | + .form-container { |
| 18 | + margin-bottom: 20px; |
| 19 | + } |
| 20 | + |
| 21 | + input, textarea { |
| 22 | + width: 100%; |
| 23 | + margin: 5px 0; |
| 24 | + padding: 10px; |
| 25 | + } |
| 26 | + |
| 27 | + button { |
| 28 | + background-color: #4CAF50; |
| 29 | + color: white; |
| 30 | + padding: 10px 20px; |
| 31 | + border: none; |
| 32 | + cursor: pointer; |
| 33 | + } |
| 34 | + |
| 35 | + button:hover { |
| 36 | + background-color: #45a049; |
| 37 | + } |
| 38 | + |
| 39 | + .wikis-container { |
| 40 | + margin-top: 20px; |
| 41 | + } |
| 42 | + |
| 43 | + ul { |
| 44 | + list-style: none; |
| 45 | + padding-left: 0; |
| 46 | + } |
| 47 | + |
| 48 | + li { |
| 49 | + padding: 10px; |
| 50 | + border: 1px solid #ddd; |
| 51 | + margin-bottom: 10px; |
| 52 | + } |
| 53 | + |
| 54 | + button.delete { |
| 55 | + background-color: red; |
| 56 | + color: white; |
| 57 | + } |
| 58 | + |
| 59 | + button.edit { |
| 60 | + background-color: yellow; |
| 61 | + color: black; |
| 62 | + } |
| 63 | + </style> |
| 64 | +</head> |
| 65 | +<body> |
| 66 | + <h1>Scratch Coding Hut Wiki</h1> |
| 67 | + |
| 68 | + <div class="form-container"> |
| 69 | + <h2>Create a Wiki</h2> |
| 70 | + <form id="createWikiForm"> |
| 71 | + <input type="text" id="wikiTitle" placeholder="Title" required> |
| 72 | + <textarea id="wikiContent" placeholder="Content" required></textarea> |
| 73 | + <input type="text" id="wikiOwner" placeholder="Your Username" required> |
| 74 | + <button type="submit">Create Wiki</button> |
| 75 | + </form> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div class="wikis-container"> |
| 79 | + <h2>Wikis</h2> |
| 80 | + <ul id="wikiList"></ul> |
| 81 | + </div> |
| 82 | + |
| 83 | + <script> |
| 84 | + document.addEventListener('DOMContentLoaded', () => { |
| 85 | + const createWikiForm = document.getElementById('createWikiForm'); |
| 86 | + const wikiList = document.getElementById('wikiList'); |
| 87 | + const backendUrl = 'https://thejsurlback.onrender.com/api'; // Replace with your backend URL |
| 88 | + |
| 89 | + // Retrieve username from localStorage |
| 90 | + const username = localStorage.getItem('username'); |
| 91 | + if (!username) { |
| 92 | + alert('You must be logged in to interact with the wiki.'); |
| 93 | + return; // Stop execution if no username is found |
| 94 | + } |
| 95 | + |
| 96 | + // Fetch and display all wikis |
| 97 | + const fetchWikis = async () => { |
| 98 | + const response = await fetch(`${backendUrl}/wikis`); |
| 99 | + const wikis = await response.json(); |
| 100 | + wikiList.innerHTML = ''; // Clear the current list |
| 101 | + |
| 102 | + wikis.forEach(wiki => { |
| 103 | + const li = document.createElement('li'); |
| 104 | + li.innerHTML = ` |
| 105 | + <h3>${wiki.title}</h3> |
| 106 | + <p>${wiki.content}</p> |
| 107 | + <small>Owner: ${wiki.owner}</small> |
| 108 | + <button class="edit" onclick="editWiki(${wiki.id})">Edit</button> |
| 109 | + <button class="delete" onclick="deleteWiki(${wiki.id})">Delete</button> |
| 110 | + `; |
| 111 | + wikiList.appendChild(li); |
| 112 | + }); |
| 113 | + }; |
| 114 | + |
| 115 | + // Create a new wiki |
| 116 | + createWikiForm.addEventListener('submit', async (e) => { |
| 117 | + e.preventDefault(); |
| 118 | + const title = document.getElementById('wikiTitle').value; |
| 119 | + const content = document.getElementById('wikiContent').value; |
| 120 | + |
| 121 | + const response = await fetch(`${backendUrl}/wikis`, { |
| 122 | + method: 'POST', |
| 123 | + headers: { 'Content-Type': 'application/json' }, |
| 124 | + body: JSON.stringify({ title, content, owner: username }), // Send username from localStorage |
| 125 | + }); |
| 126 | + |
| 127 | + const newWiki = await response.json(); |
| 128 | + fetchWikis(); // Refresh wiki list |
| 129 | + createWikiForm.reset(); |
| 130 | + }); |
| 131 | + |
| 132 | + // Delete a wiki |
| 133 | + window.deleteWiki = async (id) => { |
| 134 | + if (!username) { |
| 135 | + alert('You must be logged in to delete a wiki.'); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const response = await fetch(`${backendUrl}/wikis/${id}`, { |
| 140 | + method: 'DELETE', |
| 141 | + headers: { 'Content-Type': 'application/json' }, |
| 142 | + body: JSON.stringify({ owner: username }), // Send username from localStorage |
| 143 | + }); |
| 144 | + |
| 145 | + if (response.ok) { |
| 146 | + fetchWikis(); // Refresh wiki list |
| 147 | + } else { |
| 148 | + alert('Not authorized to delete this wiki or wiki not found'); |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + // Edit a wiki |
| 153 | + window.editWiki = async (id) => { |
| 154 | + if (!username) { |
| 155 | + alert('You must be logged in to edit a wiki.'); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + const newTitle = prompt('New title:'); |
| 160 | + const newContent = prompt('New content:'); |
| 161 | + |
| 162 | + const response = await fetch(`${backendUrl}/wikis/${id}`, { |
| 163 | + method: 'PUT', |
| 164 | + headers: { 'Content-Type': 'application/json' }, |
| 165 | + body: JSON.stringify({ title: newTitle, content: newContent, owner: username }), // Send username from localStorage |
| 166 | + }); |
| 167 | + |
| 168 | + if (response.ok) { |
| 169 | + fetchWikis(); // Refresh wiki list |
| 170 | + } else { |
| 171 | + alert('Not authorized to edit this wiki or wiki not found'); |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + // Initial fetch of wikis |
| 176 | + fetchWikis(); |
| 177 | + }); |
| 178 | + </script> |
| 179 | +</body> |
| 180 | +</html> |
0 commit comments