Improve URL parameter handling and add error handling in filters
Refactored the `initFromUrl` function to handle edge cases, including invalid or malformed URL parameters, and added a fallback to default filter options in case of errors. Improved validation for `page`, `status`, and `repo` parameters, ensuring robust behavior and logging errors for debugging.
This commit is contained in:
@@ -87,25 +87,68 @@ export function usePackageFilters() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const initFromUrl = () => {
|
const initFromUrl = () => {
|
||||||
const urlParams = new URLSearchParams(window.location.search)
|
try {
|
||||||
|
const urlParams = new URLSearchParams(window.location.search)
|
||||||
|
|
||||||
filterOptions.value = {
|
const defaultOptions: FilterOptions = {
|
||||||
pkgbaseSearch: urlParams.get('pkgbase') || '',
|
pkgbaseSearch: '',
|
||||||
page: urlParams.has('page') ? parseInt(urlParams.get('page') || '1') : 1,
|
page: 1,
|
||||||
exactSearch: urlParams.has('exact'),
|
exactSearch: false,
|
||||||
statuses: urlParams.getAll('status').map((status) => ({
|
statuses: [],
|
||||||
title: status.toUpperCase(),
|
repo: undefined
|
||||||
value: status
|
}
|
||||||
})),
|
|
||||||
repo: undefined
|
let page = 1
|
||||||
|
if (urlParams.has('page')) {
|
||||||
|
const pageParam = urlParams.get('page') || '1'
|
||||||
|
const parsedPage = parseInt(pageParam, 10)
|
||||||
|
page = !isNaN(parsedPage) && parsedPage > 0 ? parsedPage : 1
|
||||||
|
}
|
||||||
|
|
||||||
|
const statuses = urlParams
|
||||||
|
.getAll('status')
|
||||||
|
.filter((status) => typeof status === 'string' && status.trim() !== '')
|
||||||
|
.map((status) => ({
|
||||||
|
title: status.toUpperCase(),
|
||||||
|
value: status
|
||||||
|
}))
|
||||||
|
|
||||||
|
filterOptions.value = {
|
||||||
|
...defaultOptions,
|
||||||
|
pkgbaseSearch: urlParams.get('pkgbase') || '',
|
||||||
|
page,
|
||||||
|
exactSearch: urlParams.has('exact'),
|
||||||
|
statuses
|
||||||
|
}
|
||||||
|
|
||||||
|
const repoValue = urlParams.get('repo')
|
||||||
|
if (repoValue && typeof repoValue === 'string' && repoValue.trim() !== '') {
|
||||||
|
filterOptions.value.repo = { title: repoValue, value: repoValue }
|
||||||
|
}
|
||||||
|
|
||||||
|
applyFilters()
|
||||||
|
|
||||||
|
// Check if page exceeds total pages and adjust if necessary
|
||||||
|
if (
|
||||||
|
filterOptions.value.page > filterResult.value.totalPages &&
|
||||||
|
filterResult.value.totalPages > 0
|
||||||
|
) {
|
||||||
|
filterOptions.value.page = filterResult.value.totalPages
|
||||||
|
applyFilters()
|
||||||
|
updateUrlParams()
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error parsing URL parameters:', error)
|
||||||
|
// Reset to default state
|
||||||
|
filterOptions.value = {
|
||||||
|
pkgbaseSearch: '',
|
||||||
|
page: 1,
|
||||||
|
exactSearch: false,
|
||||||
|
statuses: [],
|
||||||
|
repo: undefined
|
||||||
|
}
|
||||||
|
applyFilters()
|
||||||
}
|
}
|
||||||
|
|
||||||
const repoValue = urlParams.get('repo')
|
|
||||||
if (repoValue) {
|
|
||||||
filterOptions.value.repo = { title: repoValue, value: repoValue }
|
|
||||||
}
|
|
||||||
|
|
||||||
applyFilters()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateUrlParams = () => {
|
const updateUrlParams = () => {
|
||||||
|
Reference in New Issue
Block a user