get all poems workin, get semantic search workin, get thoughts section working
This commit is contained in:
15
src/routes/(app)/thoughts/+page.server.ts
Normal file
15
src/routes/(app)/thoughts/+page.server.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export const load = async ({ fetch, url }) => {
|
||||
const limit = 8;
|
||||
|
||||
const page = Number(url.searchParams.get('page')) || 1;
|
||||
|
||||
const response = await fetch(`/api/thoughts?limit=${limit}&page=${page}`);
|
||||
const { posts, total } = await response.json();
|
||||
|
||||
return {
|
||||
posts,
|
||||
total,
|
||||
page,
|
||||
limit
|
||||
};
|
||||
};
|
85
src/routes/(app)/thoughts/+page.svelte
Normal file
85
src/routes/(app)/thoughts/+page.svelte
Normal file
@@ -0,0 +1,85 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/stores';
|
||||
import { searchResults } from '$lib/store';
|
||||
import type { SearchResult } from '$lib/utils/search';
|
||||
import { onMount } from 'svelte';
|
||||
import type { PageData } from '../thoughts/$types';
|
||||
export let data: PageData;
|
||||
|
||||
let results: SearchResult[] = [];
|
||||
|
||||
searchResults.subscribe((value: SearchResult[]) => {
|
||||
results = value ? value : [];
|
||||
});
|
||||
|
||||
const formatDate = (date: string) => {
|
||||
return new Date(date).toLocaleDateString(undefined, {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric'
|
||||
});
|
||||
};
|
||||
|
||||
let { posts, total } = data;
|
||||
const limit = 8;
|
||||
|
||||
let currentPage = Number($page.url.searchParams.get('page')) || 1;
|
||||
let totalPages = Math.ceil(total / limit);
|
||||
$: $page.url.searchParams.get('page'),
|
||||
(currentPage = Number($page.url.searchParams.get('page')) || 1);
|
||||
|
||||
async function fetchData(page: number) {
|
||||
const response = await fetch(`/api/thoughts?limit=${limit}&page=${page}`);
|
||||
const newData = await response.json();
|
||||
currentPage = page;
|
||||
posts = newData.posts;
|
||||
total = newData.total;
|
||||
totalPages = Math.ceil(total / limit);
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
searchResults.set([]);
|
||||
});
|
||||
|
||||
function navigate(page: number) {
|
||||
fetchData(page);
|
||||
goto(`/thoughts/?page=${page}`, { replaceState: true });
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if results.length <= 0}
|
||||
<div class="container mx-auto flex flex-col items-center">
|
||||
<div class="prose">
|
||||
<h1 class="py-6 pt-10">Thoughts</h1>
|
||||
</div>
|
||||
|
||||
<ul>
|
||||
{#each posts as post}
|
||||
<li class="py-4">
|
||||
<h3 class="pb-1">
|
||||
<a class="link" href={post.path}>
|
||||
{post.meta.title}
|
||||
</a>
|
||||
</h3>
|
||||
<p class="text-sm">{formatDate(post.meta.date)}</p>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</div>
|
||||
{#if total > 1}
|
||||
<nav class="join justify-end py-10">
|
||||
<button
|
||||
class="join-item btn-primary btn btn-outline"
|
||||
on:click={() => navigate(currentPage - 1)}
|
||||
disabled={currentPage === 1}>Prev</button
|
||||
>
|
||||
<button class="join-item btn btn-outline">{currentPage} of {totalPages}</button>
|
||||
<button
|
||||
class="join-item btn btn-primary btn-outline"
|
||||
on:click={() => navigate(currentPage + 1)}
|
||||
disabled={currentPage === totalPages}>Next</button
|
||||
>
|
||||
</nav>
|
||||
{/if}
|
||||
{/if}
|
26
src/routes/(app)/thoughts/[slug]/+page.svelte
Normal file
26
src/routes/(app)/thoughts/[slug]/+page.svelte
Normal file
@@ -0,0 +1,26 @@
|
||||
<script lang="ts">
|
||||
import { searchResults } from '$lib/store';
|
||||
import type { SearchResult } from '$lib/utils/search';
|
||||
import { onMount } from 'svelte';
|
||||
import type { PageData } from './$types';
|
||||
export let data: PageData;
|
||||
|
||||
const { title, date: _date, Content, categories: _ } = data;
|
||||
|
||||
let results: SearchResult[] = [];
|
||||
|
||||
searchResults.subscribe((value: SearchResult[]) => {
|
||||
results = value ? value : [];
|
||||
});
|
||||
onMount(() => {
|
||||
searchResults.set([]);
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if results.length <= 0}
|
||||
<div class="container mx-auto flex flex-col items-center prose px-4">
|
||||
<h1 class="pt-10">{title}</h1>
|
||||
<Content />
|
||||
<a href="/thoughts" class="link py-10">Back to Thoughts</a>
|
||||
</div>
|
||||
{/if}
|
20
src/routes/(app)/thoughts/[slug]/+page.ts
Normal file
20
src/routes/(app)/thoughts/[slug]/+page.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { Metadata } from '$lib/utils/index.js';
|
||||
|
||||
export async function load({ params }) {
|
||||
const post = await import(`../../../../posts/thoughts/${params.slug}.md`);
|
||||
const { title, date, categories, draft } = post.metadata as Metadata;
|
||||
const Content = post.default;
|
||||
const parsedDate = new Date(date.slice(0, date.length - 6));
|
||||
const validDate = `${
|
||||
parsedDate.getMonth() + 1
|
||||
}/${parsedDate.getDate()}/${parsedDate.getFullYear()}`;
|
||||
|
||||
return {
|
||||
Content,
|
||||
title,
|
||||
date: validDate,
|
||||
categories,
|
||||
post,
|
||||
draft
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user