69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { error } from '@sveltejs/kit'
|
|
import type { Actions, PageServerLoad } from './$types'
|
|
import { fetchAPI } from '$utils/api'
|
|
import { getRandomItems } from '$utils/functions'
|
|
import subscribe from '$utils/forms/subscribe'
|
|
|
|
|
|
/**
|
|
* Page Data
|
|
*/
|
|
export const load: PageServerLoad = async ({ setHeaders }) => {
|
|
try {
|
|
// Get total of published photos
|
|
const totalRes = await fetchAPI(`query {
|
|
photo (
|
|
filter: {
|
|
favorite: { _eq: true },
|
|
status: { _eq: "published" },
|
|
},
|
|
limit: -1,
|
|
) {
|
|
id
|
|
}
|
|
}`)
|
|
const { data: { photo: photosIds }} = totalRes
|
|
|
|
// Get random photos
|
|
const randomPhotosIds = [...getRandomItems(photosIds, 11)].map(({ id }) => id)
|
|
|
|
// Query these random photos from IDs
|
|
const photosRes = await fetchAPI(`query {
|
|
photo (filter: { id: { _in: "${randomPhotosIds}" }}) {
|
|
slug
|
|
title
|
|
city
|
|
location {
|
|
name
|
|
slug
|
|
country {
|
|
slug
|
|
name
|
|
flag { id }
|
|
}
|
|
}
|
|
image { id }
|
|
}
|
|
}`)
|
|
const { data: { photo: photos }} = photosRes
|
|
|
|
if (photos) {
|
|
setHeaders({ 'Cache-Control': 'public, max-age=1, stale-while-revalidate=599' })
|
|
|
|
return {
|
|
photos,
|
|
}
|
|
}
|
|
} catch (err) {
|
|
throw error(500, err.message)
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Form Data
|
|
*/
|
|
export const actions: Actions = {
|
|
// Form newsletter subscription
|
|
subscribe,
|
|
} |