Skip to content

Commit b5d2281

Browse files
Add files via upload
1 parent 2a4c07b commit b5d2281

2 files changed

Lines changed: 79 additions & 60 deletions

File tree

assets/app.js

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -353,48 +353,98 @@ window.giveKudos = async function() {
353353
// Load post navigation (prev/next in same type)
354354
async function loadPostNavigation(postId) {
355355
try {
356-
const res = await fetch(`${API_BASE}/post/${postId}/navigation`);
357-
const data = await res.json();
356+
// Try the navigation API first
357+
let data = null;
358+
try {
359+
const res = await fetch(`${API_BASE}/post/${postId}/navigation`);
360+
if (res.ok) {
361+
data = await res.json();
362+
console.log('Navigation API data:', data);
363+
}
364+
} catch (apiErr) {
365+
console.log('Navigation API not available, using fallback');
366+
}
367+
368+
// If API didn't work or returned error, use fallback
369+
if (!data || data.error) {
370+
console.log('Using fallback navigation method');
371+
// Get current post type
372+
const postType = window.currentPostType || 'blog';
373+
374+
// Fetch all posts of this type
375+
const postsRes = await fetch(`${API_BASE}/post?type=${postType}`);
376+
const postsData = await postsRes.json();
377+
378+
// Flatten posts from grouped by year format
379+
const allPosts = (postsData.posts || []).flatMap(g => g.posts);
380+
381+
// Posts come sorted desc (newest first), so we reverse for chronological order
382+
const posts = allPosts.reverse();
383+
384+
// Find current post index
385+
const currentIndex = posts.findIndex(p => String(p.id) === String(postId));
386+
387+
if (currentIndex !== -1) {
388+
data = {
389+
type: postType,
390+
prev: currentIndex > 0 ? posts[currentIndex - 1] : null,
391+
next: currentIndex < posts.length - 1 ? posts[currentIndex + 1] : null
392+
};
393+
console.log('Fallback navigation data:', data);
394+
} else {
395+
console.log('Could not find post in list');
396+
return;
397+
}
398+
}
358399

359400
const postType = data.type || 'blog';
360401
const typeLabel = postType === 'story' ? 'Story' : 'Post';
361402

362403
// Bottom navigation
363-
const prevBtn = document.getElementById('prevPost');
364-
const nextBtn = document.getElementById('nextPost');
365-
366-
if (prevBtn && data.prev) {
367-
prevBtn.href = `./post.html?id=${data.prev.id}`;
368-
prevBtn.innerHTML = `<i class="bi bi-arrow-left"></i> Older ${typeLabel}`;
369-
prevBtn.title = data.prev.title;
404+
// Left button = Newer (data.next), Right button = Older (data.prev)
405+
const prevBtn = document.getElementById('prevPost'); // Left button
406+
const nextBtn = document.getElementById('nextPost'); // Right button
407+
408+
// Left button shows NEWER post (data.next)
409+
if (prevBtn && data.next) {
410+
prevBtn.href = `./post.html?id=${data.next.id}`;
411+
prevBtn.innerHTML = `<i class="bi bi-arrow-left"></i> Newer ${typeLabel}`;
412+
prevBtn.title = data.next.title;
370413
prevBtn.style.visibility = 'visible';
371414
}
372415

373-
if (nextBtn && data.next) {
374-
nextBtn.href = `./post.html?id=${data.next.id}`;
375-
nextBtn.innerHTML = `Newer ${typeLabel} <i class="bi bi-arrow-right"></i>`;
376-
nextBtn.title = data.next.title;
416+
// Right button shows OLDER post (data.prev)
417+
if (nextBtn && data.prev) {
418+
nextBtn.href = `./post.html?id=${data.prev.id}`;
419+
nextBtn.innerHTML = `Older ${typeLabel} <i class="bi bi-arrow-right"></i>`;
420+
nextBtn.title = data.prev.title;
377421
nextBtn.style.visibility = 'visible';
378422
}
379423

380424
// Sidebar navigation
381-
const prevSidebar = document.getElementById('prevPostSidebar');
382-
const nextSidebar = document.getElementById('nextPostSidebar');
425+
const prevSidebar = document.getElementById('prevPostSidebar'); // Shows older
426+
const nextSidebar = document.getElementById('nextPostSidebar'); // Shows newer
383427

428+
// prevSidebar shows OLDER (data.prev)
384429
if (prevSidebar && data.prev) {
385430
const link = prevSidebar.querySelector('a');
386-
link.href = `./post.html?id=${data.prev.id}`;
387-
link.textContent = `← ${data.prev.title}`;
388-
link.title = data.prev.title;
389-
prevSidebar.style.display = 'list-item';
431+
if (link) {
432+
link.href = `./post.html?id=${data.prev.id}`;
433+
link.textContent = `← Older: ${data.prev.title}`;
434+
link.title = data.prev.title;
435+
prevSidebar.style.display = 'list-item';
436+
}
390437
}
391438

439+
// nextSidebar shows NEWER (data.next)
392440
if (nextSidebar && data.next) {
393441
const link = nextSidebar.querySelector('a');
394-
link.href = `./post.html?id=${data.next.id}`;
395-
link.textContent = `${data.next.title} →`;
396-
link.title = data.next.title;
397-
nextSidebar.style.display = 'list-item';
442+
if (link) {
443+
link.href = `./post.html?id=${data.next.id}`;
444+
link.textContent = `Newer: ${data.next.title} →`;
445+
link.title = data.next.title;
446+
nextSidebar.style.display = 'list-item';
447+
}
398448
}
399449

400450
} catch (err) {

post.html

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ <h1 id="title">Loading...</h1>
199199

200200
<div class="post-nav">
201201
<a id="prevPost" class="btn btn-ghost" href="#" style="visibility:hidden;">
202-
<i class="bi bi-arrow-left"></i> Older
202+
<i class="bi bi-arrow-left"></i> Newer
203203
</a>
204204
<a id="nextPost" class="btn btn-ghost" href="#" style="visibility:hidden;">
205-
Newer <i class="bi bi-arrow-right"></i>
205+
Older <i class="bi bi-arrow-right"></i>
206206
</a>
207207
</div>
208208
</article>
@@ -316,49 +316,18 @@ <h1 id="title">Loading...</h1>
316316
import { getPost } from './assets/app.js';
317317

318318
async function load() {
319-
// Load post
319+
// Load post (this also handles navigation via loadPostNavigation)
320320
await getPost();
321321

322322
// Compute reading time
323323
const content = document.querySelector('#post-content');
324324
const meta = document.querySelector('#meta');
325325
const words = (content?.innerText || '').trim().split(/\s+/).length;
326326
const minutes = Math.max(1, Math.round(words / 200));
327-
const date = new Date().toISOString().slice(0, 10);
328-
meta.innerHTML = `<i class="bi bi-clock"></i> ${minutes} min read · <i class="bi bi-calendar3"></i> ${date}`;
329327

330-
// Fetch list for prev/next indexing
331-
const res = await fetch('https://nijikade-backend.vercel.app/api/post?type=blog');
332-
const data = await res.json();
333-
const list = (data.posts || []).flatMap(g => g.posts);
334-
const url = new URL(window.location.href);
335-
const id = url.searchParams.get('id');
336-
const idx = list.findIndex(p => String(p.id) === String(id));
337-
338-
const prev = list[idx - 1];
339-
const next = list[idx + 1];
340-
341-
// Bottom nav buttons
342-
const prevBtn = document.querySelector('#prevPost');
343-
const nextBtn = document.querySelector('#nextPost');
344-
345-
// Sidebar nav items
346-
const prevSidebar = document.querySelector('#prevPostSidebar');
347-
const nextSidebar = document.querySelector('#nextPostSidebar');
348-
349-
if (prev) {
350-
prevBtn.href = `./post.html?id=${prev.id}`;
351-
prevBtn.style.visibility = 'visible';
352-
prevSidebar.style.display = 'block';
353-
prevSidebar.querySelector('a').href = `./post.html?id=${prev.id}`;
354-
}
355-
356-
if (next) {
357-
nextBtn.href = `./post.html?id=${next.id}`;
358-
nextBtn.style.visibility = 'visible';
359-
nextSidebar.style.display = 'block';
360-
nextSidebar.querySelector('a').href = `./post.html?id=${next.id}`;
361-
}
328+
// Get date from the loaded post if available
329+
const postDate = document.title.includes('·') ? '' : '';
330+
meta.innerHTML = `<i class="bi bi-clock"></i> ${minutes} min read`;
362331
}
363332

364333
load();

0 commit comments

Comments
 (0)