Update the results after pressing the Back button

This commit is contained in:
Arhey 2022-04-22 02:41:03 +03:00
parent 59dde05ee9
commit f22f3cb680
3 changed files with 40 additions and 15 deletions

View file

@ -1,6 +1,6 @@
<script>
import InfiniteLoading from 'svelte-infinite-loading'
import { fetchChannels, hasQuery, countries, filteredChannels, query, search } from '../store.js'
import { fetchChannels, hasQuery, countries, filteredChannels, query, search, setSearchParam, setPageTitle } from '../store.js'
import { onMount, onDestroy } from 'svelte'
import CountryItem from '../components/CountryItem.svelte'
import SearchField from '../components/SearchField.svelte'
@ -19,12 +19,8 @@
$: grouped = _.groupBy($filteredChannels, 'country.code')
function reset() {
limit = initLimit
infiniteId = +new Date()
}
function loadMore({ detail: { loaded, complete } }) {
function loadMore({ detail }) {
let { loaded, complete } = detail
if (limit < _countries.length) {
limit++
loaded()
@ -33,10 +29,16 @@
}
}
function reset() {
infiniteId = +new Date()
limit = initLimit
}
onMount(async () => {
const params = new URLSearchParams(window.location.search)
const q = params.get('q')
if (q) {
setPageTitle(q)
query.set(q)
hasQuery.set(true)
}
@ -45,9 +47,22 @@
_countries = Object.values($countries)
isLoading = false
if ($hasQuery) {
if($hasQuery) {
search($query)
}
window.onpopstate = (event) => {
const q = event.state.q
if (q) {
setPageTitle(q)
query.set(q)
hasQuery.set(true)
search($query)
} else {
setPageTitle(null)
hasQuery.set(false)
}
}
})
</script>