From cbc6989563b3aeb48fe6136c610a8d84528a99bd Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Thu, 29 Nov 2018 12:57:03 -0800 Subject: [PATCH] Load initial search --- src/redux/epics.js | 22 ++++++++++++++++++++-- src/redux/store.js | 22 ++++------------------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/redux/epics.js b/src/redux/epics.js index 40b16159..3f0a843e 100644 --- a/src/redux/epics.js +++ b/src/redux/epics.js @@ -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); diff --git a/src/redux/store.js b/src/redux/store.js index d6880a91..69939bec 100644 --- a/src/redux/store.js +++ b/src/redux/store.js @@ -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; }