Skip to content

Commit

Permalink
Moved routes search to local component state
Browse files Browse the repository at this point in the history
  • Loading branch information
lerastromtsova committed May 16, 2020
1 parent 178fbe2 commit 4184e2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 54 deletions.
46 changes: 20 additions & 26 deletions src/features/routes/RoutesSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 (
<Layout style={styles.flexArea} level="3">
Expand All @@ -34,9 +47,7 @@ const RoutesSearch = props => {
<Layout style={styles.headerLayout} level="3">
<SearchBar
style={styles.searchBar}
onChangeText={text =>
text !== '' ? props.fetchRoutes(text) : () => null
}
onChangeText={text => fetchSearchResults(text)}
onBlur={() => {
setSearchBarOpen(false);
}}
Expand Down Expand Up @@ -67,21 +78,21 @@ const RoutesSearch = props => {
</Layout>
)}
<Layout style={styles.roundedLayout} level="1">
{props.isFetching ? (
{isFetching ? (
<Layout style={shared.centerContent}>
<Spinner />
</Layout>
) : (
<ScrollView contentContainerStyle={styles.scrollPadded}>
{props.routes.length === 0 ? (
{searchResults.length === 0 ? (
<Layout style={shared.centerContent}>
<Text appearance="hint">
{str('routes.noRoutes')}
</Text>
</Layout>
) : (
<RoutesList
data={props.routes}
data={searchResults}
navigation={props.navigation}
/>
)}
Expand All @@ -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;
2 changes: 0 additions & 2 deletions src/reducers/rootReducer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,7 +11,6 @@ const rootReducer = combineReducers({
auth,
currentLocation,
userInfo,
routesSearch,
data,
});

Expand Down
26 changes: 0 additions & 26 deletions src/reducers/routes.js

This file was deleted.

0 comments on commit 4184e2f

Please sign in to comment.