Commit 193f6f7f authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Handle focus-user filter conflict when setting filters

parent f7a74a2a
......@@ -170,10 +170,21 @@ function changeFocusModeUser(user) {
* @param {FilterOption} filterOption
*/
function setFilter(filterName, filterOption) {
return {
type: actions.SET_FILTER,
filterName,
filterOption,
return (dispatch, getState) => {
// If there is a filter conflict with focusFilters, deactivate focus
// mode to prevent unintended collisions and let the new filter value
// take precedence.
if (getState().filters.focusFilters?.[filterName]) {
dispatch({
type: actions.SET_FOCUS_MODE,
active: false,
});
}
dispatch({
type: actions.SET_FILTER,
filterName,
filterOption,
});
};
}
......@@ -263,6 +274,10 @@ const getFilterValues = createSelector(
}
);
function getFocusFilters(state) {
return state.focusFilters;
}
/**
* Are there currently any active (applied) filters?
*/
......@@ -286,6 +301,7 @@ export default storeModule({
getFilter,
getFilters,
getFilterValues,
getFocusFilters,
hasAppliedFilter,
},
});
......@@ -74,6 +74,26 @@ describe('sidebar/store/modules/filters', () => {
assert.isUndefined(filters.whatever);
});
it('disables focus mode if there is a conflicting filter key', () => {
store = createStore(
[filters],
[{ focus: { user: { username: 'somebody' } } }]
);
assert.isTrue(store.focusState().active);
// No conflict in focusFilters on `elephant`
store.setFilter('elephant', {
value: 'pink',
display: 'Pink Elephant',
});
assert.isTrue(store.focusState().active);
store.setFilter('user', { value: '', display: 'Everybody' });
assert.isFalse(store.focusState().active);
});
it('replaces pre-existing filter with the same key', () => {
store.setFilter('whatever', {
value: 'anyOldThing',
......@@ -251,6 +271,27 @@ describe('sidebar/store/modules/filters', () => {
});
});
describe('getFocusFilters', () => {
it('returns any set focus filters', () => {
store = createStore(
[filters],
[
{
focus: {
user: { username: 'somebody', displayName: 'Ding Bat' },
},
},
]
);
const focusFilters = store.getFocusFilters();
assert.exists(focusFilters.user);
assert.deepEqual(focusFilters.user, {
value: 'somebody',
display: 'Ding Bat',
});
});
});
describe('hasAppliedFilter', () => {
it('returns true if there is a search query set', () => {
store.setFilterQuery('foobar');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment