-
Notifications
You must be signed in to change notification settings - Fork 13
Fix/filters disappearing bug #71
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
Changes from all commits
1d5d504
42adcdf
dd2ca8b
b77dc46
35da973
8db7a74
306112e
7d693af
2103936
040d36a
d1f09ef
b3fd0e7
950a6ab
5838ab5
e9374f9
bf4a065
f1c9102
c5dad64
f8a3ef5
cd5c2f2
28b07bd
96802f4
c2f18c6
6ca5f08
8fac895
04a568c
7745902
01cd5fd
cfd6cdd
befca0d
aeb264b
2564b8a
4ec8dd9
fdb6ce3
aedcdff
65dfded
a2e07f7
208abab
d286a02
9fc8e0c
0cffdf1
32c8b36
61c8516
4d875ce
357a136
90aedc1
a672740
7a1522f
4485590
8af8a5a
f6023b4
087a5fa
71989c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,6 @@ export const getFilterSections = ( | |
| options: defaultOptions, | ||
| }; | ||
| }); | ||
|
|
||
| return sections; | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,84 @@ export const mergeFilters = (userFilter, adminAppliedPreFilter) => { | |
|
|
||
| return filterAB; | ||
| }; | ||
|
|
||
| function isFilterOptionToBeHidden(option, filtersApplied, fieldName) { | ||
| if (option.count > 0) { | ||
| return false; | ||
| } | ||
|
|
||
| if (typeof filtersApplied !== 'undefined' | ||
| && filtersApplied[fieldName] | ||
| && filtersApplied[fieldName].selectedValues.includes(option.key)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * This function updates the counts in the initial set of tab options | ||
| * calculated from unfiltered data. | ||
| * It is used to retain field options in the rendering if | ||
| * they are still checked but their counts are zero. | ||
| */ | ||
| export const updateCountsInInitialTabsOptions = ( | ||
| initialTabsOptions, processedTabsOptions, filtersApplied, | ||
| ) => { | ||
| const updatedTabsOptions = JSON.parse(JSON.stringify(initialTabsOptions)); | ||
|
Contributor
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. Why do we need to stringify and then parse this object here? 🤔
Contributor
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. alright it took me time to understand this code block but I guess you were trying to deep clone an object here. :D |
||
| const initialFields = Object.keys(initialTabsOptions); | ||
| for (let i = 0; i < initialFields.length; i += 1) { | ||
| const fieldName = initialFields[i]; | ||
| const initialFieldOptions = initialTabsOptions[fieldName].histogram.map(x => x.key); | ||
| let processedFieldOptions = []; | ||
| if (Object.prototype.hasOwnProperty.call(processedTabsOptions, fieldName)) { | ||
| processedFieldOptions = processedTabsOptions[fieldName].histogram.map(x => x.key); | ||
| } | ||
|
|
||
qingyashu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (let j = 0; j < initialFieldOptions.length; j += 1) { | ||
| const optionName = initialFieldOptions[j]; | ||
| let newCount; | ||
| if (processedFieldOptions.includes(optionName)) { | ||
| newCount = processedTabsOptions[fieldName].histogram.filter( | ||
| x => x.key === optionName, | ||
| )[0].count; | ||
| } else { | ||
| newCount = 0; | ||
| } | ||
| for (let k = 0; k < updatedTabsOptions[fieldName].histogram.length; k += 1) { | ||
| const option = updatedTabsOptions[fieldName].histogram[k]; | ||
| if (option.key === optionName) { | ||
| updatedTabsOptions[fieldName].histogram[k].count = newCount; | ||
| if (isFilterOptionToBeHidden( | ||
| updatedTabsOptions[fieldName].histogram[k], filtersApplied, fieldName, | ||
| )) { | ||
| updatedTabsOptions[fieldName].histogram.splice(k, 1); | ||
|
Contributor
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. Wow I finally understand this 3-level loop 👍 I think this works! except the readability, and if there's a way to use JS's built -in Array filter or map function to reduce the level of nested loops that would be great. But anyway I am fine with keeping this. Could you add a |
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return updatedTabsOptions; | ||
| }; | ||
|
|
||
| function sortCountThenAlpha(a, b) { | ||
| if (a.count === b.count) { | ||
| return a.key < b.key ? -1 : 1; | ||
| } | ||
| return b.count - a.count; | ||
| } | ||
|
|
||
| export const sortTabsOptions = (tabsOptions) => { | ||
| const fields = Object.keys(tabsOptions); | ||
| const sortedTabsOptions = Object.assign({}, tabsOptions); | ||
| for (let x = 0; x < fields.length; x += 1) { | ||
| const field = fields[x]; | ||
|
|
||
| const optionsForThisField = sortedTabsOptions[field].histogram; | ||
| optionsForThisField.sort(sortCountThenAlpha); | ||
| sortedTabsOptions[field].histogram = optionsForThisField; | ||
| } | ||
| return sortedTabsOptions; | ||
| }; | ||
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.
it seems this
this.state.filteris not useful anymore, could we just remove this, and so that not having bothfilterandfiltersApplied, you might also want to remove its only reference incomponentDidMountfunction