feat: improve search to target single collections (#3760)

This commit is contained in:
Hannes Küttner
2020-05-18 09:52:06 +02:00
committed by GitHub
parent 72596bbbec
commit 588622adb2
15 changed files with 569 additions and 82 deletions

View File

@ -44,13 +44,14 @@ export const selectEntries = (state: State, collection: string) =>
export const selectPublishedSlugs = (state: State, collection: string) =>
fromEntries.selectPublishedSlugs(state.entries, collection);
export const selectSearchedEntries = (state: State) => {
export const selectSearchedEntries = (state: State, availableCollections: string[]) => {
const searchItems = state.search.get('entryIds');
// only return search results for actually available collections
return (
searchItems &&
searchItems.map(({ collection, slug }) =>
fromEntries.selectEntry(state.entries, collection, slug),
)
searchItems
.filter(({ collection }) => availableCollections.indexOf(collection) !== -1)
.map(({ collection, slug }) => fromEntries.selectEntry(state.entries, collection, slug))
);
};

View File

@ -12,10 +12,12 @@ let loadedEntries;
let response;
let page;
let searchTerm;
let searchCollections;
const defaultState = Map({
isFetching: false,
term: null,
collections: null,
page: 0,
entryIds: List([]),
queryHits: Map({}),
@ -31,6 +33,7 @@ const entries = (state = defaultState, action) => {
return state.withMutations(map => {
map.set('isFetching', true);
map.set('term', action.payload.searchTerm);
map.set('collections', List(action.payload.searchCollections));
map.set('page', action.payload.page);
});
}
@ -40,6 +43,7 @@ const entries = (state = defaultState, action) => {
loadedEntries = action.payload.entries;
page = action.payload.page;
searchTerm = action.payload.searchTerm;
searchCollections = action.payload.searchCollections;
return state.withMutations(map => {
const entryIds = List(
loadedEntries.map(entry => ({ collection: entry.collection, slug: entry.slug })),
@ -48,6 +52,7 @@ const entries = (state = defaultState, action) => {
map.set('fetchID', null);
map.set('page', page);
map.set('term', searchTerm);
map.set('collections', List(searchCollections));
map.set(
'entryIds',
!page || isNaN(page) || page === 0