From 30d9e63afec57d48c889bcca1a9cf5edaa020646 Mon Sep 17 00:00:00 2001 From: Pablo Hdez <62877300+pheralb@users.noreply.github.com> Date: Tue, 24 Sep 2024 08:41:12 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Create=20utility=20to=20ge?= =?UTF-8?q?t=20categories=20+=20fix=20prerender=20function=20with=20``entr?= =?UTF-8?q?ies``?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data/index.ts | 19 +++++++++++++++++- src/routes/+layout.svelte | 21 ++++++++++---------- src/routes/directory/[slug]/+page.ts | 29 ++++++++++++++-------------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/data/index.ts b/src/data/index.ts index 54092b2..3710562 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -3,4 +3,21 @@ import { svgs } from './svgs'; export const svgsData = svgs.map((svg: iSVG, index: number) => { return { id: index, ...svg }; -}); \ No newline at end of file +}); + +export const getCategories = () => { + const categories = svgs + .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) + .filter((category, index, array) => array.indexOf(category) === index); + return categories; +}; + +export const getCategoriesForDirectory = () => { + const categories = svgs + .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) + .filter((category, index, array) => array.indexOf(category) === index) + .map((category) => ({ + slug: category.toLowerCase() + })); + return categories; +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 8ee90e2..8b38947 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -11,9 +11,7 @@ // Get categories: import { svgs } from '@/data/svgs'; - const categories = svgs - .flatMap((svg) => (Array.isArray(svg.category) ? svg.category : [svg.category])) - .filter((category, index, array) => array.indexOf(category) === index); + const categories = getCategories(); // Get category counts: let categoryCounts: Record = {}; @@ -30,6 +28,7 @@ // Layout: import Navbar from '@/components/navbar.svelte'; + import { getCategories } from '@/data'; @@ -38,22 +37,22 @@ -
+
diff --git a/src/routes/directory/[slug]/+page.ts b/src/routes/directory/[slug]/+page.ts index eb91b6b..595a5ac 100644 --- a/src/routes/directory/[slug]/+page.ts +++ b/src/routes/directory/[slug]/+page.ts @@ -1,35 +1,34 @@ -import { error } from '@sveltejs/kit'; -import type { PageLoad } from './$types'; - -import { svgs } from '@/data/svgs'; +import type { PageLoad, EntryGenerator } from './$types'; import type { iSVG } from '@/types/svg'; +import { error } from '@sveltejs/kit'; +import { svgs } from '@/data/svgs'; +import { getCategoriesForDirectory } from '@/data'; + +export const entries: EntryGenerator = () => { + const categories = getCategoriesForDirectory(); + return categories; +}; + export const prerender = true; export const load = (async ({ params }) => { const { slug } = params; - // Check if slug is valid: - if (!slug) { - return error(404, 'Not found'); - } - - // Filter out the svg with the matching slug: const svgsByCategory = svgs.filter((svg: iSVG) => { if (Array.isArray(svg.category)) { - return svg.category.some((categoryItem) => categoryItem.toLowerCase() === slug); + return svg.category.some((categoryItem) => categoryItem.toLowerCase() === slug.toLowerCase()); } else { - return svg.category.toLowerCase() === slug; + return svg.category.toLowerCase() === slug.toLowerCase(); } }); - // If SVGs array is empty, category can't exist if (svgsByCategory.length === 0) { - return error(404, 'Not found'); + throw error(404, 'Category not found'); } return { - category: slug as string, + category: slug, svgs: svgsByCategory }; }) satisfies PageLoad;