0e10c3f984
* Version Bump * local search skeleton * Added WaitService middleware * Return matching queries * wait action middleware rename/refactor * bigger debounce time * Fix: Initialize state using Immutable * Local Search without integrations * Local Search refactor: Keep state in closure, recurse * “string” should be treated as the default widget by the inference. Closes #199
68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
import { Map, List } from 'immutable';
|
|
|
|
import {
|
|
SEARCH_ENTRIES_REQUEST,
|
|
SEARCH_ENTRIES_SUCCESS,
|
|
QUERY_REQUEST,
|
|
QUERY_SUCCESS,
|
|
SEARCH_CLEAR,
|
|
} from '../actions/search';
|
|
|
|
let loadedEntries;
|
|
let response;
|
|
let page;
|
|
let searchTerm;
|
|
|
|
const defaultState = Map({ isFetching: false, term: null, page: 0, entryIds: List([]), queryHits: Map({}) });
|
|
|
|
const entries = (state = defaultState, action) => {
|
|
switch (action.type) {
|
|
case SEARCH_CLEAR:
|
|
return defaultState;
|
|
|
|
case SEARCH_ENTRIES_REQUEST:
|
|
if (action.payload.searchTerm !== state.get('term')) {
|
|
return state.withMutations((map) => {
|
|
map.set('isFetching', true);
|
|
map.set('term', action.payload.searchTerm);
|
|
});
|
|
}
|
|
return state;
|
|
|
|
case SEARCH_ENTRIES_SUCCESS:
|
|
loadedEntries = action.payload.entries;
|
|
page = action.payload.page;
|
|
searchTerm = action.payload.searchTerm;
|
|
return state.withMutations((map) => {
|
|
const entryIds = List(loadedEntries.map(entry => ({ collection: entry.collection, slug: entry.slug })));
|
|
map.set('isFetching', false);
|
|
map.set('page', page);
|
|
map.set('term', searchTerm);
|
|
map.set('entryIds', page === 0 ? entryIds : map.get('entryIds', List()).concat(entryIds));
|
|
});
|
|
|
|
case QUERY_REQUEST:
|
|
if (action.payload.searchTerm !== state.get('term')) {
|
|
return state.withMutations((map) => {
|
|
map.set('isFetching', action.payload.namespace);
|
|
map.set('term', action.payload.searchTerm);
|
|
});
|
|
}
|
|
return state;
|
|
|
|
case QUERY_SUCCESS:
|
|
searchTerm = action.payload.searchTerm;
|
|
response = action.payload.response;
|
|
return state.withMutations((map) => {
|
|
map.set('isFetching', false);
|
|
map.set('term', searchTerm);
|
|
map.set('queryHits', Map({ [action.payload.namespace]: response.hits }));
|
|
});
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export default entries;
|