223 lines
6.7 KiB
JavaScript
223 lines
6.7 KiB
JavaScript
import React from 'react';
|
|
import {useState, useEffect} from 'react';
|
|
import {Divider} from 'react-native-elements';
|
|
import {
|
|
Text,
|
|
View,
|
|
TouchableOpacity,
|
|
ActivityIndicator,
|
|
SafeAreaView,
|
|
} from 'react-native';
|
|
import { connect } from 'react-redux';
|
|
import BottomSheet from 'reanimated-bottom-sheet';
|
|
import Theme from '../../../components/theme.style.js';
|
|
import Icon from '../../../components/icons';
|
|
import DB from '../../../components/storage';
|
|
import REQUEST from '../../../components/api';
|
|
|
|
const styles = {
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: '#f8f9fa',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
},
|
|
panel: {
|
|
flex: 1,
|
|
backgroundColor: '#fff',
|
|
},
|
|
panelHeader: {
|
|
height: 60,
|
|
backgroundColor: Theme.colors.lightGray,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
},
|
|
};
|
|
|
|
const renderStations = (data, onPress, onUpdateFavorite, props) => {
|
|
return data.map((station, index) => {
|
|
let stars = [1, 2, 3, 4, 5].map((star, i) => {
|
|
let name = station.stars >= star ? 'star' : 'staro';
|
|
let mgn = index > 0 ? 5 : 0;
|
|
return (
|
|
<TouchableOpacity key={i}>
|
|
<Icon.AntDesign
|
|
name={name}
|
|
style={{marginLeft: mgn}}
|
|
color={Theme.colors.yellow}
|
|
size={20}
|
|
/>
|
|
</TouchableOpacity>
|
|
);
|
|
});
|
|
|
|
// filter the value of key pair if it is empty
|
|
var filteredAddress = Object.entries(station.address).filter(([key, value]) => {
|
|
if(key === "stateCode") return `${value.replace(/\s/g, '')}`
|
|
return `${value}`
|
|
})
|
|
.map(([key, value]) => `${value}`)
|
|
|
|
return (
|
|
<TouchableOpacity
|
|
key={index}
|
|
activeOpacity={1}
|
|
onPress={() => onPress(station) || null}
|
|
key={index}
|
|
style={{backgroundColor: props.app_theme?.theme.colors.background}}>
|
|
<View style={{flex: 0, padding: 15, flexDirection: 'row'}}>
|
|
<View style={{flex: 5}}>
|
|
<Text
|
|
style={{
|
|
fontSize: 16,
|
|
padding: 5,
|
|
color: props.app_theme?.theme.colors.text,
|
|
}}>
|
|
{station.storeName}
|
|
</Text>
|
|
<Text
|
|
style={{
|
|
color: props.app_theme?.theme.colors.text,
|
|
padding: 5,
|
|
width: '90%',
|
|
fontSize: 13,
|
|
}}>
|
|
{filteredAddress.join(', ').toString()}
|
|
</Text>
|
|
<View style={{flexDirection: 'row', paddingTop: 7}}>{stars}</View>
|
|
</View>
|
|
{/* <TouchableOpacity
|
|
onPress={() => updateFavorite(station, index, onUpdateFavorite)}
|
|
style={{flex: 0, justifyContent: 'center'}}>
|
|
<Icon.Ionicons
|
|
name={station.favorite ? 'md-heart' : 'ios-heart-empty'}
|
|
size={28}
|
|
color={station.favorite ? 'red' : Theme.colors.darkGray}
|
|
/>
|
|
</TouchableOpacity> */}
|
|
</View>
|
|
<Divider
|
|
style={{marginTop: 5, padding: 0, margin: 0, width: Theme.screen.w}}
|
|
/>
|
|
</TouchableOpacity>
|
|
);
|
|
});
|
|
};
|
|
|
|
const updateFavorite = async (city, index, callback) => {
|
|
let session = await DB.session();
|
|
let urlTask = city.favorite
|
|
? 'station_delete_favorite'
|
|
: 'station_add_favorite';
|
|
let method = city.favorite ? 'delete' : 'get';
|
|
console.log(urlTask, method);
|
|
REQUEST(
|
|
urlTask,
|
|
method, { Authorization: session.token },
|
|
{
|
|
noID: true,
|
|
value: city.station_uuid,
|
|
}, {},
|
|
function (res) {
|
|
callback(index, city.favorite ? false : true);
|
|
},
|
|
function (error) {
|
|
console.log(error);
|
|
},
|
|
);
|
|
};
|
|
|
|
const renderStationPanel = (props) => {
|
|
const [panel, setpanel] = useState(null);
|
|
const [updatedFavorite, setupdatedfavorite] = useState(false);
|
|
const [updatedFavoriteIndex, setupdatedfavoriteindex] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [snap, setSnap] = useState(['10', '10', '3']);
|
|
const [initialSnap, setInitialSnap] = useState(0)
|
|
|
|
useEffect(() => {
|
|
init();
|
|
}, [props.visible]);
|
|
|
|
const init = () => {
|
|
if (props.visible == true) {
|
|
setSnap(['38', '8', '8']);
|
|
setInitialSnap(0)
|
|
} else if (props.data.length != 0) {
|
|
setSnap(['38', '8', '8']);
|
|
setInitialSnap(0)
|
|
} else {
|
|
setSnap(props.snapPoints);
|
|
setInitialSnap(2)
|
|
}
|
|
};
|
|
|
|
const getPanelTitle = () => {
|
|
if(props.error) return 'There was an error'
|
|
else if(props.loading || !props.isSearched && props.data.length == 0) return 'Getting Nearby Stations'
|
|
else if(props.isSearched) return 'Unioil Stations found: ' + (props.data.length > 0 ? props.data.length : 0)
|
|
else return 'Unioil Stations found: ' + (props.data.length > 0 ? props.data.length : 0)
|
|
}
|
|
|
|
const renderHeaders = () => {
|
|
return (
|
|
<View style={styles.panel}>
|
|
<View style={[styles.panelHeader, { backgroundColor: props.app_theme?.theme.dark ? Theme.colors.darkGray : Theme.colors.lightGray }]}>
|
|
<View
|
|
style={{
|
|
flexDirection: 'row',
|
|
justifyContent: 'center',
|
|
padding: 15,
|
|
}}>
|
|
<Text
|
|
style={{
|
|
flex: 4,
|
|
padding: 5,
|
|
color: props.app_theme?.theme.colors.text,
|
|
fontSize: 15,
|
|
}}>
|
|
{getPanelTitle()}
|
|
</Text>
|
|
<View style={{flex: 0, justifyContent: 'center'}}>
|
|
{props.error ? (
|
|
<TouchableOpacity onPress={props.onError} activeOpacity={1}>
|
|
<Text style={{color: '#fff'}}>Try Again</Text>
|
|
</TouchableOpacity>
|
|
) : props.loading ? (
|
|
<ActivityIndicator color={props.app_theme?.theme.dark ? props.app_theme?.theme.colors.text : "gray"} size={25} />
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const renderContents = () => {
|
|
return (
|
|
<SafeAreaView>
|
|
{props.data.length > 0 ? renderStations(props.data, props.onClick, props.onUpdateFavorite, props) : <View style={{height: 250, width: '100%', backgroundColor: props.app_theme?.theme.colors.background}}><View></View></View>}
|
|
</SafeAreaView>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<View style={{flex: 1}}>
|
|
<BottomSheet
|
|
snapPoints={props.data.length >= 3 ? snap : props.data.length < 2 ? ['23', '23', '23'] : ['13','13','6']}
|
|
renderContent={renderContents}
|
|
enabledBottomInitialAnimation={true}
|
|
enabledInnerScrolling={true}
|
|
initialSnap={0}
|
|
renderHeader={renderHeaders}/>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const mapStateToProps = (state) => {
|
|
return {
|
|
app_theme: state.appThemeReducer.theme
|
|
}
|
|
}
|
|
|
|
export default connect(mapStateToProps, null)(renderStationPanel) |