aretherecookies-mobile/js/components/TopToolbar.js

165 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2017-04-01 20:53:37 -05:00
// @flow
import React, { Component } from 'react';
2019-05-12 10:04:12 -05:00
import { View, TextInput, TouchableOpacity, SafeAreaView } from 'react-native';
2018-07-21 11:54:04 -05:00
import { Toolbar, Icon } from 'react-native-material-ui';
2020-03-29 19:47:33 +00:00
import { path, cond, test } from 'ramda';
import queryString from 'query-string';
2019-06-29 17:04:22 +00:00
import { getSearch, getViewMode } from '../helpers/RouteHelpers';
2020-04-17 22:50:23 -05:00
import ProductSaveBtn from '../components/ProductSaveBtn';
import { withFilter } from '../enhancers/filterEnhancers';
import FilterRecord from '../records/FilterRecord';
2018-07-21 11:54:04 -05:00
import { palette } from '../ui-theme';
import { withRouterContext } from '../enhancers/routeEnhancers';
2019-06-22 15:48:52 +00:00
import FAIcon from 'react-native-vector-icons/FontAwesome';
2017-04-01 20:53:37 -05:00
type Props = {
2017-08-06 12:57:23 -05:00
title: ?string,
filter: FilterRecord,
setFilter: (filter: FilterRecord) => void,
2019-06-22 15:48:52 +00:00
onFilterPress: () => void,
router: Object,
2017-04-01 20:53:37 -05:00
};
class TopToolbar extends Component {
2017-08-06 12:57:23 -05:00
static defaultProps = {
2020-04-03 21:21:00 -05:00
title: "Where's the TP?",
2017-08-06 12:57:23 -05:00
};
props: Props;
onBackPress = () => {
const { router } = this.props;
const { history } = router;
2020-03-29 19:47:33 +00:00
if (history.length > 1) {
history.goBack();
} else {
history.replace('/list/food');
}
2019-06-22 15:48:52 +00:00
};
2019-06-29 17:04:22 +00:00
toggleMapList = () => {
const history = path(['router', 'history'], this.props);
const search = getSearch(this.props);
2019-06-29 17:04:22 +00:00
const viewMode = getViewMode(this.props);
2017-08-06 12:57:23 -05:00
history.replace({
search: queryString.stringify({
...search,
2019-06-29 17:04:22 +00:00
viewMode: viewMode === 'list' ? 'map' : 'list',
}),
2017-08-06 12:57:23 -05:00
});
};
2018-07-21 11:54:04 -05:00
onSearchCleared = () => {
const { setFilter, filter } = this.props;
2020-04-03 22:21:26 -05:00
setFilter(filter.set('search', ''));
};
2017-08-06 12:57:23 -05:00
2020-04-17 22:50:23 -05:00
onChangeText = search => {
2018-07-01 11:54:16 -05:00
const { setFilter, filter } = this.props;
2020-04-03 22:21:26 -05:00
setFilter(filter.set('search', search));
2018-07-21 11:54:04 -05:00
};
2017-08-06 12:57:23 -05:00
2017-09-10 19:20:26 -05:00
getRightElement = () => {
const route = path(['router', 'route', 'location', 'pathname'], this.props);
2017-09-10 19:20:26 -05:00
2020-04-17 22:50:23 -05:00
if (/^\/createProduct/.test(route)) {
return <ProductSaveBtn />;
2017-09-10 19:20:26 -05:00
}
};
2017-08-06 12:57:23 -05:00
render() {
2019-06-22 15:48:52 +00:00
const { filter, onFilterPress } = this.props;
const route = path(['router', 'route', 'location', 'pathname'], this.props);
const routeSearch = getSearch(this.props);
2018-07-21 11:54:04 -05:00
const title = routeSearch.routeTitle || this.props.title;
2019-06-29 17:04:22 +00:00
const viewMode = getViewMode(this.props);
2017-08-06 12:57:23 -05:00
2018-07-21 11:54:04 -05:00
const showSearch = /^\/list/.test(route);
const showSearchClear = filter.search.length > 0;
2020-03-29 19:47:33 +00:00
const searchPlaceholder = cond([
[test(/\/food/), () => 'Search products...'],
[test(/\/places/), () => 'Search places...'],
2020-03-29 19:47:33 +00:00
[test(/\/faves/), () => 'Search favorites'],
])(route);
2018-07-21 11:54:04 -05:00
2017-08-06 12:57:23 -05:00
return (
2020-03-29 19:47:33 +00:00
<View style={{ backgroundColor: palette.primaryColor }}>
2019-05-12 10:04:12 -05:00
<SafeAreaView>
2019-06-22 15:48:52 +00:00
{!showSearch && (
<Toolbar
leftElement="arrow-back"
onLeftElementPress={this.onBackPress}
centerElement={title}
rightElement={this.getRightElement()}
2018-07-21 11:54:04 -05:00
/>
2019-06-22 15:48:52 +00:00
)}
{showSearch && (
2020-03-29 19:47:33 +00:00
<View
style={{
flexDirection: 'row',
padding: 8,
2020-03-29 19:47:33 +00:00
}}>
2019-06-29 17:04:22 +00:00
<View
2019-06-22 15:48:52 +00:00
style={{
flex: 1,
2019-06-29 17:04:22 +00:00
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white',
borderRadius: 10,
}}>
<Icon name="search" style={{ marginHorizontal: 8 }} />
2019-06-29 17:04:22 +00:00
<TextInput
value={filter.search}
onChangeText={this.onChangeText}
placeholder={searchPlaceholder}
underlineColorAndroid="transparent"
2019-06-22 15:48:52 +00:00
style={{
2019-06-29 17:04:22 +00:00
flex: 1,
fontSize: 16,
padding: 4,
height: 40,
2019-06-29 17:04:22 +00:00
}}
/>
{showSearchClear ? (
<TouchableOpacity onPress={this.onSearchCleared}>
<Icon name="close" style={{ margin: 8 }} />
2019-06-29 17:04:22 +00:00
</TouchableOpacity>
) : (
<TouchableOpacity
onPress={onFilterPress}
style={{
2020-04-04 19:47:53 -05:00
backgroundColor: '#D4EAEF',
2019-06-29 17:04:22 +00:00
borderRadius: 3,
marginHorizontal: 10,
paddingVertical: 5,
paddingHorizontal: 8,
}}>
<FAIcon name="filter" style={{ fontSize: 24, color: palette.primaryColor }} />
2019-06-29 17:04:22 +00:00
</TouchableOpacity>
)}
</View>
<TouchableOpacity
onPress={this.toggleMapList}
style={{
padding: 8,
borderRadius: 10,
backgroundColor: 'rgba(255,255,255,0.2)',
marginLeft: 10,
alignItems: 'center',
justifyContent: 'center',
}}>
<Icon name={viewMode === 'map' ? 'view-list' : 'map'} size={24} color="white" />
2019-06-29 17:04:22 +00:00
</TouchableOpacity>
2019-06-22 15:48:52 +00:00
</View>
)}
2019-05-12 10:04:12 -05:00
</SafeAreaView>
2018-07-21 11:54:04 -05:00
</View>
2017-08-06 12:57:23 -05:00
);
}
}
2017-04-01 20:53:37 -05:00
export default withFilter(withRouterContext(TopToolbar));