diff --git a/app/soapbox/actions/search.js b/app/soapbox/actions/search.js index 6f1621ad4..ca515d06c 100644 --- a/app/soapbox/actions/search.js +++ b/app/soapbox/actions/search.js @@ -17,9 +17,16 @@ export const SEARCH_EXPAND_SUCCESS = 'SEARCH_EXPAND_SUCCESS'; export const SEARCH_EXPAND_FAIL = 'SEARCH_EXPAND_FAIL'; export function changeSearch(value) { - return { - type: SEARCH_CHANGE, - value, + return (dispatch, getState) => { + // If backspaced all the way, clear the search + if (value.length === 0) { + return dispatch(clearSearch()); + } else { + return dispatch({ + type: SEARCH_CHANGE, + value, + }); + } }; } @@ -29,10 +36,11 @@ export function clearSearch() { }; } -export function submitSearch() { +export function submitSearch(filter) { return (dispatch, getState) => { const value = getState().getIn(['search', 'value']); + // An empty search doesn't return any results if (value.length === 0) { return; } @@ -44,6 +52,7 @@ export function submitSearch() { q: value, resolve: true, limit: 20, + type: filter || getState().getIn(['search', 'filter'], 'accounts'), }, }).then(response => { if (response.data.accounts) { @@ -83,13 +92,17 @@ export function fetchSearchFail(error) { }; } -export const setFilter = filterType => dispatch => { - dispatch({ - type: SEARCH_FILTER_SET, - path: ['search', 'filter'], - value: filterType, - }); -}; +export function setFilter(filterType) { + return (dispatch) => { + dispatch(submitSearch(filterType)); + + dispatch({ + type: SEARCH_FILTER_SET, + path: ['search', 'filter'], + value: filterType, + }); + }; +} export const expandSearch = type => (dispatch, getState) => { const value = getState().getIn(['search', 'value']); diff --git a/app/soapbox/reducers/search.js b/app/soapbox/reducers/search.js index 47aa9f00b..8a4ec4c5b 100644 --- a/app/soapbox/reducers/search.js +++ b/app/soapbox/reducers/search.js @@ -28,21 +28,7 @@ const toIds = items => { return ImmutableOrderedSet(items.map(item => item.id)); }; -const getResultsFilter = results => { - if (results.accounts.length > 0) { - return 'accounts'; - } else if (results.statuses.length > 0) { - return 'statuses'; - } else if (results.hashtags.length > 0) { - return 'hashtags'; - } else { - return 'accounts'; - } -}; - const importResults = (state, results) => { - const filter = getResultsFilter(results); - return state.withMutations(state => { state.set('results', ImmutableMap({ accounts: toIds(results.accounts), @@ -57,7 +43,6 @@ const importResults = (state, results) => { })); state.set('submitted', true); - state.set('filter', filter); }); };