|
| 1 | +<style> |
| 2 | + /* Container Styling */ |
| 3 | + .form-container { |
| 4 | + background-color: #ffcccc; |
| 5 | + padding: 30px; |
| 6 | + border-radius: 10px; |
| 7 | + box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1); |
| 8 | + width: 100%; |
| 9 | + max-width: 500px; |
| 10 | + margin-bottom: 30px; |
| 11 | + text-align: center; |
| 12 | + } |
| 13 | + |
| 14 | + /* Input and Textarea Styling */ |
| 15 | + input, textarea { |
| 16 | + width: 100%; |
| 17 | + padding: 15px; |
| 18 | + margin-bottom: 15px; |
| 19 | + border-radius: 8px; |
| 20 | + border: 2px solid #e60000; |
| 21 | + font-size: 1rem; |
| 22 | + outline: none; |
| 23 | + transition: all 0.3s ease-in-out; |
| 24 | + } |
| 25 | + |
| 26 | + /* Focus Effect for Input and Textarea */ |
| 27 | + input:focus, textarea:focus { |
| 28 | + border-color: #ff4d4d; |
| 29 | + box-shadow: 0 0 5px rgba(255, 77, 77, 0.5); |
| 30 | + } |
| 31 | + |
| 32 | + /* Textarea Height Adjustment */ |
| 33 | + textarea { |
| 34 | + height: 150px; |
| 35 | + resize: vertical; |
| 36 | + } |
| 37 | + |
| 38 | + /* Button Styling */ |
| 39 | + button { |
| 40 | + background-color: #e60000; |
| 41 | + color: white; |
| 42 | + padding: 12px 20px; |
| 43 | + font-size: 1rem; |
| 44 | + border: none; |
| 45 | + border-radius: 8px; |
| 46 | + cursor: pointer; |
| 47 | + transition: background-color 0.3s; |
| 48 | + } |
| 49 | + |
| 50 | + /* Hover effect for the button */ |
| 51 | + button:hover { |
| 52 | + background-color: #ff4d4d; |
| 53 | + } |
| 54 | + |
| 55 | + /* Mobile Responsive Styling */ |
| 56 | + @media (max-width: 768px) { |
| 57 | + .form-container { |
| 58 | + width: 90%; |
| 59 | + padding: 20px; |
| 60 | + } |
| 61 | + |
| 62 | + input, textarea { |
| 63 | + font-size: 0.9rem; |
| 64 | + padding: 12px; |
| 65 | + } |
| 66 | + } |
| 67 | +</style> |
| 68 | + |
| 69 | +<div class="form-container"> |
| 70 | + <form id="createWikiForm" onsubmit="return done(event)"> |
| 71 | + <input type="text" id="wikiTitle" placeholder="Title" required> |
| 72 | + <textarea id="wikiContent" placeholder="Content" required></textarea> |
| 73 | + <button type="submit">Done</button> |
| 74 | + </form> |
| 75 | +</div> |
| 76 | + |
| 77 | +<script> |
| 78 | + const backendUrl = 'https://thejsurlback.onrender.com/api'; |
| 79 | + const res = await fetch(`${backendUrl}/wikis`); |
| 80 | + const data = await res.json(); |
| 81 | + |
| 82 | + const id = data.findIndex(wiki => wiki.id === paramValue); |
| 83 | + if (id === -1) { |
| 84 | + alert('Wiki not found!'); |
| 85 | + return; |
| 86 | + } |
| 87 | + // Get URL parameters using URLSearchParams |
| 88 | + const urlParams = new URLSearchParams(window.location.search); |
| 89 | + const paramValue = urlParams.get('edit'); |
| 90 | + |
| 91 | + document.getElementById('wikiTitle').textContent = paramValue; |
| 92 | + document.getElemantById('wikiContent').textContent = data.id.content |
| 93 | + |
| 94 | + async function done(event) { |
| 95 | + event.preventDefault(); // Prevent default form submission behavior |
| 96 | + |
| 97 | + const res = await fetch(`${backendUrl}/wikis`); |
| 98 | + const data = await res.json(); |
| 99 | + |
| 100 | + const id = data.findIndex(wiki => wiki.id === paramValue); |
| 101 | + if (id === -1) { |
| 102 | + alert('Wiki not found!'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const wikiTitle = document.getElementById('wikiTitle').value; |
| 107 | + const wikiContent = document.getElementById('wikiContent').value; |
| 108 | + |
| 109 | + const newTitle = prompt('New title:', wikiTitle); |
| 110 | + const newContent = prompt('New content:', wikiContent); |
| 111 | + const username = localStorage.getItem('username'); |
| 112 | + |
| 113 | + if (newTitle && newContent) { |
| 114 | + const response = await fetch(`${backendUrl}/wikis/${id}`, { |
| 115 | + method: 'PUT', |
| 116 | + headers: { 'Content-Type': 'application/json' }, |
| 117 | + body: JSON.stringify({ |
| 118 | + title: newTitle, |
| 119 | + content: newContent, |
| 120 | + owner: username, |
| 121 | + }), |
| 122 | + }); |
| 123 | + |
| 124 | + if (response.ok) { |
| 125 | + alert('Wiki updated successfully!'); |
| 126 | + } else { |
| 127 | + alert('Failed to update the wiki.'); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +</script> |
0 commit comments