[chore] Switch Homepage and Photos to page endpoints

This commit is contained in:
2022-05-30 20:47:31 +02:00
parent 9792165272
commit c406278baf
4 changed files with 124 additions and 120 deletions

77
src/routes/photos.ts Normal file
View File

@@ -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<RequestHandlerOutput> {
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,
}
}
}