Merge branch 'search-race-condition' into 'develop'

Fix: Race condition in search

See merge request soapbox-pub/soapbox-fe!908
This commit is contained in:
Alex Gleason 2021-12-12 20:30:30 +00:00
commit b706024e31
2 changed files with 26 additions and 21 deletions

View file

@ -63,7 +63,7 @@ export function submitSearch(filter) {
dispatch(importFetchedStatuses(response.data.statuses)); dispatch(importFetchedStatuses(response.data.statuses));
} }
dispatch(fetchSearchSuccess(response.data)); dispatch(fetchSearchSuccess(response.data, value));
dispatch(fetchRelationships(response.data.accounts.map(item => item.id))); dispatch(fetchRelationships(response.data.accounts.map(item => item.id)));
}).catch(error => { }).catch(error => {
dispatch(fetchSearchFail(error)); dispatch(fetchSearchFail(error));
@ -78,10 +78,11 @@ export function fetchSearchRequest(value) {
}; };
} }
export function fetchSearchSuccess(results) { export function fetchSearchSuccess(results, searchTerm) {
return { return {
type: SEARCH_FETCH_SUCCESS, type: SEARCH_FETCH_SUCCESS,
results, results,
searchTerm,
}; };
} }

View file

@ -28,29 +28,33 @@ const toIds = items => {
return ImmutableOrderedSet(items.map(item => item.id)); return ImmutableOrderedSet(items.map(item => item.id));
}; };
const importResults = (state, results) => { const importResults = (state, results, searchTerm) => {
return state.withMutations(state => { return state.withMutations(state => {
state.set('results', ImmutableMap({ if (state.get('value') === searchTerm) {
accounts: toIds(results.accounts), state.set('results', ImmutableMap({
statuses: toIds(results.statuses), accounts: toIds(results.accounts),
hashtags: fromJS(results.hashtags), // it's a list of maps statuses: toIds(results.statuses),
accountsHasMore: results.accounts.length >= 20, hashtags: fromJS(results.hashtags), // it's a list of maps
statusesHasMore: results.statuses.length >= 20, accountsHasMore: results.accounts.length >= 20,
hashtagsHasMore: results.hashtags.length >= 20, statusesHasMore: results.statuses.length >= 20,
accountsLoaded: true, hashtagsHasMore: results.hashtags.length >= 20,
statusesLoaded: true, accountsLoaded: true,
hashtagsLoaded: true, statusesLoaded: true,
})); hashtagsLoaded: true,
}));
state.set('submitted', true); state.set('submitted', true);
}
}); });
}; };
const paginateResults = (state, searchType, results) => { const paginateResults = (state, searchType, results, searchTerm) => {
return state.withMutations(state => { return state.withMutations(state => {
state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20); if (state.get('value') === searchTerm) {
state.setIn(['results', `${searchType}Loaded`], true); state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20);
state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id))); state.setIn(['results', `${searchType}Loaded`], true);
state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id)));
}
}); });
}; };
@ -77,13 +81,13 @@ export default function search(state = initialState, action) {
case SEARCH_FETCH_REQUEST: case SEARCH_FETCH_REQUEST:
return handleSubmitted(state, action.value); return handleSubmitted(state, action.value);
case SEARCH_FETCH_SUCCESS: case SEARCH_FETCH_SUCCESS:
return importResults(state, action.results); return importResults(state, action.results, action.searchTerm);
case SEARCH_FILTER_SET: case SEARCH_FILTER_SET:
return state.set('filter', action.value); return state.set('filter', action.value);
case SEARCH_EXPAND_REQUEST: case SEARCH_EXPAND_REQUEST:
return state.setIn(['results', `${action.searchType}Loaded`], false); return state.setIn(['results', `${action.searchType}Loaded`], false);
case SEARCH_EXPAND_SUCCESS: case SEARCH_EXPAND_SUCCESS:
return paginateResults(state, action.searchType, action.results); return paginateResults(state, action.searchType, action.results, action.searchTerm);
default: default:
return state; return state;
} }