@@ -10,7 +10,7 @@ q-select(
1010)
1111 template( v-slot:option ="{ index, itemProps, opt, selected, toggleOption }" )
1212 q-item-label( v-bind ="itemProps" v-if ="opt.header" header ) {{ opt.label }}
13- q-item( v-bind ="itemProps" v-else @click.capture ='addFilter({index, value: opt} )' )
13+ q-item( v-bind ="itemProps" v-else @click.capture ='addFilter(opt)' )
1414 q-item-section( side )
1515 q-icon( :name ="opt.icon" : color= "opt.color" size ="xs" )
1616 q-item-section
@@ -52,12 +52,13 @@ onMounted(() => {
5252})
5353
5454watch (() => route .query , () => {
55- filters .value = []
5655 getFilters ()
5756})
5857
5958const filters = ref <Option []>([])
6059const getFilters = () => {
60+ filters .value = []
61+
6162 // Use destructuring assignment to clone the route.query object
6263 const query = { ... route .query };
6364
@@ -67,24 +68,29 @@ const getFilters = () => {
6768 .filter (([key , value ]) => key .startsWith (' filters[@' ) && value !== null )
6869 .map (([key , value ]) => {
6970 group = key .replace (' filters[@' , ' ' ).replace (' ]' , ' ' ).replace (' []' , ' ' )
70- return Array .isArray (value ) ? value : [value ]
71+ if (Array .isArray (value )) {
72+ return value .map ((val : string ) => {
73+ return {
74+ value: val ,
75+ group
76+ }
77+ })
78+ } else {
79+ return {
80+ value ,
81+ group
82+ }
83+ }
84+
7185 })
7286 .flat ()
73- .map (value => options .value .find (option => option .value ?.toString () === value ?.toString () && option .group === group ))
74- .filter (option => option !== undefined ) as Option []
87+ .map (filter => options .value .find (option => option .value ?.toString () === filter . value ?.toString () && option .group === filter . group ))
88+ .filter (option => option !== undefined );
7589 filters .value = filteredOptions ;
7690}
7791
7892
7993const options = computed (() => {
80- // const categories: Option[] = categoriesData.value.data.map((category: Category) => {
81- // return {
82- // label: category.name,
83- // value: category._id,
84- // group: 'categories'
85- // }
86- // }) ?? []
87- // categories.unshift({ label: 'Catégories', header: true })
8894 const ticketTypeOptions: Option [] = ticketType .map ((type ) => {
8995 return {
9096 label: type .label ,
@@ -104,6 +110,14 @@ const options = computed(() => {
104110 color: state .color ?? ' '
105111 }
106112 }) ?? []
113+ const categories: Option [] = categoriesData .value .data .map ((category : Category ) => {
114+ return {
115+ label: category .name ,
116+ value: category ._id ,
117+ group: ' categories'
118+ }
119+ }) ?? []
120+ if (! categories .find (category => category .header )) categories .unshift ({ label: ' Catégories' , header: true })
107121 if (! states .find (state => state .header )) states .unshift ({ label: ' États' , header: true })
108122 if (! lifeSteps .find (lifestep => lifestep .header )) lifeSteps .unshift ({ label: ' Étapes de vie' , header: true })
109123 return [
@@ -114,6 +128,7 @@ const options = computed(() => {
114128 ]
115129})
116130
131+ // Regroup the filters by key
117132const regroupFilters = async () => {
118133 return filters .value .reduce ((acc : any , filter : Option ) => {
119134 const key = ` filters[@${filter .group }] `
@@ -126,22 +141,32 @@ const regroupFilters = async () => {
126141}
127142
128143const pushQueries = async () => {
144+ // Regroup the filters by key
129145 const regroupedFilters = await regroupFilters ();
146+
147+ // Push the filters to the url
130148 for (const key in regroupedFilters ) {
131149 const values = regroupedFilters [key ];
132150 for (const value of values ) {
133- pushQuery ({ value , key , multiple: true })
151+ pushQuery ({ value , key , multiple: true , pagination: { limit: 10 , skip: 0 } })
134152 }
135153 }
136154};
137155
138- const addFilter = (option : { index: number , value: Option }) => {
139- const index = filters .value .findIndex (filter => filter .value ?.toString () === option .value .toString () && filter .group === option .value .group )
156+ const addFilter = (option : Option ) => {
157+ // Find the index of the option in the filters array
158+ const index = filters .value .findIndex (filter => {
159+ return filter .group === option .group && filter .value === option .value
160+
161+ })
162+ console .log (' index' , index )
163+ // If the option is not in the filters array, add it else remove it
140164 if (index === - 1 ) {
141- filters .value .push (option . value )
165+ filters .value .push (option )
142166 } else {
143167 filters .value .splice (index , 1 )
144168 }
169+ // Push the new filters to the url
145170 pushQueries ()
146171}
147172
0 commit comments