added message when no post

This commit is contained in:
J-onasJones 2024-06-16 20:27:04 +02:00
parent c2ba40c8aa
commit 94b9f6637d

View file

@ -12,12 +12,20 @@
let error = false; let error = false;
let loading = true; let loading = true;
let noPostMessage = '';
onMount(async () => { onMount(async () => {
try { try {
const response = await fetch('https://cdn.jonasjones.dev/blog/index.json'); const response = await fetch('https://cdn.jonasjones.dev/blog/index.json');
posts = await response.json(); posts = await response.json();
if (posts.length === 0) {
loading = false;
noPostMessage = 'No posts found :/';
return;
} else {
// for each post, conver the date string to a Date object // for each post, conver the date string to a Date object
posts.forEach(post => { posts.forEach(post => {
post.date = new Date(post.date); post.date = new Date(post.date);
@ -28,6 +36,7 @@
// sort the posts by date // sort the posts by date
filteredPosts.sort((a, b) => b.date - a.date); filteredPosts.sort((a, b) => b.date - a.date);
loading = false; loading = false;
}
} catch (error) { } catch (error) {
console.error(error); console.error(error);
error = true; error = true;
@ -42,6 +51,7 @@
{#if error} {#if error}
<NotFound /> <NotFound />
{:else} {:else}
<h2 style="text-align: center;">{noPostMessage}</h2>
<div class="postList"> <div class="postList">
{#each filteredPosts as post} {#each filteredPosts as post}
<div class="postDiv"> <div class="postDiv">