Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored logic for blog post labels
- Loading branch information
1 parent
4d229cf
commit 1aad2bd
Showing
5 changed files
with
66 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ALL, queryFromLabels } from './utils'; | ||
|
||
export function makeBlogPostsJsonUrl(labels: string[]): string { | ||
const query = queryFromLabels(labels); | ||
return `/blog/${query}.json`; | ||
} | ||
|
||
export function makeBlogPostsPageUrl(labels: string[]): string { | ||
const query = queryFromLabels(labels); | ||
return query === ALL ? '/blog' : `/blog?l=${query}`; | ||
} | ||
|
||
export function makeBlogPostPageUrl(id: number): string { | ||
return `/blog/${id}`; | ||
} |
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import type { LoadEvent } from '@sveltejs/kit'; | ||
import type { PageLoadEvent } from './$types'; | ||
import type { BlogPost } from '$lib/public/blog/types'; | ||
import { parseQuery, ALL } from '$lib/public/blog/utils'; | ||
import { parseQuery } from '$lib/public/blog/utils'; | ||
import { makeBlogPostsJsonUrl } from '$lib/public/blog/urls'; | ||
|
||
export async function load({ fetch, url }: LoadEvent) { | ||
const query = url.searchParams.get('l'); | ||
const target = `/blog/${query ?? ALL}.json`; | ||
export async function load({ fetch, url }: PageLoadEvent) { | ||
const query = parseQuery(url.searchParams.get('l') || ''); | ||
const target = makeBlogPostsJsonUrl(query.labels ?? []); | ||
|
||
const response = await fetch(target); | ||
const blogOutput = await response.json(); | ||
|
||
const { labels: selectedLabels } = parseQuery(query ?? ALL); | ||
const posts = (blogOutput.posts ?? []) as BlogPost[]; | ||
const labels = (blogOutput.labels ?? []) as string[]; | ||
|
||
return { | ||
posts, | ||
labels, | ||
selectedLabels, | ||
selectedLabels: query.labels, | ||
}; | ||
} |
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