🚧 Switch to monorepo with Turbo
This commit is contained in:
91
apps/website/src/routes/(site)/photos/+page.server.ts
Normal file
91
apps/website/src/routes/(site)/photos/+page.server.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
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'
|
||||
|
||||
|
||||
/**
|
||||
* Page Data
|
||||
*/
|
||||
export const load: PageServerLoad = 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user