ilink-world/app/screens/WebviewScreen.js

226 lines
7.5 KiB
JavaScript
Raw Normal View History

import React, {useState, useEffect, useCallback} from 'react';
import {ActivityIndicator, Platform, Alert, ProgressBarAndroid, Dimensions, StyleSheet, View, Text,} from 'react-native';
2022-02-28 09:41:54 +00:00
import WebView from 'react-native-webview';
import {connect, useDispatch} from 'react-redux';
import {ScreenComponent} from "../components/ScreenComponent";
import {readUser} from "../webservice/AuthApi";
import queryString from 'query-string';
import I18n from "react-native-i18n";
import {bindActionCreators} from "redux";
import { envoieUserWalletToCardAction, getCommissionUserWalletToCardAction } from "../webservice/EnvoieUserApi"
2023-10-05 06:09:30 +00:00
import { fetchActivePaySubscription } from '../redux/insurance/insurance.actions';
2022-02-28 09:41:54 +00:00
const WebviewScreen = ({
navigation,
getCommissionUserWalletToCardAction,
envoieUserWalletToCardAction,
2023-10-05 06:09:30 +00:00
fetchActivePaySubscription,
saveTransaction
}) => {
2022-02-28 09:41:54 +00:00
let webviewRef = null;
const dispatch = useDispatch();
2023-10-05 06:09:30 +00:00
const [hasRedirected, setHasRedirected] = useState(false);
const [constRedirect, setCountRedirect ] = useState(0)
2022-02-28 09:41:54 +00:00
2023-10-05 06:09:30 +00:00
const {url, requestBody, transactionType, subscription} = navigation.state.params;
//useEffect(
// useCallback(() => {
// const onBackPress = () => {
// if (backButtonEnabled) {
// webviewRef?.goBack();
// } else {
// navigation.goBack();
// }
// return false;
// };
// BackHandler.addEventListener('hardwareBackPress', onBackPress);
// return () =>
// BackHandler.removeEventListener(
// 'hardwareBackPress',
// onBackPress,
// );
// }, []);
2023-10-05 06:09:30 +00:00
useEffect(() => {
//useCallback(() => {
if(saveTransaction.result !== null) {
Alert.alert(
I18n.t("SUCCESS"),
2023-10-05 06:09:30 +00:00
saveTransaction.result.response,
[
{
text: I18n.t("OK"), onPress: () => {
readUser().then(() => {
if (user) {
if (user !== undefined) {
getCommissionUserWalletToCardAction(user.phone, 0)
}
}
});
navigation.goBack()
}
}
],
{cancelable: false}
2022-02-28 09:41:54 +00:00
);
}
2023-10-05 06:09:30 +00:00
if(saveTransaction.error !== null) {
navigation.goBack();
Alert.alert(
I18n.t("ERROR_TREATMENT_REQUEST"),
2023-10-05 06:09:30 +00:00
saveTransaction.error.data.error,
[
{
text: I18n.t("OK"), onPress: () => {
navigation.goBack();
}
}
],
{cancelable: false}
)
}
2023-10-05 06:09:30 +00:00
}, [saveTransaction]);
2022-02-28 09:41:54 +00:00
const renderLoader = () => (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
{Platform.OS === 'android'
?
(
<>
<ProgressBarAndroid/>
<Text>{I18n.t('LOADING_DOTS')}</Text>
</>
) :
<>
<ActivityIndicator size="large" color={'#ccc'}/>
<Text>{I18n.t('LOADING_DOTS')}</Text>
</>
}
</View>
)
2023-10-05 06:09:30 +00:00
const checkPaymentStatus = (message) => {
message = JSON.parse(message);
2023-10-05 06:09:30 +00:00
let status = message['status'];
let transaction_id = message['transaction_id'];
2023-10-05 06:09:30 +00:00
if(status === "0") {
navigation.goBack()
Alert.alert(I18n.t('PAYMENT_ERROR'), I18n.t('PAYMENT_COULD_NOT_MADE'), [{text:"Ok",onPress:()=>{}}])
}
if(status === "1") {
2023-10-05 06:09:30 +00:00
if(transactionType === "USER_CARD_WALLET" ){
envoieUserWalletToCardAction({
payment_transaction_id: transaction_id,
...requestBody
}, true)
navigation.goBack()
2023-10-05 06:09:30 +00:00
}
if(transactionType === "USER_PAY_INSURANCE") {
fetchActivePaySubscription(subscription.id, {
payment_transaction_id: transaction_id,
...requestBody
}, true)
navigation.goBack()
}
2023-10-05 06:09:30 +00:00
// Marquez la redirection comme déjà effectuée
setHasRedirected(true);
2023-10-05 06:09:30 +00:00
// Retournez dans l'application
navigation.goBack();
}
};
2022-02-28 09:41:54 +00:00
return (
<ScreenComponent>
<View style={styles.contain}>
<WebView
source={{uri: url}}
style={styles.webview}
ref={ref => {
webviewRef = ref;
}}
javaScriptEnabled
domStorageEnabled
2023-10-05 06:09:30 +00:00
onMessage={({nativeEvent}) => {
checkPaymentStatus(nativeEvent.data);
2022-02-28 09:41:54 +00:00
}}
renderLoading={() => (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center', marginTop: -42}}>
{Platform.OS === 'android'
?
(
<>
<ProgressBarAndroid/>
<Text>{I18n.t('LOADING_DOTS')}</Text>
</>
) :
<>
<ActivityIndicator size="large" color={'#ccc'}/>
<Text>{I18n.t('LOADING_DOTS')}</Text>
</>
}
</View>
)}
2022-02-28 09:41:54 +00:00
startInLoadingState
/>
2023-10-05 06:09:30 +00:00
2022-02-28 09:41:54 +00:00
</View>
</ScreenComponent>
2022-02-28 09:41:54 +00:00
);
};
const mapStateToProps = state => ({
2023-10-05 06:09:30 +00:00
saveTransaction: state.envoieUserWalletToCardReducer
});
const mapDispatchToProps = dispatch => bindActionCreators({
envoieUserWalletToCardAction,
2023-10-05 06:09:30 +00:00
getCommissionUserWalletToCardAction,
fetchActivePaySubscription
}, dispatch);
2022-02-28 09:41:54 +00:00
export default connect(mapStateToProps, mapDispatchToProps)(WebviewScreen);
2022-02-28 09:41:54 +00:00
const styles = StyleSheet.create({
contain: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingLeft: 20,
paddingRight: 20,
},
contentModal: {
width: '100%',
borderRadius: 8,
padding: 8,
},
item: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 20,
},
contentAction: {
flexDirection: 'row',
justifyContent: 'flex-end',
paddingTop: 24,
},
webview: {
height: Dimensions.get('window').height,
width: Dimensions.get('window').width,
},
});