import React from 'react'; import { useState, useEffect, useRef } from 'react'; import { Divider } from 'react-native-elements'; import { Text, View, TouchableOpacity, ActivityIndicator, SafeAreaView, Platform, Alert, } from 'react-native'; import { connect } from 'react-redux'; import BottomSheet, { BottomSheetScrollView } from '@gorhom/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) => { // 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 ( onPress(station) || null} style={{ backgroundColor: props.app_theme?.theme.dark ? props.app_theme?.theme.colors.border : Theme.colors.white }}> {station.storeName} {filteredAddress.join(', ').toString()} ); }); }; 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'; REQUEST( urlTask, method, { Authorization: session.token }, { noID: true, value: city.station_uuid, }, {}, function (res) { callback(index, city.favorite ? false : true); }, function (err) { Alert.alert("Information", `\n${err.message}`); }, "Favorite", city.favorite ? "Delete" : "Add" ); }; 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 bottomSheetRef = useRef(null); useEffect(() => { init(); if (props.visible) { bottomSheetRef.current?.expand(); } else { bottomSheetRef.current?.close(); } }, [props.visible, props.data.length]); const init = () => { if (props.visible == true || props.data.length !== 0) { if (props.data.length === 1) { setSnap([Platform.OS === "android" ? '23%' : '16%', '8%', '8%']); } else if (props.data.length === 2) { setSnap([Platform.OS === "android" ? '39%' : '27%', '8%', '8%']); } else { setSnap(['38%', '8%', '8%']); } } else { setSnap([Platform.select({ ios: '14%', android: '12%' }), '8%', '8%']); } }; 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.loading && !props.isSearched && props.data.length == 0) return 'No Nearby Stations Found' 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 = () => ( {getPanelTitle()} {props.error ? ( Try Again ) : props.loading ? ( ) : null} ); const renderContents = () => ( {props.data.length > 0 ? ( renderStations(props.data, props.onClick, props.onUpdateFavorite, props) ) : ( )} ); return ( {renderHeaders()} {renderContents()} ); } const mapStateToProps = (state) => { return { app_theme: state.appThemeReducer.theme } } export default connect(mapStateToProps, null)(renderStationPanel)