import React from 'react';
import { useState, useEffect, useRef } from 'react';
import { connect } from 'react-redux';
import {
Text,
View,
TouchableOpacity,
Linking,
Platform,
Alert,
Modal,
FlatList,
Image,
ActivityIndicator,
} from 'react-native';
import {
NavigationApps,actions,
googleMapsTravelModes,
mapsTravelModes
} from "../../../components/navigationapps";
import { Divider } from 'native-base';
import { noInternetError } from '../../../utils/errorHandler';
import REQUEST_POST_PAY from '../../../components/api/postpayapi';
import Assets from '../../../components/assets.manager.js';
import Theme from '../../../components/theme.style.js';
import Icon from '../../../components/icons';
import DB from '../../../components/storage/';
const styles = {
container: {
flex: 1,
backgroundColor: '#f8f9fa',
alignItems: 'center',
justifyContent: 'center'
},
panel: {
flex: 1,
backgroundColor: 'white',
position: 'relative'
},
panelHeader: {
height: 60,
backgroundColor: Theme.colors.lightGray,
alignItems: 'center',
justifyContent: 'center'
},
centeredView: {
flex: 1,
justifyContent: "center",
backgroundColor: '#00000090',
},
modalView: {
margin: 25,
padding: 20,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5
},
selectedPumpBorder: {
backgroundColor: '#ffbaa3',
borderRadius: 10,
borderWidth: 2,
borderColor: Theme.colors.primary,
padding: 5
},
unselectedPumpBorder: {
borderRadius: 10,
borderWidth: 2,
padding: 5
}
}
const PayatpumpStationDetails = (props) => {
const [panel, setpanel] = useState(null)
const [updateFavorite, setUpdateFavorite] = useState(0)
const [updateFavoriteVal, setUpdateFavoriteVal] = useState(false)
const [showModal, setShowModal] = useState(false)
const [error, setError] = useState(null)
const [selectedPump, setSelectedPump] = useState(null)
const [selectedPumpIndex, setSelectedPumpIndex] = useState(0)
const [initiateCancel, setInitiateCancel] = useState(false)
const [initiateProceed, setInitiateProceed] = useState(false)
const [confirmProceed, setConfirmProceed] = useState(false)
const pumpListRef = useRef(null);
useEffect(() => {
if(props.visible){
panel.show()
}
return setpanel(null)
}, [props.visible])
const renderStationDetails = (station, onPress) => {
// 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 (
{filteredAddress.join(', ').toString()}
Contact:
{
let url = Platform.OS == 'ios' ? 'telprompt:' : 'tel:'
Linking.canOpenURL(url).then(supported => {
if (!supported) {
alert("Call Not Supported")
} else {
Alert.alert("Call Customer Service", '\n' + "You will be redirected to the dialer to call Unioil Customer Service", [
{
text: 'Cancel',
style: 'cancel',
},{
text: 'OK', onPress: () => Linking.openURL(`${url}${num}`)
}
],
{cancelable: true})
return true
}
}).catch(err => {
console.error('An error occurred', err)
})
}}>
{station.phone}
{
if(props.data.fuelService?.fuelingPoints == undefined) {
Alert.alert("Information", '\n' + "No pump available.")
} else {
setShowModal(true)
}
}}
style={{ backgroundColor: Theme.colors.primary, padding: 12, alignItems: 'center', justifyContent: 'center', borderRadius: 5 }}>
PUMP AND GO
{PumpSelectorModalView()}
)
}
const resetFlags = () => {
setShowModal(false)
setSelectedPump(null)
setSelectedPumpIndex(null)
setInitiateCancel(false)
setInitiateProceed(false)
setConfirmProceed(false)
}
const initiateTransaction = async () => {
let store = props.data
let pump = selectedPump
let body = {
store_id: store.storeId,
pump: pump.pumpNumber
}
REQUEST_POST_PAY('postClaim', 'post', {}, {}, body, async (res) => {
if(res.status) {
resetFlags()
if(!res.data.saleItems) {
return Alert.alert('Information', '\n' + "No Transaction available");
}
props.onTransaction(res.data, store.storeId, selectedPump)
} else {
resetFlags()
setTimeout(() => {
Alert.alert('Information', res?.message?.key == 'buy_gas_pump_unavailable' ? '\n' + `Pump ${pump.pumpNumber} is not yet available. Try again.` : res?.message.key == 'pay_outside_petrozone_error' ? '\n' + res?.message.partnerApiMessage : '\n' + 'Failed transaction. Try again.')
}, 300)
}
}, (error) => {
resetFlags()
setTimeout(() => {
if(!noInternetError(error)) {
Alert.alert('Information', error?.message?.key == 'buy_gas_pump_unavailable' ? '\n' + `Pump ${pump.pumpNumber} is not yet available. Try again.` : error?.message?.key == 'pay_outside_petrozone_error' ? '\n' + error?.ermessageror?.partnerApiMessage : '\n' + 'Failed transaction. Try again.')
} else {
noInternetError(error);
}
}, 300)
})
}
const PumpSelectorModalView = () => {
return (
{!initiateCancel && !confirmProceed && ( {
setInitiateCancel(true)
if(initiateProceed) {
setInitiateProceed(false)
}
}}>
)}
{(!initiateCancel && !initiateProceed && !confirmProceed) && PumpListView(selectedPump)}
{(initiateCancel && !initiateProceed && !confirmProceed) && CancelPumpSelectorView()}
{(initiateProceed && !initiateCancel && !confirmProceed) && ProceedPumpSelectorView()}
{confirmProceed && ConfirmPumpSelectorView()}
)
}
const CancelPumpSelectorView = () => {
return (
<>
Are you sure you want to{'\n'}cancel the transaction?
{
setInitiateCancel(false)
selectedPump != null ? setInitiateProceed(true) : setInitiateProceed(false)
}} style={{ width: 100, paddingVertical: 5, margin: 12, backgroundColor: Theme.colors.white, borderColor: Theme.colors.primary, borderWidth: 0.5, alignItems: 'center', justifyContent: 'center', borderRadius: 5 }}>
No
{
setShowModal(false)
setInitiateCancel(false)
setInitiateProceed(false)
setSelectedPump(null)
setSelectedPumpIndex(null)
}} style={{ width: 100, paddingVertical: 5, margin: 12, backgroundColor: Theme.colors.primary, justifyContent: 'center', alignItems: 'center', borderRadius: 5 }}>
Yes
>
)
}
const ProceedPumpSelectorView = () => {
let message = selectedPump != null ? `This will process your transaction at` : "No selected pump."
return (
{message} {selectedPump != null && Pump {selectedPump.pumpNumber}.}
Please ensure that this is the correct pump and click OK below to proceed.
{
setInitiateCancel(false)
setInitiateProceed(false)
if(selectedPumpIndex > 3) {
setTimeout(() => {
pumpListRef.current?.scrollToIndex({ animated: true, index: Math.floor(selectedPumpIndex / 2) })
}, 500)
}
}}
style={{ paddingVertical: 5, width: 120, backgroundColor: Theme.colors.primary, justifyContent: 'center', alignItems: 'center', borderRadius: 5 }}>
OK
)
}
const ConfirmPumpSelectorView = () => {
let message = selectedPump != null ? "Processing..." : "No selected pump."
return (
<>
{
Platform.select({
ios: ,
android:
})
}
{message}
>
)
}
const PumpListView = (currentSelectedPump) => {
return (
<>
Please {selectedPump ? "confirm" : "select"} your Pump no.{'\n'} to process the payment.
index.toString()}
contentContainerStyle={{ alignItems: 'center'}}
data={props.data.fuelService?.fuelingPoints}
scrollEnabled={true}
showsVerticalScrollIndicator={false}
renderItem={({item, index}) => {
let available = item.pumpStatus == "Available"
return (
{
setInitiateProceed(true)
setSelectedPump(item)
setSelectedPumpIndex(index)
}}
style={{marginHorizontal: 15, marginBottom: 10, borderColor: (currentSelectedPump != null && currentSelectedPump.pumpNumber == item.pumpNumber) ? Theme.colors.primary : "transparent", borderWidth: 4, borderRadius: 15}}>
{index + 1}
)
}}
numColumns={2}
/>
{
if(selectedPump != null) {
setConfirmProceed(true)
initiateTransaction()
}
}}>
OK
>
)
}
if(props.data === null) {
return null
}
return (
{props.data.storeName}
{}} activeOpacity={1} style={{top: -25}}>
{(props.data && props.data.address != undefined && props.data.address != "") ?
}
actionSheetTitle="Choose an application to view the route"
address={props.data.storeName} // address to navigate by for all apps
waze={{ address: props.data.address, lat: props.data.geoLocation.latitude, lon: props.data.geoLocation.longitude, action: actions.navigateByLatAndLon}} // specific settings for waze
googleMaps={{ address: props.data.address, lat: props.data.geoLocation.latitude, lon: props.data.geoLocation.longitude, action: actions.navigateByAddress, travelMode:googleMapsTravelModes.driving}} // specific settings for google maps
maps={{ address: props.data.address, lat: props.data.geoLocation.latitude, lon: props.data.geoLocation.longitude, action: actions.navigateByAddress, travelMode:mapsTravelModes.driving}} /> // specific settings for maps
: null}
Directions
{renderStationDetails(props.data, props.onClick)}
);
}
const mapStateToProps = (state) => {
return {
app_theme: state.appThemeReducer.theme
}
}
export default connect(mapStateToProps, null)(PayatpumpStationDetails)