From 8aa3ddfe35411dcff622adb61cc7549450a2160e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?=
Date: Sat, 9 Oct 2021 22:24:37 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Load=20new=20photos=20on=20Locat?=
=?UTF-8?q?ion=20page?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/routes/[country]/[location]/index.svelte | 113 +++++++++++++++++--
src/routes/photos.svelte | 2 +-
src/style/pages/_location.scss | 32 +++---
3 files changed, 117 insertions(+), 30 deletions(-)
diff --git a/src/routes/[country]/[location]/index.svelte b/src/routes/[country]/[location]/index.svelte
index 4706b61..933307c 100644
--- a/src/routes/[country]/[location]/index.svelte
+++ b/src/routes/[country]/[location]/index.svelte
@@ -10,13 +10,77 @@
import Image from '$components/atoms/Image.svelte'
export let data: any
- export let photos: any
+ export let photos: any[]
+ export let lastUpdated: string
+ export let totalPhotos: number
dayjs.extend(advancedFormat)
dayjs.extend(relativeTime)
const { params } = $page
let descriptionEl: HTMLElement
+ let currentPage: number = 1
+ let ended: boolean
+ let currentPhotosAmount: number
+ $: currentPhotosAmount = photos.length
+ $: ended = currentPhotosAmount === totalPhotos
+
+
+ /**
+ * Load photos
+ */
+ // Load more photos from CTA
+ const loadMorePhotos = async () => {
+ // Append more photos from API
+ const newPhotos: any = await loadPhotos(currentPage + 1)
+
+ if (newPhotos) {
+ photos = [...photos, ...newPhotos]
+
+ // Define actions if the number of new photos is the expected ones
+ if (newPhotos.length === Number(import.meta.env.VITE_LIST_INCREMENT)) {
+ // Increment the current page
+ currentPage++
+ }
+
+ // Increment the currently visible amount of photos
+ currentPhotosAmount += newPhotos.length
+ }
+ }
+
+ // [function] Load photos helper
+ const loadPhotos = async (page?: number) => {
+ const res = fetchAPI(`
+ query {
+ photos: photo (
+ filter: {
+ location: { slug: { _eq: "${params.location}" }},
+ status: { _eq: "published" },
+ },
+ sort: "-date_created",
+ limit: ${import.meta.env.VITE_LIST_INCREMENT},
+ page: ${page},
+ ) {
+ title
+ slug
+ image {
+ id
+ title
+ }
+ date_taken
+ date_created
+ }
+ }
+ `)
+ const { data: { photos }} = await res
+
+ if (photos) {
+ // Return new photos
+ return photos
+ } else {
+ throw new Error('Error while loading new photos')
+ }
+ }
@@ -56,7 +120,7 @@
{/each}
·
-
+
Updated
@@ -105,16 +169,17 @@