@@ -9,25 +9,24 @@ import {
99 deserializeSortByParams
1010} from "./queryParamsUtils" ;
1111
12- function useQueryParamsTable ( { initialParams, columnNames, } ) {
12+ /**
13+ This function automatically defines the params to be used in the request based on the filters set in the table and the URL search parameters.
14+ */
15+ function useQueryParamsTable ( ) {
1316 // react-router
1417 const navigate = useNavigate ( ) ;
1518 const location = useLocation ( ) ;
1619
17- // state
18- /*
19- initialParams is used for
20- fallback values incase URL query params are empty
21- */
20+ // Set the params based on url search params, if no parameter is set in the url it returns an empty dict
2221 const [ params , setParams ] = React . useState ( ( ) => {
2322 const urlParams = Object . fromEntries (
2423 new URLSearchParams ( location . search )
2524 ) ;
26- return Object . keys ( urlParams ) . length ? urlParams : initialParams || { } ;
25+ return Object . keys ( urlParams ) . length ? urlParams : { } ;
2726 } ) ;
2827
29- // memo
30- const initialState = React . useMemo ( ( ) => {
28+ // The table state changes every time the url params are modified, therefore every time some filters are modified
29+ const tableInitialState = React . useMemo ( ( ) => {
3130 const { ordering, page, ...filters } = params ;
3231 return {
3332 pageIndex : page ? parseInt ( page - 1 , 10 ) : 0 ,
@@ -36,7 +35,7 @@ function useQueryParamsTable({ initialParams, columnNames, }) {
3635 } ;
3736 } , [ params ] ) ;
3837
39- // update query params to match table state
38+ // Update query params to match table state and navigate to a page with the new params
4039 React . useEffect ( ( ) => {
4140 const search = `?${ new URLSearchParams ( params ) . toString ( ) } ` ;
4241 if ( search !== location . search )
@@ -45,21 +44,11 @@ function useQueryParamsTable({ initialParams, columnNames, }) {
4544
4645 // callbacks
4746 const onTableFilterDebounced = useAsyncDebounce (
48- /* this function MUST updates ONLY the parameters used by the table: the columns.
49- It has to maintain all the params used in the page outside the table.
50- */
51- ( filters ) =>
52-
53- setParams ( ( { ordering, ...others } ) => ( { // we need to separate ordering, or this doesn't work
54- // Maintain the parameter
55- ...Object . keys ( others )
56- . filter ( param => ! ! columnNames . includes ( param . id ) ) // select only parameter external to the table
57- . reduce ( ( savedParams , nextParam ) => ( { ...savedParams , [ nextParam [ 0 ] ] : nextParam [ 1 ] , } ) , { } ) , // concatenate them
58- // Add ordering parameter (if it's defined)
59- ...( ordering ? { ordering, } : { } ) ,
60- // add the filter parameters (the ones with columns names)
61- ...serializeFilterParams ( filters . filter ( filter => columnNames . includes ( filter . id ) ) ) ,
62- } ) ) ,
47+ ( filters ) =>
48+ setParams ( ( { ordering, } ) => ( {
49+ ...( ordering ? { ordering, } : { } ) , // only include 'ordering' key if it defined
50+ ...serializeFilterParams ( filters ) , // only serialize 'filters' field
51+ } ) ) ,
6352 500
6453 ) ; // Debounce filter call for 500ms
6554
@@ -68,9 +57,9 @@ function useQueryParamsTable({ initialParams, columnNames, }) {
6857 sortBy ?. length
6958 ? setParams ( ( { ordering, ...others } ) => ( {
7059 ...others ,
71- ordering : serializeSortByParams ( sortBy ) ,
60+ ordering : serializeSortByParams ( sortBy ) , // override only 'ordering' key
7261 } ) )
73- : setParams ( ( { ordering, ...others } ) => others ) ,
62+ : setParams ( ( { ordering, ...others } ) => others ) , // if sortBy has been reset remove the 'orgering' key
7463 500
7564 ) ; // Debounce sortBy call for 500ms
7665
@@ -97,7 +86,7 @@ function useQueryParamsTable({ initialParams, columnNames, }) {
9786 [ setParams , onTableFilterDebounced , onTableSortDebounced ]
9887 ) ;
9988
100- return [ params , initialState , tableStateReducer ] ;
89+ return [ params , tableInitialState , tableStateReducer ] ;
10190}
10291
10392export default useQueryParamsTable ;
0 commit comments