Skip to content

Commit c57baca

Browse files
committed
feat: update useFiltersQuery to accept columnTypes and enhance filter functionality
1 parent 1a2ccda commit c57baca

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

apps/web/src/components/core/edit-filters.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export default defineNuxtComponent({
149149
},
150150
},
151151
},
152-
setup({ columns, initialFilter }) {
153-
const { fieldTypes, comparatorTypes, writeFilter } = useFiltersQuery(ref(columns))
152+
setup({ columns, initialFilter, columnsType }) {
153+
const { fieldTypes, comparatorTypes, writeFilter } = useFiltersQuery(ref(columns), ref(columnsType))
154154
155155
const detectInitialOperator = () => {
156156
if (!initialFilter || !initialFilter.querySign) return ''

apps/web/src/components/core/pan-filters.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export default defineComponent({
9898
default: () => [],
9999
},
100100
},
101-
setup({ columns }) {
102-
const { countFilters, hasFilters, getFilters, removeFilter } = useFiltersQuery(ref(columns))
101+
setup({ columns, columnsType }) {
102+
const { countFilters, hasFilters, getFilters, removeFilter } = useFiltersQuery(ref(columns), ref(columnsType))
103103
104104
return {
105105
countFilters,

apps/web/src/composables/useFiltersQuery.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { QTableProps } from "quasar"
22
import type { LocationQueryValue } from "vue-router"
3+
import dayjs from "dayjs"
34

45
const fieldTypes = ref<
56
{
@@ -27,6 +28,11 @@ export type ComparatorType = {
2728
suffix: string
2829
}
2930

31+
export type ColumnType = {
32+
name: string
33+
type: string
34+
}
35+
3036
const comparatorTypes = ref<ComparatorType[]>([
3137
{
3238
label: 'Égal à',
@@ -220,10 +226,15 @@ const sanitizeSearchString = (search: string) => {
220226
return search
221227
}
222228

223-
const getSearchString = (columns: Ref<QTableProps['columns'] & { type: string }[]>, search: LocationQueryValue | LocationQueryValue[], fieldLabel: string) => {
229+
const getSearchString = (
230+
columns: Ref<QTableProps['columns'] & { type: string }[]>,
231+
search: LocationQueryValue | LocationQueryValue[],
232+
fieldLabel: string,
233+
columnTypes?: Ref<ColumnType[]>
234+
) => {
224235
const field = columns.value?.find((f) => f.name === fieldLabel.replace('[]', ''))
225-
console.log('field', fieldLabel, field)
226236
if (!field) return ''
237+
const fieldType = columnTypes?.value.find((col) => col.name === fieldLabel.replace('[]', ''))?.type || 'text'
227238
// if (field.type === 'multiple') {
228239
// const searchArray = Array.isArray(search) ? search : [search]
229240
// return searchArray
@@ -237,9 +248,10 @@ const getSearchString = (columns: Ref<QTableProps['columns'] & { type: string }[
237248
// if (Array.isArray(search)) {
238249
// return search.join(' ou ')
239250
// }
240-
// if (field?.type === 'date') {
241-
// return dayjs(search).format('DD/MM/YYYY')
242-
// }
251+
console.log('fieldType', fieldType, fieldLabel, search, columnTypes)
252+
if (fieldType === 'date') {
253+
return dayjs(search!.toString()).format('DD/MM/YYYY')
254+
}
243255
return sanitizeSearchString(search!.toString())
244256
}
245257

@@ -279,7 +291,7 @@ const getComparatorObject = (comparatorSign: string, search?: string) => {
279291
return candidates[0]
280292
}
281293

282-
export function useFiltersQuery(columns: Ref<QTableProps['columns'] & { type: string }[]>) {
294+
export function useFiltersQuery(columns: Ref<QTableProps['columns'] & { type: string }[]>, columnTypes?: Ref<ColumnType[]>) {
283295
const $route = useRoute()
284296

285297
const countFilters = computed(() => {
@@ -304,7 +316,7 @@ export function useFiltersQuery(columns: Ref<QTableProps['columns'] & { type: st
304316

305317
const label = getLabelByName(columns, extract.field) || extract.field
306318
const rawValue = `${$route.query[key]}`
307-
const search = getSearchString(columns, $route.query[key], extract.field)
319+
const search = getSearchString(columns, $route.query[key], extract.field, columnTypes)
308320

309321
if (!search || search === '') {
310322
console.warn(`Invalid search for filter key: ${key} ${filteredKey}`, {

0 commit comments

Comments
 (0)