-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
106 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<script lang="ts"> | ||
import { slide } from 'svelte/transition'; | ||
import * as config from '$lib/config'; | ||
import PostPreview from '$lib/components/custom/post-preview.svelte'; | ||
import type { Post } from '$lib/types'; | ||
// export let data; | ||
let { data } = $props(); | ||
let totalPosts: Post[] = data.posts; | ||
// let currentPage = $derived(parseInt(data.page ?? '0')); | ||
let currentPage = $state(0); | ||
let totalPages = Math.ceil(totalPosts.length / config.pagination); | ||
let start = $derived(currentPage * config.pagination); | ||
let end = $derived( | ||
currentPage === totalPages - 1 ? totalPosts.length - 1 : start + config.pagination - 1 | ||
); | ||
let posts = $derived(totalPosts.slice(start, end + 1)); | ||
let nextPage = $derived(currentPage === totalPages - 1 ? -1 : currentPage + 1); | ||
let previousPage = $derived(currentPage === 0 ? -1 : currentPage - 1); | ||
</script> | ||
|
||
<svelte:head> | ||
<title>{config.title}</title> | ||
</svelte:head> | ||
|
||
<h1 class="capitalize">Category: {data.category}</h1> | ||
|
||
{#if totalPosts.length > 0} | ||
<!-- Posts --> | ||
<section class="list-posts"> | ||
<ul class="list"> | ||
{#each posts as post (post.slug)} | ||
<li class="post" transition:slide> | ||
<PostPreview {post}></PostPreview> | ||
</li> | ||
{/each} | ||
</ul> | ||
</section> | ||
|
||
<!-- Pagination --> | ||
<section class=" flex justify-between items-center px-4 py-3"> | ||
<div class="hidden sm:block"> | ||
Showing <span class="font-semibold">{start + 1}</span> to | ||
<span class="font-semibold">{end + 1}</span> results | ||
</div> | ||
<div class="flex gap-2"> | ||
{#if previousPage >= 0} | ||
<button on:click={() => currentPage--}>Previous</button> | ||
{/if} | ||
{#if nextPage >= 0} | ||
<button on:click={() => currentPage++}>Next</button> | ||
{/if} | ||
</div> | ||
</section> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// import { error } from '@sveltejs/kit'; | ||
|
||
export async function load({ fetch, params }) { | ||
const { category } = params; | ||
|
||
const response = await fetch('../../api/posts'); | ||
const allPosts = await response.json(); | ||
|
||
const posts = allPosts.filter((post: { categories: string[] }) => { | ||
const listCategories = post.categories.map((p) => p.toLowerCase()); | ||
return listCategories.includes(category.toLowerCase()); | ||
}); | ||
|
||
return { | ||
category, | ||
posts | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters