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

346 lines
9.8 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>Príspevok — Môj Blog</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.8;
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;
}
/* ── ARTICLE ── */
article {
max-width: 44rem;
margin: 0 auto;
padding: 4rem clamp(1.5rem, 5vw, 2rem) 6rem;
}
.article-category {
font-family: var(--mono);
font-size: 0.65rem;
letter-spacing: 0.18em;
text-transform: uppercase;
color: var(--accent);
background: var(--accent-lt);
display: inline-block;
padding: 0.25rem 0.6rem;
border-radius: 2px;
margin-bottom: 1.5rem;
}
.article-title {
font-family: var(--display);
font-size: clamp(2rem, 5vw, 3.25rem);
font-weight: 700;
line-height: 1.1;
letter-spacing: -0.02em;
margin-bottom: 1.5rem;
}
.article-meta {
display: flex;
align-items: center;
gap: 1.5rem;
padding: 1.25rem 0;
border-top: 1px solid var(--rule);
border-bottom: 1px solid var(--rule);
margin-bottom: 3rem;
}
.meta-item {
font-family: var(--mono);
font-size: 0.68rem;
color: var(--ink-muted);
letter-spacing: 0.06em;
}
.meta-label {
display: block;
font-size: 0.58rem;
letter-spacing: 0.15em;
text-transform: uppercase;
color: var(--ink-muted);
opacity: 0.6;
margin-bottom: 0.2rem;
}
.meta-divider {
width: 1px;
height: 2rem;
background: var(--rule);
}
/* ── BODY TEXT ── */
.article-body {
font-size: 1.05rem;
line-height: 1.85;
white-space: pre-wrap;
word-break: break-word;
}
.article-body p { margin-bottom: 1.5rem; }
/* ── DELETE BUTTON ── */
.danger-zone {
margin-top: 4rem;
padding-top: 2rem;
border-top: 1px solid var(--rule);
display: flex;
justify-content: flex-end;
gap: 1rem;
}
.btn-edit {
font-family: var(--mono);
font-size: 0.7rem;
letter-spacing: 0.1em;
text-transform: uppercase;
color: var(--accent);
background: none;
border: 1px solid var(--accent);
padding: 0.5rem 1.25rem;
cursor: pointer;
border-radius: 2px;
text-decoration: none;
transition: background 0.2s, color 0.2s;
}
.btn-edit:hover { background: var(--accent); color: white; }
.btn-delete {
font-family: var(--mono);
font-size: 0.7rem;
letter-spacing: 0.1em;
text-transform: uppercase;
color: #dc2626;
background: none;
border: 1px solid #dc2626;
padding: 0.5rem 1.25rem;
cursor: pointer;
border-radius: 2px;
transition: background 0.2s, color 0.2s;
}
.btn-delete:hover { background: #dc2626; color: white; }
/* ── SKELETON ── */
.skeleton {
background: linear-gradient(90deg, var(--rule) 25%, #ede9e2 50%, var(--rule) 75%);
background-size: 200% 100%;
animation: shimmer 1.4s infinite;
border-radius: 3px;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
/* ── ERROR STATE ── */
.error-state {
text-align: center;
padding: 6rem 2rem;
}
.error-state h2 {
font-family: var(--display);
font-size: 2rem;
font-style: italic;
color: var(--ink-muted);
margin-bottom: 1rem;
}
.error-state a {
color: var(--accent);
}
/* ── 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.5s ease forwards; }
@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>
<article id="article-root">
<!-- loaded by JS -->
<div style="display:flex;flex-direction:column;gap:1rem;margin-top:1rem;">
<div class="skeleton" style="height:1.1rem;width:5rem;"></div>
<div class="skeleton" style="height:3rem;width:90%;"></div>
<div class="skeleton" style="height:3rem;width:65%;"></div>
<div class="skeleton" style="height:1rem;width:100%;margin-top:1rem;"></div>
<div class="skeleton" style="height:1rem;width:100%;"></div>
<div class="skeleton" style="height:1rem;width:80%;"></div>
</div>
</article>
<div id="toast"></div>
<script>
const API = '/api';
function formatDate(iso) {
return new Date(iso).toLocaleDateString('sk-SK', {
day: 'numeric', month: 'long', year: 'numeric'
});
}
function readingTime(text) {
const words = text.trim().split(/\s+/).length;
return Math.max(1, Math.round(words / 200));
}
function showToast(msg, duration = 3000) {
const t = document.getElementById('toast');
t.textContent = msg;
t.classList.add('show');
setTimeout(() => t.classList.remove('show'), duration);
}
function renderPost(post) {
document.title = `${post.title} — Môj Blog`;
document.getElementById('article-root').innerHTML = `
<span class="article-category fade-in">${post.category || 'Bez kategórie'}</span>
<h1 class="article-title fade-in">${post.title}</h1>
<div class="article-meta fade-in">
<div class="meta-item">
<span class="meta-label">Dátum</span>
${formatDate(post.created_at)}
</div>
<div class="meta-divider"></div>
<div class="meta-item">
<span class="meta-label">Čítanie</span>
${readingTime(post.content)} min
</div>
${post.updated_at ? `
<div class="meta-divider"></div>
<div class="meta-item">
<span class="meta-label">Upravené</span>
${formatDate(post.updated_at)}
</div>` : ''}
</div>
<div class="article-body fade-in">${post.content}</div>
<div class="danger-zone fade-in">
<a class="btn-edit" href="new-post.html?id=${post.id}">Upraviť príspevok</a>
<button class="btn-delete" id="btn-delete">Odstrániť príspevok</button>
</div>`;
document.getElementById('btn-delete').addEventListener('click', () => deletePost(post.id));
}
async function deletePost(id) {
if (!confirm('Naozaj chcete odstrániť tento príspevok?')) return;
try {
const res = await fetch(`${API}/posts/${id}`, { method: 'DELETE' });
if (!res.ok) throw new Error();
showToast('Príspevok bol odstránený.');
setTimeout(() => window.location.href = 'index.html', 1500);
} catch {
showToast('⚠ Nepodarilo sa odstrániť príspevok.');
}
}
async function loadPost() {
const params = new URLSearchParams(location.search);
const id = params.get('id');
if (!id) {
document.getElementById('article-root').innerHTML = `
<div class="error-state">
<h2>Príspevok nenájdený</h2>
<p><a href="index.html">Späť na domovskú stránku</a></p>
</div>`;
return;
}
try {
const res = await fetch(`${API}/posts/${id}`);
if (res.status === 404) throw new Error('not_found');
if (!res.ok) throw new Error();
const post = await res.json();
renderPost(post);
} catch (err) {
const msg = err.message === 'not_found'
? 'Príspevok neexistuje alebo bol odstránený.'
: 'Nepodarilo sa načítať príspevok.';
document.getElementById('article-root').innerHTML = `
<div class="error-state">
<h2>${msg}</h2>
<p><a href="index.html">Späť na domovskú stránku</a></p>
</div>`;
}
}
loadPost();
</script>
</body>
</html>