95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { error } from '@sveltejs/kit'
|
|
import type { PageServerLoad } from './$types'
|
|
import { PUBLIC_LIST_AMOUNT } from '$env/static/public'
|
|
import { fetchAPI, photoFields } from '$utils/api'
|
|
|
|
export const config = {
|
|
runtime: 'edge'
|
|
}
|
|
|
|
export const load = (async ({ params, setHeaders }) => {
|
|
try {
|
|
const { location: slug } = params
|
|
|
|
// Query
|
|
const res = await fetchAPI(`query {
|
|
location (
|
|
filter: {
|
|
slug: { _eq: "${slug}" },
|
|
status: { _eq: "published" },
|
|
},
|
|
) {
|
|
id
|
|
name
|
|
slug
|
|
description
|
|
date_updated
|
|
illustration_desktop { id }
|
|
illustration_desktop_2x { id }
|
|
illustration_mobile { id }
|
|
credits {
|
|
credit_id {
|
|
name
|
|
website
|
|
}
|
|
}
|
|
country {
|
|
name
|
|
slug
|
|
flag { id }
|
|
}
|
|
has_poster
|
|
acknowledgement
|
|
}
|
|
|
|
photos: photo (
|
|
filter: {
|
|
location: { slug: { _eq: "${slug}" }},
|
|
status: { _eq: "published" },
|
|
},
|
|
sort: "-date_created",
|
|
limit: ${PUBLIC_LIST_AMOUNT},
|
|
page: 1,
|
|
) {
|
|
${photoFields}
|
|
}
|
|
|
|
# Total
|
|
total_published: photo_aggregated (filter: { location: { slug: { _eq: "${slug}" }}}) {
|
|
count { location }
|
|
}
|
|
|
|
# Shop product
|
|
product (
|
|
filter: {
|
|
location: { slug: { _eq: "${slug}" }},
|
|
status: { _eq: "published" },
|
|
}
|
|
) {
|
|
photos_product {
|
|
directus_files_id {
|
|
id
|
|
}
|
|
}
|
|
}
|
|
}`)
|
|
|
|
const { data: { location: location, photos, total_published, product } } = res
|
|
|
|
if (!location.length || location.length && params.country !== location[0].country.slug) {
|
|
throw error(404, 'This location is not available… yet!')
|
|
}
|
|
|
|
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=604799' })
|
|
|
|
return {
|
|
location: location[0],
|
|
photos,
|
|
totalPhotos: photos.length ? total_published[0].count.location : 0,
|
|
product: product[0],
|
|
}
|
|
} catch (err) {
|
|
throw error(500, err.message)
|
|
}
|
|
}) satisfies PageServerLoad
|