Handle inexistant location page

This commit is contained in:
2022-06-24 12:26:38 +02:00
parent 4c0b7101ea
commit 3e13a86581

View File

@@ -16,14 +16,14 @@ export const photoFields = `
export async function get({ params }: RequestEvent): Promise<RequestHandlerOutput> { export async function get({ params }: RequestEvent): Promise<RequestHandlerOutput> {
try { try {
const { location } = params const { location: slug } = params
// Query // Query
const res = await fetchAPI(` const res = await fetchAPI(`
query { query {
location ( location (
filter: { filter: {
slug: { _eq: "${location}" }, slug: { _eq: "${slug}" },
status: { _eq: "published" }, status: { _eq: "published" },
} }
) { ) {
@@ -47,7 +47,7 @@ export async function get({ params }: RequestEvent): Promise<RequestHandlerOutpu
photos: photo ( photos: photo (
filter: { filter: {
location: { slug: { _eq: "${location}" }} location: { slug: { _eq: "${slug}" }}
}, },
sort: "-date_created", sort: "-date_created",
limit: ${import.meta.env.VITE_LIST_AMOUNT}, limit: ${import.meta.env.VITE_LIST_AMOUNT},
@@ -57,19 +57,22 @@ export async function get({ params }: RequestEvent): Promise<RequestHandlerOutpu
} }
# Total # Total
total_published: photo_aggregated (filter: { location: { slug: { _eq: "${location}" }}}) { total_published: photo_aggregated (filter: { location: { slug: { _eq: "${slug}" }}}) {
count { location } count { location }
} }
} }
`) `)
const { data: { location, photos, total_published } } = res
const { data } = res if (!location.length) {
return { status: 404 }
}
return { return {
body: { body: {
location: data.location[0], location: location[0],
photos: data.photos, photos: photos,
totalPhotos: data.photos.length ? data.total_published[0].count.location : 0, totalPhotos: photos.length ? total_published[0].count.location : 0,
} }
} }
} catch (error) { } catch (error) {