// @flow import React, { Component } from 'react'; import { View, TextInput, TouchableOpacity, SafeAreaView } from 'react-native'; import { Toolbar, Icon } from 'react-native-material-ui'; import { path, cond, test } from 'ramda'; import queryString from 'query-string'; import { getSearch, getViewMode } from '../helpers/RouteHelpers'; import ProductSaveBtn from '../components/ProductSaveBtn'; import { withFilter } from '../enhancers/filterEnhancers'; import FilterRecord from '../records/FilterRecord'; import { palette } from '../ui-theme'; import { withRouterContext } from '../enhancers/routeEnhancers'; import FAIcon from 'react-native-vector-icons/FontAwesome'; type Props = { title: ?string, filter: FilterRecord, setFilter: (filter: FilterRecord) => void, onFilterPress: () => void, router: Object, }; class TopToolbar extends Component { static defaultProps = { title: "Where's the TP?", }; props: Props; onBackPress = () => { const { router } = this.props; const { history } = router; if (history.length > 1) { history.goBack(); } else { history.replace('/list/food'); } }; toggleMapList = () => { const history = path(['router', 'history'], this.props); const search = getSearch(this.props); const viewMode = getViewMode(this.props); history.replace({ search: queryString.stringify({ ...search, viewMode: viewMode === 'list' ? 'map' : 'list', }), }); }; onSearchCleared = () => { const { setFilter, filter } = this.props; setFilter(filter.set('search', '')); }; onChangeText = search => { const { setFilter, filter } = this.props; setFilter(filter.set('search', search)); }; getRightElement = () => { const route = path(['router', 'route', 'location', 'pathname'], this.props); if (/^\/createProduct/.test(route)) { return ; } }; render() { const { filter, onFilterPress } = this.props; const route = path(['router', 'route', 'location', 'pathname'], this.props); const routeSearch = getSearch(this.props); const title = routeSearch.routeTitle || this.props.title; const viewMode = getViewMode(this.props); const showSearch = /^\/list/.test(route); const showSearchClear = filter.search.length > 0; const searchPlaceholder = cond([ [test(/\/food/), () => 'Search products...'], [test(/\/places/), () => 'Search places...'], [test(/\/faves/), () => 'Search favorites'], ])(route); return ( {!showSearch && ( )} {showSearch && ( {showSearchClear ? ( ) : ( )} )} ); } } export default withFilter(withRouterContext(TopToolbar));