-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix hideFilter called repeatedly only registers the last call #6226
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { useCallback, useMemo, useEffect, useState } from 'react'; | ||
import { useCallback, useMemo, useEffect, useState, useRef } from 'react'; | ||
import { useSelector, useDispatch, shallowEqual } from 'react-redux'; | ||
import { parse, stringify } from 'query-string'; | ||
import lodashDebounce from 'lodash/debounce'; | ||
import set from 'lodash/set'; | ||
import pickBy from 'lodash/pickBy'; | ||
import { useHistory, useLocation } from 'react-router-dom'; | ||
|
||
import queryReducer, { | ||
SET_FILTER, | ||
HIDE_FILTER, | ||
SHOW_FILTER, | ||
SET_PAGE, | ||
SET_PER_PAGE, | ||
SET_SORT, | ||
|
@@ -16,7 +17,6 @@ import queryReducer, { | |
import { changeListParams, ListParams } from '../actions/listActions'; | ||
import { SortPayload, ReduxState, FilterPayload } from '../types'; | ||
import removeEmpty from '../util/removeEmpty'; | ||
import removeKey from '../util/removeKey'; | ||
|
||
interface ListParamsOptions { | ||
resource: string; | ||
|
@@ -125,6 +125,7 @@ const useListParams = ({ | |
: defaultParams, | ||
shallowEqual | ||
); | ||
const tempParams = useRef<ListParams>(); | ||
|
||
const requestSignature = [ | ||
location.search, | ||
|
@@ -163,21 +164,31 @@ const useListParams = ({ | |
}, []); // eslint-disable-line | ||
|
||
const changeParams = useCallback(action => { | ||
const newParams = queryReducer(query, action); | ||
if (syncWithLocation) { | ||
history.push({ | ||
search: `?${stringify({ | ||
...newParams, | ||
filter: JSON.stringify(newParams.filter), | ||
displayedFilters: JSON.stringify( | ||
newParams.displayedFilters | ||
), | ||
})}`, | ||
state: { _scrollToTop: action.type === SET_PAGE }, | ||
}); | ||
dispatch(changeListParams(resource, newParams)); | ||
if (!tempParams.current) { | ||
// no other changeParams action dispatched this tick | ||
tempParams.current = queryReducer(query, action); | ||
// schedule side effects for next tick | ||
setTimeout(() => { | ||
if (syncWithLocation) { | ||
history.push({ | ||
search: `?${stringify({ | ||
...tempParams.current, | ||
filter: JSON.stringify(tempParams.current.filter), | ||
displayedFilters: JSON.stringify( | ||
tempParams.current.displayedFilters | ||
), | ||
})}`, | ||
state: { _scrollToTop: action.type === SET_PAGE }, | ||
}); | ||
dispatch(changeListParams(resource, tempParams.current)); | ||
} else { | ||
setLocalParams(tempParams.current); | ||
} | ||
tempParams.current = undefined; | ||
}, 0); | ||
} else { | ||
setLocalParams(newParams); | ||
// side effects already scheduled, just change the params | ||
tempParams.current = queryReducer(tempParams.current, action); | ||
} | ||
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
|
@@ -229,38 +240,18 @@ const useListParams = ({ | |
); | ||
|
||
const hideFilter = useCallback((filterName: string) => { | ||
// we don't use lodash.set() for displayed filters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the logic to the queryReducer for easier testability, but that has no effect on the bug. |
||
// to avoid problems with compound filter names (e.g. 'author.name') | ||
const displayedFilters = Object.keys(displayedFilterValues).reduce( | ||
(filters, filter) => { | ||
return filter !== filterName | ||
? { ...filters, [filter]: true } | ||
: filters; | ||
}, | ||
{} | ||
); | ||
const filter = removeEmpty(removeKey(filterValues, filterName)); | ||
changeParams({ | ||
type: SET_FILTER, | ||
payload: { filter, displayedFilters }, | ||
type: HIDE_FILTER, | ||
payload: filterName, | ||
}); | ||
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const showFilter = useCallback((filterName: string, defaultValue: any) => { | ||
// we don't use lodash.set() for displayed filters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, this change is just cosmetic |
||
// to avoid problems with compound filter names (e.g. 'author.name') | ||
const displayedFilters = { | ||
...displayedFilterValues, | ||
[filterName]: true, | ||
}; | ||
const filter = defaultValue | ||
? set(filterValues, filterName, defaultValue) | ||
: filterValues; | ||
changeParams({ | ||
type: SET_FILTER, | ||
type: SHOW_FILTER, | ||
payload: { | ||
filter, | ||
displayedFilters, | ||
filterName, | ||
defaultValue, | ||
}, | ||
}); | ||
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the fix: since changeParams can be called several times in the same tick, we keep the result if the reducer locally so that we can start from it if another call comes immediately after. And we change the location (or the local state) only at the next tick, when all the sync changes were applied.