Skip to content

Commit 6f86830

Browse files
committed
fix: improve filter query handling in useFiltersQuery
#100 - Added a check for the comparator to prevent errors when it is undefined. - Refactored value handling to ensure proper trimming and mapping of filter values. - Enhanced date handling logic to correctly format date values in the query.
1 parent 5e2ada7 commit 6f86830

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

apps/web/src/composables/useFiltersQuery.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,10 @@ export function useFiltersQuery(columns: Ref<QTableProps['columns'] & { type: st
423423
const router = useRouter()
424424
const query = { ...$route.query }
425425
const comparator = comparatorTypes.value.find((comp) => comp.value === filter.operator)
426-
const filterKey = `${FILTER_PREFIX}${comparator?.querySign}${filter.key}${FILTER_SUFFIX}`
427-
const value = comparator?.multiplefields ? filter.value.split(',').map((v) => v.trim()) : filter.value.trim()
426+
if (!comparator) return
427+
428+
const filterKey = `${FILTER_PREFIX}${comparator.querySign}${filter.key}${FILTER_SUFFIX}`
429+
const scalarValue = typeof filter.value === 'undefined' || filter.value === null ? '' : String(filter.value).trim()
428430

429431
// Remove any existing filter for the same field
430432
for (const key in query) {
@@ -444,22 +446,28 @@ export function useFiltersQuery(columns: Ref<QTableProps['columns'] & { type: st
444446
switch (filter.operator) {
445447
case '@':
446448
if (filter.items && filter.items.length > 0) {
447-
query[filterKey] = filter.items.map((item) => `${comparator?.prefix || ''}${item}${comparator?.suffix || ''}`)
449+
query[filterKey] = filter.items.map((item) => `${comparator.prefix || ''}${item}${comparator.suffix || ''}`)
450+
} else if (scalarValue) {
451+
query[filterKey] = scalarValue
452+
.split(',')
453+
.map((item) => item.trim())
454+
.filter((item) => item.length > 0)
455+
.map((item) => `${comparator.prefix || ''}${item}${comparator.suffix || ''}`)
448456
}
449457
break
450458

451459
case '~':
452-
if (value) {
453-
query[filterKey] = value
460+
if (scalarValue) {
461+
query[filterKey] = scalarValue
454462
}
455463
break
456464

457465
default:
458-
if (comparator?.type.includes('date') && value) {
459-
const dateValue = dayjs(value as string).toISOString()
460-
query[filterKey] = `${comparator?.prefix || ''}${dateValue}${comparator?.suffix || ''}`
461-
} else if (value) {
462-
query[filterKey] = `${comparator?.prefix || ''}${value}${comparator?.suffix || ''}`
466+
if (comparator.type.includes('date') && scalarValue) {
467+
const dateValue = dayjs(scalarValue as string).toISOString()
468+
query[filterKey] = `${comparator.prefix || ''}${dateValue}${comparator.suffix || ''}`
469+
} else if (scalarValue) {
470+
query[filterKey] = `${comparator.prefix || ''}${scalarValue}${comparator.suffix || ''}`
463471
}
464472
break
465473
}

0 commit comments

Comments
 (0)