Skip to content

Commit

Permalink
fix: [torrust#601] bug in tags and categories filters
Browse files Browse the repository at this point in the history
Going back with the browser button in the torrent list page produces an
URL with a param like this "tagFilters=os" when the previous filters only contain sone item.

That can be interpreted as a flat option or an array with one element.

The application was applying the first option and the index-api-lib
package expects the tagFilters to be always an array of strings.

Same for categoryFilters.

This only happended using the "back" buttom.
  • Loading branch information
josecelano committed Aug 6, 2024
1 parent de44e47 commit 94a07e0
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions pages/torrents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const tags = useTags();
const rest = useRestApi();
const defaultPageSize = 50;
const queryPageSize = parseInt(route.query?.pageSize as string, 10);
const queryPageSize = isNaN(route.query?.pageSize) ? defaultPageSize : parseInt(route.query?.pageSize as string, 10);
const pageSize: Ref<number> = ref(isNaN(queryPageSize) ? defaultPageSize : queryPageSize);
const queryCategoryFilters = route.query?.categoryFilters as string[] || [];
const categoryFilters: Ref<string[]> = ref(Array.isArray(queryCategoryFilters) ? queryCategoryFilters : [queryCategoryFilters]);
Expand All @@ -113,11 +113,19 @@ const selectedSorting = computed({
watch(() => route.fullPath, () => {
searchQuery.value = route.query.search as string ?? null;
itemsSorting.value = route.query.sorting as string ?? null;
pageSize.value = route.query.pageSize as number ?? null;
currentPage.value = route.query.page as number ?? null;
pageSize.value = isNaN(route.query.pageSize) ? parseInt(route.query.pageSize) : defaultPageSize;
currentPage.value = isNaN(route.query.page) ? parseInt(route.query.page) : 1;
layout.value = route.query.layout as string ?? null;
categoryFilters.value = route.query.categoryFilters as string[] ?? null;
tagFilters.value = route.query.tagFilters as string[] ?? null;
// Ensure categoryFilters is always an array of strings
categoryFilters.value = Array.isArray(route.query.categoryFilters)
? route.query.categoryFilters as string[]
: (route.query.categoryFilters ? [route.query.categoryFilters as string] : []);
// Ensure tagFilters is always an array of strings
tagFilters.value = Array.isArray(route.query.tagFilters)
? route.query.tagFilters as string[]
: (route.query.tagFilters ? [route.query.tagFilters as string] : []);
});
watch([searchQuery, itemsSorting, pageSize, currentPage, layout, categoryFilters, tagFilters], () => {
Expand All @@ -128,8 +136,8 @@ watch([searchQuery, itemsSorting, pageSize, currentPage, layout, categoryFilters
pageSize: pageSize.value,
page: currentPage.value,
layout: layout.value,
categoryFilters: categoryFilters.value,
tagFilters: tagFilters.value
categoryFilters: categoryFilters.value.length > 0 ? categoryFilters.value : [],
tagFilters: tagFilters.value.length > 0 ? tagFilters.value : []
}
});
Expand All @@ -139,8 +147,8 @@ watch([searchQuery, itemsSorting, pageSize, currentPage, layout, categoryFilters
onActivated(() => {
searchQuery.value = route.query.search as string ?? null;
itemsSorting.value = route.query.sorting as string ?? null;
pageSize.value = route.query.pageSize as number ?? null;
currentPage.value = route.query.page as number ?? null;
pageSize.value = route.query.pageSize as number ?? defaultPageSize;
currentPage.value = route.query.page as number ?? 1;
layout.value = route.query.layout as string ?? null;
categoryFilters.value = route.query.categoryFilters as string[] ?? null;
tagFilters.value = route.query.tagFilters as string[] ?? null;
Expand All @@ -167,6 +175,15 @@ function submitSearch () {
}
function loadTorrents () {
const query = {
pageSize: pageSize.value,
page: currentPage.value,
sorting: itemsSorting.value,
categories: categoryFilters.value,
tags: tagFilters.value,
searchQuery: searchQuery.value
};
rest.value.torrent.getTorrents(
{
pageSize: pageSize.value,
Expand Down

0 comments on commit 94a07e0

Please sign in to comment.