import React, { useEffect, useState } from 'react' import { StyleSheet, TextInput, View, FlatList, TouchableOpacity, Text, Platform } from 'react-native' import { useSelector } from 'react-redux'; import Elements from '../../../../components/elements.js'; import Theme from '../../../../components/theme.style.js'; import Icon from '../../../../components/icons.js'; import REQUEST from '../../../../components/api'; import DB from '../../../../components/storage'; const Search = (props) => { const app_theme = useSelector(state => state.appThemeReducer.theme); const [cities, setCities] = useState([]); const [cityDropDown, setCityDropDown] = useState([]); const [loading, setLoading] = useState(false); const [selectedCity, setSelectedCity] = useState({}); const [searchValue, setSearchValue] = useState(""); useEffect(() => { init(); }, []); const init = async () => { let SESSION = await DB.session() REQUEST('station_search', 'post', { Authorization: SESSION.token }, {}, {}, (res) => { setCities(res.data) }, (error) => { console.log(error) }) } const onChange = (value) => { let result = [] if(value) { for(var x=0;x 0) result.push(cities[x]) } } setSearchValue(value); setCityDropDown(result); } const onPressSearch = () => { if(cityDropDown.length !== 0) { setSearchValue(""); setCityDropDown([]); } else { search(); } } const search = async (item) => { setLoading(true); let SESSION = await DB.session() REQUEST("gas_stations_city", "get", { Authorization: SESSION.token }, { noID: true, value: item ? item.city_uuid : selectedCity.city_uuid }, {}, (res) => { if(res.data.length > 0){ setSearchValue(""); setSelectedCity({}); props.successSearch(res.data); } setLoading(false); }, (error) => { setLoading(false); }) } const renderItem = ({item, index}) => { const onPress = () => { setSearchValue(item.name); setSelectedCity(item); setCityDropDown([]); search(item); } return ( {item.name} ) } return ( { props.onPress ? City, Location { Platform.OS === 'android' ? : } { cityDropDown.length > 0 && } : { Platform.OS === 'android' ? : } { cityDropDown.length > 0 && } } ) } export default Search const styles = StyleSheet.create({ container: { marginHorizontal: 18, zIndex: 10, }, input: { width: '100%', borderWidth: 1, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 10, borderRadius: 10, borderColor: Theme.colors.searchGray, }, textInput: (apptheme) => { return { flex: 1, fontSize: 15, color: apptheme.theme.dark ? Theme.colors.white : Theme.colors.black } }, dropDown: (apptheme) => { return { position: 'absolute', height: 400, left: 0, right: 0, bottom: -405, backgroundColor: apptheme?.theme.dark ? apptheme?.theme.colors.border : Theme.colors.white, shadowColor: '#000', shadowOffset: { width: 0, height: .8}, shadowOpacity: .4, shadowRadius: 1, elevation: 3, paddingHorizontal: 5 } }, searchItemButton: { paddingVertical: 20, paddingHorizontal: 10, borderBottomWidth: 1, borderColor: Theme.colors.searchGray }, searchItemText: (appTheme) => { return { color: appTheme?.theme.dark ? appTheme?.theme.colors.text : Theme.colors.darkerGray } }, buttonText: { flex: 1, fontSize: 15, color: Theme.colors.searchGray } })