Load initial search

This commit is contained in:
Jordan Eldredge 2018-11-29 12:57:03 -08:00
parent 122b2bbc8b
commit cbc6989563
2 changed files with 24 additions and 20 deletions

View file

@ -1,8 +1,26 @@
import { combineEpics } from "redux-observable";
import { of, from } from "rxjs";
import { of, from, empty } from "rxjs";
import { filter, switchMap, map } from "rxjs/operators";
import { search } from "../algolia";
const urlChangedEpic = actions =>
actions.pipe(
filter(action => action.type === "URL_CHANGED"),
switchMap(action => {
const params = new URLSearchParams(action.location.search);
const query = params != null && params.get("query");
if (action.location.pathname.startsWith("/skin/")) {
const segments = action.location.pathname.split("/");
return { type: "SELECTED_SKIN", hash: segments[2] };
}
if (query == null) {
return empty();
}
return of({ type: "SEARCH_QUERY_CHANGED", query });
})
);
const searchEpic = actions =>
actions.pipe(
filter(action => action.type === "SEARCH_QUERY_CHANGED"),
@ -23,4 +41,4 @@ const searchEpic = actions =>
})
);
export default combineEpics(searchEpic);
export default combineEpics(searchEpic, urlChangedEpic);

View file

@ -24,20 +24,6 @@ function reducer(state = defaultState, action) {
selectedSkinHash: null,
selectedSkinPosition: null
};
case "URL_CHANGED":
const params = new URLSearchParams(action.location.search);
const searchQuery = params != null && params.get("query");
if (action.location.pathname.startsWith("/skin/")) {
const segments = action.location.pathname.split("/");
return {
...state,
selectedSkinHash: segments[2],
// TODO: This makes no sense
selectedSkinPosition: null,
searchQuery
};
}
return { ...defaultState, searchQuery };
case "SEARCH_QUERY_CHANGED":
return {
...state,
@ -58,6 +44,10 @@ export function createStore() {
const store = createReduxStore(reducer, applyMiddleware(epicMiddleware));
epicMiddleware.run(rootEpic);
let lastUrl = null;
window.onpopstate = function() {
store.dispatch({ type: "URL_CHANGED", location: document.location });
};
store.dispatch({ type: "URL_CHANGED", location: document.location });
store.subscribe(() => {
const state = store.getState();
const url = Selectors.getUrl(state);
@ -66,9 +56,5 @@ export function createStore() {
lastUrl = url;
}
});
window.onpopstate = function() {
store.dispatch({ type: "URL_CHANGED", location: document.location });
};
store.dispatch({ type: "URL_CHANGED", location: document.location });
return store;
}