From c406278baff0b9876cd36c87e0d180d7f54ea593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Mon, 30 May 2022 20:47:31 +0200 Subject: [PATCH] [chore] Switch Homepage and Photos to page endpoints --- src/routes/index.svelte | 38 +---------------- src/routes/index.ts | 39 +++++++++++++++++ src/routes/photos.svelte | 90 ++++------------------------------------ src/routes/photos.ts | 77 ++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 120 deletions(-) create mode 100644 src/routes/index.ts create mode 100644 src/routes/photos.ts diff --git a/src/routes/index.svelte b/src/routes/index.svelte index d36eb8a..3f31ed5 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -144,40 +144,4 @@ - - - - \ No newline at end of file + \ No newline at end of file diff --git a/src/routes/index.ts b/src/routes/index.ts new file mode 100644 index 0000000..2ffa9a4 --- /dev/null +++ b/src/routes/index.ts @@ -0,0 +1,39 @@ +import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit' +import { fetchAPI } from '$utils/api' + +export async function get({}: RequestEvent): Promise { + try { + const res = await fetchAPI(` + query { + photo (limit: 11, sort: ["-date_created"]) { + slug + title + city + location { + name + slug + country { + slug + name + flag { id } + } + } + image { id } + } + } + `) + + const { data } = res + + return { + body: { + photos: data.photo + } + } + + } catch (error) { + return { + status: 404, + } + } +} \ No newline at end of file diff --git a/src/routes/photos.svelte b/src/routes/photos.svelte index bfa797f..66af083 100644 --- a/src/routes/photos.svelte +++ b/src/routes/photos.svelte @@ -9,6 +9,7 @@ import relativeTime from 'dayjs/plugin/relativeTime.js' import anime from 'animejs' import type { AnimeTimelineInstance } from 'animejs' + import { fetchAPI } from '$utils/api' import { map, lerp, throttle } from '$utils/functions' // Components import Metas from '$components/Metas.svelte' @@ -50,20 +51,22 @@ /** * Filters */ + const defaultCountry: string = import.meta.env.VITE_FILTERS_DEFAULT_COUNTRY + const defaultSort: string = import.meta.env.VITE_FILTERS_DEFAULT_SORT const urlFiltersParams = new URLSearchParams() let filtered: boolean let filterCountry = $page.url.searchParams.get('country') || defaultCountry let filterSort = $page.url.searchParams.get('sort') || defaultSort let countryFlagId: string $: filtered = filterCountry !== defaultCountry || filterSort !== defaultSort - $: latestPhoto = photos[0] + $: latestPhoto = photos && photos[0] $: currentCountry = countries.find((country: any) => country.slug === filterCountry) // Pages related informations let currentPage = 1 let ended: boolean let currentPhotosAmount: number - $: currentPhotosAmount = photos.length + $: currentPhotosAmount = photos && photos.length $: ended = currentPhotosAmount === totalPhotos @@ -433,7 +436,7 @@
- {#if photos.length} + {#if photos}
{#each photos as { id, image, slug, location, title, city }, index (id)}
@@ -499,83 +502,4 @@
- - - - + \ No newline at end of file diff --git a/src/routes/photos.ts b/src/routes/photos.ts new file mode 100644 index 0000000..b41a9e8 --- /dev/null +++ b/src/routes/photos.ts @@ -0,0 +1,77 @@ +import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit' +import { fetchAPI } from '$utils/api' + +// Default filters values +const defaultCountry = String(import.meta.env.VITE_FILTERS_DEFAULT_COUNTRY) +const defaultSort = String(import.meta.env.VITE_FILTERS_DEFAULT_SORT) + +export async function get({ url }: RequestEvent): Promise { + try { + // Query parameters + const queryCountry = url.searchParams.get('country') || defaultCountry + const querySort = url.searchParams.get('sort') || defaultSort + + // Query + const res = await fetchAPI(` + query { + photos: photo ( + filter: { + ${queryCountry !== 'all' ? `location: { country: { slug: { _eq: "${queryCountry}" }}},` : ''} + status: { _eq: "published" }, + }, + sort: "${querySort === 'latest' ? '-' : ''}date_created", + limit: ${import.meta.env.VITE_GRID_AMOUNT}, + page: 1, + ) { + id + title + slug + image { + id + title + } + location { + slug + name + region + country { + slug + name + flag { id } + } + } + city + date_created + } + + country: country ( + filter: { + slug: { _eq: "${queryCountry}" }, + status: { _eq: "published" }, + }, + ) { + slug + } + + # Total + total_published: photo_aggregated ${queryCountry !== 'all' ? `(filter: { location: { country: { slug: { _eq: "${queryCountry}" }}}})` : ''} { + count { id } + } + } + `) + + const { data } = res + + return { + body: { + photos: data.photos, + filteredCountryExists: data.country.length > 0, + totalPhotos: data.total_published[0].count.id, + } + } + } catch (error) { + return { + status: 404, + } + } +} \ No newline at end of file