Skip to content

Commit 82b5a5e

Browse files
authored
Fix param persistence when switching styles or maps (#25)
1 parent 96060df commit 82b5a5e

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/components/SearchBoxMap.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, computed } from 'vue';
2+
import { ref } from 'vue';
33
import type { Ref } from 'vue';
44
import OffstylesApi from '@/api/offstylesApi';
55
import loadWheel from './icons/loadWheel.vue';
@@ -31,7 +31,6 @@
3131
isLoading.value = false;
3232
}, 600);
3333
}
34-
const params = computed(()=>{return urlParams.get()});
3534
</script>
3635

3736
<template>
@@ -44,7 +43,7 @@
4443
class="rounded-lg bg-main-800 text-left border border-transparent focus-within:border-main-50 py-2 px-3 text-sm leading-5 text-gray-200 placeholder:text-gray-500 outline-none">
4544
<div class="absolute top-full w-full rounded-lg bg-main-900 border border-main-100 text-sm text-gray-300 mt-1 py-2 px-2 shadow-xl/20 hidden group-focus-within:block z-[999]" v-if="showAutoCompleteDropdown && currentInput">
4645
<loadWheel v-if="isLoading" class="text-gray-300 flex mx-auto w-6 h-6"></loadWheel>
47-
<router-link v-else v-for="(result, index) in autoCompleteResults.slice(0, 6)" :to="{path:`/maps/${result}/?${params}`, query:urlParams.getAsObject()}" :key="index"
46+
<router-link v-else v-for="(result, index) in autoCompleteResults.slice(0, 6)" :to="{path:`/maps/${result}/`, query:urlParams.clearPage()}" :key="index"
4847
@click="$emit('updateMap', result); showAutoCompleteDropdown = false"
4948
class="py-1 px-1.5 hover:bg-main-600 rounded-sm block truncate">{{ result }}
5049
</router-link>

src/components/SearchBoxPlayer.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, computed } from 'vue';
2+
import { ref } from 'vue';
33
import type { Ref } from 'vue';
44
import OffstylesApi from '@/api/offstylesApi';
55
import loadWheel from './icons/loadWheel.vue';
@@ -31,7 +31,6 @@
3131
}
3232
}, 600);
3333
}
34-
const params = computed(()=>{return urlParams.get()});
3534
</script>
3635

3736
<template>
@@ -44,7 +43,7 @@
4443
class="rounded-lg bg-main-800 text-left border border-transparent focus-within:border-main-50 py-2 px-3 text-sm leading-5 text-gray-200 placeholder:text-gray-500 outline-none">
4544
<div class="absolute top-full w-full rounded-lg bg-main-900 border border-main-100 text-sm text-gray-300 mt-1 py-2 px-2 shadow-xl/20 hidden group-focus-within:block z-[999]" v-if="showAutoCompleteDropdown && currentInput">
4645
<loadWheel v-if="isLoading" class="text-gray-300 flex mx-auto w-6 h-6"></loadWheel>
47-
<router-link v-else v-for="(result, index) in autoCompleteResults.slice(0, 6)" :to="{path:`/players/${result[1]}/?${params}`, query:urlParams.getAsObject()}" :key="index"
46+
<router-link v-else v-for="(result, index) in autoCompleteResults.slice(0, 6)" :to="{path:`/players/${result[1]}/`, query:urlParams.clearPage()}" :key="index"
4847
@click="$emit('updatePlayer', result[1]); showAutoCompleteDropdown = false"
4948
class="py-1 px-1.5 hover:bg-main-600 rounded-sm block truncate">{{ result[0] }}
5049
</router-link>

src/components/TimesListPagination.vue

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<script setup lang="ts">
2-
import type { Ref } from 'vue';
32
import type { Time } from '@/types/Time';
4-
import { ref, computed } from 'vue';
5-
import urlParams from '@/utils/urlParams';
3+
import { computed } from 'vue';
4+
import { useRoute } from 'vue-router';
65
const props = defineProps<{
76
limitPerPage: number,
87
times: Time[] | null,
98
isLoading: boolean
109
}>()
1110
const emit = defineEmits(['pagination-Changed']);
12-
const currentPage : Ref<number> = ref(urlParams.getAsObject().page ? Number(urlParams.getAsObject().page) : 1);
11+
const route = useRoute();
12+
13+
// Make currentPage reactive to URL changes
14+
const currentPage = computed(() => {
15+
const pageParam = route.query.page;
16+
return pageParam ? Number(pageParam) : 1;
17+
});
1318
1419
function paginationChanged(page : number){
15-
currentPage.value = page;
16-
emit('pagination-Changed', currentPage.value);
20+
emit('pagination-Changed', page);
1721
}
1822
</script>
1923

src/utils/urlParams.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const update = function(name: string, value : number | string) {
55
if(name === 'style' && value === Style.all){
66
params.delete(name);
77
}
8+
if(name === 'style') {
9+
params.delete('page');
10+
}
811
//cant use router outside of setup component, so need to return the new query and use there instead
912
return Object.fromEntries(params)
1013
};
@@ -19,4 +22,10 @@ const getAsObject = function(){
1922
return Object.fromEntries(params)
2023
}
2124

22-
export default {update, get, getAsObject};
25+
const clearPage = function() {
26+
const params = new URLSearchParams(window.location.search);
27+
params.delete('page');
28+
return Object.fromEntries(params)
29+
}
30+
31+
export default {update, get, getAsObject, clearPage};

0 commit comments

Comments
 (0)