79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
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: PageServerLoad = async ({ url }) => {
|
|
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 }
|
|
}
|
|
}`)
|
|
|
|
const { data } = res
|
|
|
|
return {
|
|
photos: data.photos,
|
|
filteredCountryExists: data.country.length > 0,
|
|
totalPhotos: data.total_published[0].count.id,
|
|
}
|
|
} catch (err) {
|
|
throw error(500, err.message)
|
|
}
|
|
} |