diff --git a/src/features/routes/RoutesSearch.js b/src/features/routes/RoutesSearch.js index 0e7b8e9..a53da09 100644 --- a/src/features/routes/RoutesSearch.js +++ b/src/features/routes/RoutesSearch.js @@ -9,11 +9,10 @@ import { } from '@ui-kitten/components'; import RoutesList from '../../components/lists/RoutesList'; import {searchScreenStyles, sharedStyles} from '../../styles/styleProvider'; -import {fetchSearchRoutes} from '../../actions/routes'; -import {connect} from 'react-redux'; import {SearchBar} from '../../components/inputs/SearchBar'; import {str} from '../../i18n'; import {ControlButton} from '../../components/buttons/ControlButton'; +import {getSearchRoutes} from '../../api/routes'; const RoutesSearch = props => { const styles = useStyleSheet(searchScreenStyles); @@ -22,6 +21,20 @@ const RoutesSearch = props => { const [searchBarOpen, setSearchBarOpen] = useState( props.route.params.searchBarOpen, ); + const [searchResults, setSearchResults] = useState([]); + const [isFetching, setIsFetching] = useState(false); + + function fetchSearchResults(query) { + if (query !== '') { + setIsFetching(true); + getSearchRoutes(query).then(result => { + setSearchResults(result.routes); + setIsFetching(false); + }); + } else { + setSearchResults([]); + } + } return ( @@ -34,9 +47,7 @@ const RoutesSearch = props => { - text !== '' ? props.fetchRoutes(text) : () => null - } + onChangeText={text => fetchSearchResults(text)} onBlur={() => { setSearchBarOpen(false); }} @@ -67,13 +78,13 @@ const RoutesSearch = props => { )} - {props.isFetching ? ( + {isFetching ? ( ) : ( - {props.routes.length === 0 ? ( + {searchResults.length === 0 ? ( {str('routes.noRoutes')} @@ -81,7 +92,7 @@ const RoutesSearch = props => { ) : ( )} @@ -92,21 +103,4 @@ const RoutesSearch = props => { ); }; -const mapDispatchToProps = dispatch => { - return { - fetchRoutes: query => dispatch(fetchSearchRoutes(query)), - }; -}; - -const mapStateToProps = state => { - return { - isFetching: state.routesSearch.isFetching, - routesInvalid: state.routesSearch.didInvalidate, - routes: state.routesSearch.items, - }; -}; - -export default connect( - mapStateToProps, - mapDispatchToProps, -)(RoutesSearch); +export default RoutesSearch; diff --git a/src/reducers/rootReducer.js b/src/reducers/rootReducer.js index f12f825..38d2976 100644 --- a/src/reducers/rootReducer.js +++ b/src/reducers/rootReducer.js @@ -1,6 +1,5 @@ import {combineReducers} from 'redux'; import {auth} from './auth'; -import {routesSearch} from './routes'; import {currentLocation} from './currentLocation'; import {userInfo} from './profile'; import {createReducer} from 'redux-orm'; @@ -12,7 +11,6 @@ const rootReducer = combineReducers({ auth, currentLocation, userInfo, - routesSearch, data, }); diff --git a/src/reducers/routes.js b/src/reducers/routes.js deleted file mode 100644 index 312c674..0000000 --- a/src/reducers/routes.js +++ /dev/null @@ -1,26 +0,0 @@ -import {RECEIVE_SEARCH_ROUTES, REQUEST_SEARCH_ROUTES} from '../actions/routes'; - -export function routesSearch( - state = { - isFetching: false, - didInvalidate: true, - items: [], - }, - action, -) { - switch (action.type) { - case REQUEST_SEARCH_ROUTES: - return Object.assign({}, state, { - isFetching: true, - }); - case RECEIVE_SEARCH_ROUTES: - return Object.assign({}, state, { - didInvalidate: false, - isFetching: false, - items: action.routes, - }); - - default: - return state; - } -}