[chore] Switch Homepage and Photos to page endpoints
This commit is contained in:
77
src/routes/photos.ts
Normal file
77
src/routes/photos.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user