zkt26/zadanie 1/frontend/new-post.html
2026-03-22 12:58:49 +01:00

402 lines
12 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="sk">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Nový príspevok</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,700;1,400&family=Lora:ital,wght@0,400;0,500;1,400&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #faf8f3;
--ink: #1c1917;
--ink-muted:#78716c;
--accent: #b45309;
--accent-lt:#fef3c7;
--rule: #e7e5e0;
--card-bg: #ffffff;
--mono: 'JetBrains Mono', monospace;
--serif: 'Lora', Georgia, serif;
--display: 'Playfair Display', Georgia, serif;
}
body {
background: var(--bg);
color: var(--ink);
font-family: var(--serif);
font-size: 1.0625rem;
line-height: 1.75;
min-height: 100vh;
}
/* ── HEADER ── */
header {
border-bottom: 1px solid var(--rule);
padding: 1.25rem clamp(1.5rem, 5vw, 4rem);
display: flex;
align-items: center;
justify-content: space-between;
}
.back-link {
display: inline-flex;
align-items: center;
gap: 0.5rem;
font-family: var(--mono);
font-size: 0.7rem;
letter-spacing: 0.1em;
text-transform: uppercase;
color: var(--ink-muted);
text-decoration: none;
transition: color 0.2s;
}
.back-link::before { content: '←'; }
.back-link:hover { color: var(--accent); }
.site-name {
font-family: var(--display);
font-size: 1.1rem;
font-weight: 700;
font-style: italic;
color: var(--accent);
text-decoration: none;
}
/* ── MAIN ── */
main {
max-width: 44rem;
margin: 0 auto;
padding: 4rem clamp(1.5rem, 5vw, 2rem) 6rem;
}
.page-label {
font-family: var(--mono);
font-size: 0.65rem;
letter-spacing: 0.2em;
text-transform: uppercase;
color: var(--accent);
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 1.5rem;
}
.page-label::after {
content: '';
flex: 1;
height: 1px;
background: var(--rule);
}
.page-title {
font-family: var(--display);
font-size: clamp(2rem, 5vw, 2.75rem);
font-weight: 700;
line-height: 1.1;
letter-spacing: -0.02em;
margin-bottom: 3rem;
}
.page-title em { font-style: italic; color: var(--accent); }
/* ── FORM ── */
.field {
margin-bottom: 2rem;
}
label {
display: block;
font-family: var(--mono);
font-size: 0.65rem;
letter-spacing: 0.2em;
text-transform: uppercase;
color: var(--ink-muted);
margin-bottom: 0.6rem;
}
input[type="text"],
select,
textarea {
width: 100%;
background: var(--card-bg);
border: 1px solid var(--rule);
border-radius: 2px;
padding: 0.85rem 1rem;
font-family: var(--serif);
font-size: 1rem;
color: var(--ink);
outline: none;
transition: border-color 0.2s, box-shadow 0.2s;
appearance: none;
}
input[type="text"]:focus,
select:focus,
textarea:focus {
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-lt);
}
textarea {
min-height: 18rem;
resize: vertical;
line-height: 1.75;
font-size: 0.97rem;
}
.field-hint {
font-family: var(--mono);
font-size: 0.65rem;
color: var(--ink-muted);
margin-top: 0.4rem;
}
.char-count {
text-align: right;
font-family: var(--mono);
font-size: 0.65rem;
color: var(--ink-muted);
margin-top: 0.4rem;
}
/* ── ACTIONS ── */
.actions {
display: flex;
gap: 1rem;
align-items: center;
padding-top: 2rem;
border-top: 1px solid var(--rule);
}
.btn-primary {
font-family: var(--mono);
font-size: 0.72rem;
letter-spacing: 0.12em;
text-transform: uppercase;
font-weight: 500;
background: var(--accent);
color: white;
border: none;
padding: 0.85rem 2rem;
cursor: pointer;
border-radius: 2px;
transition: background 0.2s, transform 0.1s;
}
.btn-primary:hover { background: #92400e; }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-secondary {
font-family: var(--mono);
font-size: 0.72rem;
letter-spacing: 0.12em;
text-transform: uppercase;
background: none;
color: var(--ink-muted);
border: 1px solid var(--rule);
padding: 0.85rem 1.5rem;
cursor: pointer;
border-radius: 2px;
text-decoration: none;
transition: border-color 0.2s, color 0.2s;
}
.btn-secondary:hover { border-color: var(--ink-muted); color: var(--ink); }
/* ── TOAST ── */
#toast {
position: fixed;
bottom: 2rem;
right: 2rem;
background: var(--ink);
color: var(--bg);
font-family: var(--mono);
font-size: 0.75rem;
padding: 0.75rem 1.25rem;
border-radius: 4px;
opacity: 0;
transform: translateY(0.5rem);
transition: opacity 0.3s, transform 0.3s;
pointer-events: none;
}
#toast.show { opacity: 1; transform: translateY(0); }
/* ── ENTRANCE ── */
.fade-in { opacity: 0; animation: fadeUp 0.4s ease forwards; }
.delay-1 { animation-delay: 0.1s; }
.delay-2 { animation-delay: 0.2s; }
@keyframes fadeUp { to { opacity: 1; transform: none; } }
</style>
</head>
<body>
<header>
<a class="back-link" href="index.html">Späť na blog</a>
<a class="site-name" href="index.html">Môj Blog</a>
</header>
<main>
<p class="page-label fade-in" id="page-label">Nový príspevok</p>
<h1 class="page-title fade-in delay-1" id="page-title">Napíšte<br /><em>článok</em></h1>
<div class="fade-in delay-2">
<div class="field">
<label for="title">Nadpis príspevku *</label>
<input type="text" id="title" placeholder="Zadajte nadpis…" maxlength="200" required />
<div class="char-count"><span id="title-count">0</span> / 200</div>
</div>
<div class="field">
<label for="category">Kategória</label>
<select id="category">
<option value="">— Bez kategórie —</option>
<option value="Technológie">Technológie</option>
<option value="Cloud">Cloud</option>
<option value="Bezpečnosť">Bezpečnosť</option>
<option value="Sieťovanie">Sieťovanie</option>
<option value="Osobné">Osobné</option>
<option value="Iné">Iné</option>
</select>
</div>
<div class="field">
<label for="excerpt">Krátky popis (voliteľné)</label>
<input type="text" id="excerpt" placeholder="Krátky opis článku pre zoznam…" maxlength="300" />
<p class="field-hint">Zobrazí sa v zozname príspevkov. Ak nevyplníte, použije sa začiatok textu.</p>
</div>
<div class="field">
<label for="content">Text článku *</label>
<textarea id="content" placeholder="Začnite písať…"></textarea>
<div class="char-count"><span id="content-count">0</span> znakov</div>
</div>
<div class="actions">
<button class="btn-primary" id="btn-submit">Zverejniť príspevok</button>
<a class="btn-secondary" href="index.html">Zrušiť</a>
</div>
</div>
</main>
<div id="toast"></div>
<script>
const API = '/api';
// Zistíme z URL či ideme editovať (?id=5) alebo vytvárať nový
// new-post.html → editMode = false → POST
// new-post.html?id=5 → editMode = true → PUT
const params = new URLSearchParams(location.search);
const editId = params.get('id'); // null ak nie je v URL
const editMode = editId !== null;
const titleInput = document.getElementById('title');
const contentInput = document.getElementById('content');
// Ak editujeme, zmeníme texty na stránke a načítame existujúce dáta
if (editMode) {
document.title = 'Upraviť príspevok — Môj Blog';
document.getElementById('page-label').textContent = 'Úprava príspevku';
document.getElementById('page-title').innerHTML = 'Upraviť<br /><em>článok</em>';
document.getElementById('btn-submit').textContent = 'Uložiť zmeny';
document.querySelector('.back-link').href = `post.html?id=${editId}`;
loadExistingPost();
}
// Načíta dáta príspevku z API a predvyplní formulár
async function loadExistingPost() {
try {
const res = await fetch(`${API}/posts/${editId}`);
if (!res.ok) throw new Error();
const post = await res.json();
// Predvyplníme polia hodnotami z databázy
titleInput.value = post.title;
contentInput.value = post.content;
document.getElementById('excerpt').value = post.excerpt || '';
document.getElementById('title-count').textContent = post.title.length;
document.getElementById('content-count').textContent = post.content.length;
// Nastavíme správnu kategóriu v selecte
if (post.category) {
document.getElementById('category').value = post.category;
}
} catch {
showToast('Nepodarilo sa načítať príspevok.');
}
}
// Char counters
titleInput.addEventListener('input', () => {
document.getElementById('title-count').textContent = titleInput.value.length;
});
contentInput.addEventListener('input', () => {
document.getElementById('content-count').textContent = contentInput.value.length;
});
function showToast(msg, duration = 3500) {
const t = document.getElementById('toast');
t.textContent = msg;
t.classList.add('show');
setTimeout(() => t.classList.remove('show'), duration);
}
function validate() {
if (!titleInput.value.trim()) {
showToast('Prosím zadajte nadpis.');
titleInput.focus();
return false;
}
if (!contentInput.value.trim()) {
showToast('Prosím napíšte text článku.');
contentInput.focus();
return false;
}
return true;
}
document.getElementById('btn-submit').addEventListener('click', async () => {
if (!validate()) return;
const btn = document.getElementById('btn-submit');
btn.disabled = true;
btn.textContent = 'Ukladám…';
const payload = {
title: titleInput.value.trim(),
category: document.getElementById('category').value,
excerpt: document.getElementById('excerpt').value.trim(),
content: contentInput.value.trim(),
};
try {
// editMode → PUT na existujúci príspevok, inak → POST nový
const url = editMode ? `${API}/posts/${editId}` : `${API}/posts`;
const method = editMode ? 'PUT' : 'POST';
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const saved = await res.json();
showToast(editMode ? '✓ Zmeny boli uložené!' : '✓ Príspevok bol zverejnený!');
setTimeout(() => window.location.href = `post.html?id=${saved.id}`, 1200);
} catch (err) {
console.error(err);
showToast('Nepodarilo sa uložiť. Skúste znova.');
btn.disabled = false;
btn.textContent = editMode ? 'Uložiť zmeny' : 'Zverejniť príspevok';
}
});
</script>
</body>
</html>