From c8d00625036fce3d989251c8c381021a158f9f97 Mon Sep 17 00:00:00 2001 From: numex-admin Date: Wed, 9 Sep 2026 22:38:19 +0000 Subject: [PATCH] =?UTF-8?q?Numex=20yay=C4=B1n:=20public/js/app-notes.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/app-notes.js | 168 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 public/js/app-notes.js diff --git a/public/js/app-notes.js b/public/js/app-notes.js new file mode 100644 index 0000000..9d9aadd --- /dev/null +++ b/public/js/app-notes.js @@ -0,0 +1,168 @@ +/* ============================================================ + AURA OS — Uygulama A: Siber Not Defteri + POST/GET /api/notes ile backend'e bağlanır + ============================================================ */ + +const NotesApp = { + _textarea: null, + _listEl: null, + _msgEl: null, + _saveBtn: null, +}; + +/* API Yardımcıları */ +async function apiGetNotes() { + const res = await fetch('/api/notes', { method: 'GET' }); + if (!res.ok) throw new Error('GET /api/notes -> ' + res.status); + const data = await res.json(); + return data.notes || []; +} + +async function apiPostNote(text) { + const res = await fetch('/api/notes', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text }), + }); + if (!res.ok) { + const err = await res.json().catch(() => ({})); + throw new Error((err && err.error) || 'POST /api/notes -> ' + res.status); + } + return await res.json(); +} + +/* Not listesini render eder */ +function notesRenderList(notes) { + const listEl = NotesApp._listEl; + if (!listEl) return; + + if (!notes || notes.length === 0) { + listEl.innerHTML = '
Henüz kayıtlı not yok.
'; + return; + } + + listEl.innerHTML = ''; + notes.forEach(n => { + const time = n.createdAt ? new Date(n.createdAt) : null; + let dateText = ''; + if (time) { + const days = ['Pazar','Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi']; + dateText = + days[time.getDay()] + ' ' + + String(time.getDate()).padStart(2,'0') + '.' + + String(time.getMonth()+1).padStart(2,'0') + '.' + + time.getFullYear() + ' ' + + String(time.getHours()).padStart(2,'0') + ':' + + String(time.getMinutes()).padStart(2,'0'); + } + + const item = document.createElement('div'); + item.className = 'note-item'; + item.innerHTML = + '
' + + (dateText ? '
▸ ' + dateText + '
' : ''); + item.querySelector('.ni-text').textContent = n.text; + listEl.appendChild(item); + }); +} + +/* Notları yükle */ +async function refreshNotesList() { + try { + const notes = await apiGetNotes(); + notesRenderList(notes); + return notes; + } catch (err) { + notesRenderList([]); + return []; + } +} + +/* Kaydet butonu */ +async function saveCurrentNote() { + const textarea = NotesApp._textarea; + const msgEl = NotesApp._msgEl; + const btn = NotesApp._saveBtn; + + const text = textarea ? textarea.value : ''; + if (!text.trim()) { + flashMsg(msgEl, '⚠ Boş not kaydedilemez!', true); + if (textarea) textarea.focus(); + return; + } + + if (btn) { btn.disabled = true; btn.textContent = '⏳ Kaydediliyor...'; } + + try { + const result = await apiPostNote(text); + flashMsg(msgEl, '✓ Not kaydedildi! (' + result.notes.length + ' not)', false); + if (textarea) textarea.value = ''; + notesRenderList(result.notes); + } catch (err) { + flashMsg(msgEl, '✗ Hata: ' + err.message, true); + } finally { + if (btn) { btn.disabled = false; btn.textContent = '💾 Kaydet'; } + } +} + +function flashMsg(el, text, isErr) { + if (!el) return; + el.textContent = text; + el.style.color = isErr ? '#ff6b6b' : '#39ff14'; + setTimeout(() => { if (el) el.textContent = ''; }, 4000); +} + +/* Uygulama başlat şablonu: DOM'daki