From 168b5e6019e05712ba3fe9f6dac523e6e60a353b Mon Sep 17 00:00:00 2001 From: Abdul Rauf Date: Mon, 14 Oct 2019 22:51:40 +0500 Subject: [PATCH] refactor: /app/pages/Search/index.js --- packages/app/src/app/pages/Search/index.js | 170 +++++++++++---------- 1 file changed, 86 insertions(+), 84 deletions(-) diff --git a/packages/app/src/app/pages/Search/index.js b/packages/app/src/app/pages/Search/index.js index e0091410194..61a0c1af382 100644 --- a/packages/app/src/app/pages/Search/index.js +++ b/packages/app/src/app/pages/Search/index.js @@ -16,7 +16,7 @@ import { } from 'react-instantsearch/dom'; import { Navigation } from 'app/pages/common/Navigation'; -import { inject, hooksObserver } from 'app/componentConnectors'; +import { useOvermind } from 'app/overmind'; import 'instantsearch.css/themes/reset.css'; @@ -32,89 +32,91 @@ const createURL = state => `?${qs.stringify(state)}`; const searchStateToUrl = (location, searchState) => searchState ? `${location.pathname}${createURL(searchState)}` : ''; -const Search = inject('signals')( - hooksObserver(({ history, location, signals: { searchMounted } }) => { - const [searchState, setSearchState] = useState( - qs.parse(location.search.slice(1)) - ); - const debouncedSetState = useRef(null); - - useEffect(() => { - searchMounted(); - }, [searchMounted]); - - useEffect(() => { - const unlisten = history.listen((loc, action) => { - if (['POP', 'PUSH'].includes(action)) { - setSearchState(qs.parse(loc.search.slice(1))); - } - - return unlisten; - }); - }, [history]); - - const onSearchStateChange = useCallback( - newSearchState => { - clearTimeout(debouncedSetState.current); - - debouncedSetState.current = setTimeout(() => { - history.push( - searchStateToUrl(location, newSearchState), - newSearchState - ); - }, updateAfter); - - setSearchState(newSearchState); - }, - [history, location] - ); - - return ( - - - Search - CodeSandbox - - - - - - - - - - - -
-
- Search - - - - - - -
- - -
-
-
-
-
-
- ); - }) -); +const Search = ({ history, location }) => { + const { + actions: { searchMounted }, + } = useOvermind(); + + const [searchState, setSearchState] = useState( + qs.parse(location.search.slice(1)) + ); + const debouncedSetState = useRef(null); + + useEffect(() => { + searchMounted(); + }, [searchMounted]); + + useEffect(() => { + const unlisten = history.listen((loc, action) => { + if (['POP', 'PUSH'].includes(action)) { + setSearchState(qs.parse(loc.search.slice(1))); + } + + return unlisten; + }); + }, [history]); + + const onSearchStateChange = useCallback( + newSearchState => { + clearTimeout(debouncedSetState.current); + + debouncedSetState.current = setTimeout(() => { + history.push( + searchStateToUrl(location, newSearchState), + newSearchState + ); + }, updateAfter); + + setSearchState(newSearchState); + }, + [history, location] + ); + + return ( + + + Search - CodeSandbox + + + + + + + + + + + +
+
+ Search + + + + + + +
+ + +
+
+
+
+
+
+ ); +}; // eslint-disable-next-line import/no-default-export export default Search;