fix: bring back updated data on Photos page

This commit is contained in:
2024-11-12 15:01:36 +01:00
parent efd4a558af
commit d9708c5bfe

View File

@@ -4,7 +4,7 @@
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_INCREMENT } from '$env/static/public' import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_INCREMENT } from '$env/static/public'
import { page, navigating } from '$app/stores' import { page, navigating } from '$app/stores'
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { getContext } from 'svelte' import { getContext, onMount } from 'svelte'
import { fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import { quartOut as quartOutSvelte } from 'svelte/easing' import { quartOut as quartOutSvelte } from 'svelte/easing'
import dayjs from 'dayjs' import dayjs from 'dayjs'
@@ -29,9 +29,9 @@
let { data } = $props() let { data } = $props()
let photos = $state<any[]>(data.photos) let photos = $state<any[]>([...data.photos])
const totalPhotos = $derived<number>(data.totalPhotos) const totalPhotos = $derived<number>(data.totalPhotos)
const { filteredCountryExists, settings }: { filteredCountryExists: boolean, settings: any } = data const { filteredCountryExists, settings }: { filteredCountryExists: boolean, settings: any } = data
const { countries, locations }: any = getContext('global') const { countries, locations }: any = getContext('global')
dayjs.extend(relativeTime) dayjs.extend(relativeTime)
@@ -73,15 +73,13 @@
/** /**
* Handle URL query params * Handle URL query params
*/ */
const countryFlagId = $derived(currentCountry ? currentCountry.flag.id : undefined)
// Update URL filtering params from filter values // Update URL filtering params from filter values
const applyFilters = () => { const applyFilters = () => {
urlFiltersParams.set('country', filterCountry) urlFiltersParams.set('country', filterCountry)
urlFiltersParams.set('sort', filterSort) urlFiltersParams.set('sort', filterSort)
let path = `${$page.url.pathname}?${urlFiltersParams.toString()}` const path = `${$page.url.pathname}?${urlFiltersParams.toString()}`
goto(path, { replaceState: true, keepFocus: true, noScroll: true }) goto(path, { keepFocus: true, replaceState: true, noScroll: true })
} }
@@ -109,14 +107,14 @@
* Filters change events * Filters change events
*/ */
/** Country select */ /** Country select */
const handleCountryChange = ({ detail: value }) => { const handleCountryChange = (value: string) => {
filterCountry = value === defaultCountry ? defaultCountry : value filterCountry = value === defaultCountry ? defaultCountry : value
currentPage = 1 currentPage = 1
applyFilters() applyFilters()
} }
/** Sort select */ /** Sort select */
const handleSortChange = ({ detail: value }) => { const handleSortChange = (value: string) => {
filterSort = value === defaultSort ? defaultSort : value filterSort = value === defaultSort ? defaultSort : value
currentPage = 1 currentPage = 1
applyFilters() applyFilters()
@@ -197,6 +195,13 @@
} }
// Workaround: Reactive photos based on data
$effect(() => {
if (data.photos && data.photos !== photos) {
photos = data.photos
}
})
$effect(() => { $effect(() => {
/** /**
* Observers * Observers
@@ -233,6 +238,16 @@
existingPhotos.forEach(el => observerPhotos.observe(el)) existingPhotos.forEach(el => observerPhotos.observe(el))
// Destroy
return () => {
// Disconnect observers
observerPhotos && observerPhotos.disconnect()
mutationPhotos && mutationPhotos.disconnect()
}
})
onMount(() => {
/** /**
* Animations * Animations
*/ */
@@ -256,14 +271,6 @@
// Run animation // Run animation
requestAnimationFrame(animation.play) requestAnimationFrame(animation.play)
// Destroy
return () => {
// Disconnect observers
observerPhotos && observerPhotos.disconnect()
mutationPhotos && mutationPhotos.disconnect()
}
}) })
</script> </script>
@@ -307,10 +314,10 @@
onchange={handleCountryChange} onchange={handleCountryChange}
value={filterCountry} value={filterCountry}
> >
{#if countryFlagId} {#if currentCountry?.flag?.id}
<Image <Image
class="icon" class="icon"
id={countryFlagId} id={currentCountry?.flag?.id}
sizeKey="square-small" sizeKey="square-small"
width={26} height={26} width={26} height={26}
alt="{filterCountry} flag" alt="{filterCountry} flag"