import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { fetchAPI } from '$utils/api' import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public' export const load = (async ({ url, setHeaders }) => { try { // Query parameters const queryCountry = url.searchParams.get('country') || PUBLIC_FILTERS_DEFAULT_COUNTRY const querySort = url.searchParams.get('sort') || PUBLIC_FILTERS_DEFAULT_SORT // 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: ${PUBLIC_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}" }}}, status: { _eq: "published" }, } )` : `( filter: { status: { _eq: "published" }, } )`} { count { id } } settings { seo_image_photos { id } } }`) if (res) { setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=86399' }) const { data } = res return { photos: data.photos, filteredCountryExists: data.country.length > 0, totalPhotos: data.total_published[0].count.id, settings: data.settings, } } } catch (err) { throw error(500, err.message) } }) satisfies PageServerLoad