import React, { Component } from 'react'; import { Animated, Alert, Platform, StyleSheet, View, Image, StatusBar, ScrollView, Text, ProgressBarAndroid, ActivityIndicator, TouchableOpacity } from 'react-native'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import I18n from 'react-native-i18n' import { TabView, TabBar, SceneMap } from 'react-native-tab-view'; import * as Utils from '../../utils/DeviceUtils'; import Icons from 'react-native-vector-icons/Ionicons' import { Images } from '../../config/Images'; import CustomButton from '../../components/CustomButton'; import { Color } from '../../config/Color'; import Tag from '../../components/Tag'; import { IlinkEmitter } from "../../utils/events"; import { CreditCardInput } from "react-native-credit-card-input"; import { Typography, FontWeight } from '../../config/typography'; import { responsiveHeight, responsiveWidth, } from 'react-native-responsive-dimensions'; import { getWalletDetailActivated, resetWalletListDetailReducer } from '../../webservice/WalletApi'; import { depositActionReset } from '../../webservice/DepositApi'; import { getWalletTransactionHistory, getWalletTransactionHistoryReset } from '../../webservice/WalletTransactionHistoryApi'; import { transferCommissionAction } from '../../webservice/WalletTransferCommission'; import { resetCommissionReducer } from '../../webservice/WalletTransferCommission'; import Dialog, { DialogContent, DialogTitle, DialogFooter, DialogButton } from 'react-native-popup-dialog'; import { baseUrl } from '../../webservice/IlinkConstants'; let moment = require('moment-timezone'); import 'moment/locale/fr' import 'moment/locale/es-us' import 'moment/locale/en-au' import 'moment/locale/en-ca' import 'moment/locale/en-ie' import 'moment/locale/en-il' import 'moment/locale/en-nz' import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import DeviceInfo from 'react-native-device-info' import { thousandsSeparators } from '../../utils/UtilsFunction'; let route = require('./../../route.json'); let slugify = require('slugify'); require('../../utils/Translations'); class WalletDetail extends Component { constructor(props) { super(props); this.state = { index: 0, routes: [ { key: 'depot', title: I18n.t('DEPOSIT') }, { key: 'retrait', title: I18n.t('WITHDRAWAL') } ], heightHeader: Utils.heightHeader(), isModalConfirmVisible: false, wallet: null, triggerTransferCommission: false, loading: false, isTriggerRefresh: false }; this.renderContent = null; slugify.extend({ '+': 'plus' }); this.scrollY = new Animated.Value(0); this.deltaY = new Animated.Value(0); this.bgBannerY = new Animated.Value(0); this.heightImageBanner = Utils.scaleWithPixel(250, 1); this.marginTopBanner = this.heightImageBanner - this.state.heightHeader - 40; this.isHomeRootView = this.props.navigation.state.params.hasOwnProperty('agentId'); IlinkEmitter.on("langueChange", this.updateLangue.bind(this)); this.props.getWalletTransactionHistoryReset(); this.props.depositActionReset(); this.props.resetWalletListDetailReducer(); this.currentLocale = (DeviceInfo.getDeviceLocale().includes("fr") ? "fr" : DeviceInfo.getDeviceLocale()); moment.locale(this.currentLocale); if (this.props.navigation.state.params.hasOwnProperty('agentId')) { let agentId = this.props.navigation.state.params.agentId; this.props.getWalletDetailActivated(agentId); } else { this.props.getWalletTransactionHistory(this.props.navigation.state.params.wallet.id); this.state.wallet = this.props.navigation.state.params.wallet; } } static options(passProps) { return { topBar: { drawBehind: false, visible: true, animate: true, buttonColor: 'white', background: { color: 'white', }, rightButtons: [] }, backButton: { visible: true, color: "white" }, buttonColor: "white", background: { color: Color.primaryDarkColor }, statusBar: { drawBehind: false, visible: true, } }; } static navigationOptions = ({ navigation }) => { return { //title: this.isHomeRootView ? this.props.navigation.state.params.wallet.network, headerStyle: { backgroundColor: Color.primaryColor, paddingTop: 10 }, headerTitleStyle: { color: "white" } } }; componentDidMount() { const { result } = this.props; if (this.isHomeRootView) { if (result !== null) { if (typeof result.response !== 'undefined') { const wallet = result.response[0]; this.setState({ wallet: wallet, isTriggerRefresh: false }) } } } /*else { if (this.state.isTriggerRefresh) { console.log("this.state.isTriggerRefresh", result); if (result !== null) { if (typeof result.response !== 'undefined') { const wallet = result.response.filter((item) => item.id === this.state.wallet.id); this.setState({ wallet: wallet, isTriggerRefresh: false }) console.log("REPONSE FILTER", result.response.filter((item) => item.id === this.state.wallet.id)); } } } } */ } shouldComponentUpdate(nextProps, nextState) { if (this.state.triggerTransferCommission !== nextState.triggerTransferCommission) { return false; } else { return true; } } componentWillReceiveProps(nextProps) { if (nextProps.loading || nextProps.loadingTransferCommission) this.setState({ loading: true }) else this.setState({ loading: false }) } isEmptyObject = (obj) => { for (let prop in obj) { if (obj.hasOwnProperty(prop)) { return false; } } return JSON.stringify(obj) === JSON.stringify({}); } getWalletIcon = (wallet) => { return `${baseUrl}/datas/img/network/${slugify(wallet.network, { lower: true })}-logo.png`; } getCreationDateToHumanFormat = (date) => { let re = moment.tz(date, 'Etc/GMT+0').format(); return moment(re).fromNow(); } updateLangue() { this.props.navigation.setParams({ name: I18n.t('WALLET') }) this.forceUpdate() } handleIndexChange = index => this.setState({ index }); imageScale = () => { return this.scrollY.interpolate({ inputRange: [0, 100], outputRange: [1, 0.5], extrapolate: 'clamp', }); } imageTranslateY = () => { return this.scrollY.interpolate({ inputRange: [0, 100], outputRange: [-5, 50], extrapolate: 'clamp', }); } bgBannerY = () => { return this.scrollY.interpolate({ inputRange: [0, 100], outputRange: [150, 0], extrapolate: 'clamp', }); } refresh = () => { const { wallet } = this.props.navigation.state.params; console.log("STATE NAVIGATION PARAMS REFRESH", this.props.navigation.state); if (!this.isHomeRootView) { this.props.getWalletDetailActivated(wallet.agentId); this.setState({ isTriggerRefresh: true }); if (typeof this.props.navigation.state.params.onRefreshDetail !== 'undefined') this.props.navigation.state.params.onRefreshDetail(); } else { let agentId = this.props.navigation.state.params.agentId; this.props.getWalletDetailActivated(agentId); this.props.getWalletTransactionHistory(this.state.wallet.id); } } renderTabBar = props => ( ( { (route.key === 'depot') ? () : () } {` ${route.title}`} )} /> ); renderHeader = (wallet) => ( {I18n.t('COUNTRY')} {wallet.country} {wallet.network} { const { result } = this.props; if (result !== null) { if (typeof result.response !== 'undefined') { const wallet = result.response.filter((item) => item.id === this.state.wallet.id) if (parseInt(wallet.balance_com) === 0) { Alert.alert( I18n.t("ERROR_TRANSFER_COMMISSION"), I18n.t("COMMISSION_TRANSFER_ERROR_EMPTY"), [ { text: I18n.t("OK"), onPress: () => { this.props.resetCommissionReducer(); } } ], { cancelable: false } ) } else this.renderDialogConfirmTransferCommission() } } else { if (parseInt(this.state.wallet.balance_com) === 0) { Alert.alert( I18n.t("ERROR_TRANSFER_COMMISSION"), I18n.t("COMMISSION_TRANSFER_ERROR_EMPTY"), [ { text: I18n.t("OK"), onPress: () => { this.props.resetCommissionReducer(); } } ], { cancelable: false } ) } else this.renderDialogConfirmTransferCommission() } }}> {I18n.t('TRANSFER_TO_PRINCIPAL_ACCOUNT')} {I18n.t('CREATION_DATE')} {this.getCreationDateToHumanFormat(wallet.created_date)} ); renderLoader = () => { return ( {Platform.OS === 'android' ? ( <> {I18n.t('LOADING_DOTS')} ) : <> {I18n.t('LOADING_DOTS')} } ) } renderAccountDetail = (wallet) => ( {I18n.t('PRINCIPAL_ACCOUNT_TITLE')} {thousandsSeparators(wallet.balance_princ)} {I18n.t('COMMISSION_ACCOUNT_TITLE')} {thousandsSeparators(wallet.balance_com)} ); renderDetailWallet = (walletParam) => { const wallet = Array.isArray(walletParam) ? walletParam[0] : walletParam; return ( !this.isEmptyObject(wallet) ? (<> {this.state.triggerTransferCommission && this.renderDialogTransferCommissionResponse()} {this.state.loading ? {I18n.t("LOADING_DOTS")} : null } {this.renderHeader(wallet)} {this.renderAccountDetail(wallet)} {!this.isHomeRootView && ( <> {I18n.t('TRANSACTIONS')} this.props.navigation.push(route.walletDepot, { wallet, onGoBack: () => this.refresh(), })} activeOpacity={0.9}> {I18n.t('DEPOSIT')} {I18n.t('DEPOSIT_DESCRIPTION')} this.props.navigation.push(route.walletRetrait, { wallet, onGoBack: () => this.refresh(), })} activeOpacity={0.9}> {I18n.t('WITHDRAWAL')} {I18n.t('WITHDRAWAL_DESCRIPTION')} )} {!this.isHomeRootView && this.renderHistoryTransaction()} ) : ( {I18n.t('NO_WALLET_ACTIVED')} ) ) } renderHistoryTransactionItem = (item) => { let re = moment.tz(item.date, 'Etc/GMT+0').format(); let date = moment(re).fromNow(); return ( {item.type === 'debit' ? ( {I18n.t('WITHDRAWAL_TRANSACTION_HISTORY_DESCRIPTION')} {item.montant} ) : ( {I18n.t('DEPOSIT_TRANSACTION_HISTORY_DESCRIPTION')} {item.montant} ) } {date} ); } renderHistoryTransactionList = () => { const { resultTransaction, errorTransaction } = this.props; if (errorTransaction !== null) { if (typeof errorTransaction.data !== 'undefined') { return ( {errorTransaction.data.error} ) } else { return ( {errorTransaction} ) } } if (resultTransaction !== null) { if (resultTransaction.response !== null) { return ( Array.isArray(resultTransaction.response) && (resultTransaction.response.length) > 0 ? ( resultTransaction.response.map((item, ) => ( this.renderHistoryTransactionItem(item) )) ) : ( {I18n.t('NO_WALLET_HISTORY')} ) ) } } } renderHistoryTransaction = () => { return ( <> {I18n.t('TRANSACTION_HISTORY')} { this.props.loadingTransaction ? ( {Platform.OS === 'android' ? ( <> {I18n.t('LOADING_DOTS')} ) : <> {I18n.t('LOADING_DOTS')} } ) : this.renderHistoryTransactionList() } ) } renderDialogConfirmTransferCommission = () => { Alert.alert( I18n.t("CONFIRM"), I18n.t('CONFIRM_TRANSFER_COMMISSION') , [ { text: I18n.t("NO"), onPress: () => { } }, { text: I18n.t("YES"), onPress: () => { this.props.transferCommissionAction(this.state.wallet.id); this.setState({ triggerTransferCommission: true }); } } ], { cancelable: false } ) } renderDialogTransferCommissionResponse = () => { const { resultTransferCommission, errorTransferCommission } = this.props; if (errorTransferCommission !== null) { if (typeof errorTransferCommission.data !== 'undefined') { Alert.alert( I18n.t("ERROR_TRANSFER_COMMISSION"), errorTransferCommission.data.error, [ { text: I18n.t("OK"), onPress: () => { this.props.resetCommissionReducer(); } } ], { cancelable: false } ) } } if (resultTransferCommission !== null) { if (resultTransferCommission.response !== null) { Alert.alert( I18n.t("SUCCESS_TRANSFER_COMMISSION"), I18n.t("COMMISSION_TRANSFER_SUCCESS"), [ { text: I18n.t("OK"), onPress: () => { this.props.resetCommissionReducer(); this.refresh(); } } ], { cancelable: false } ) } } } getWalletParam = () => { const { result } = this.props; const isHomeRootView = this.props.navigation.state.params.hasOwnProperty('agentId'); if (!isHomeRootView) return this.props.navigation.state.params.wallet; else return this.props.response.filter((item) => item.id === this.state.wallet.id); } render() { const isHomeRootView = this.props.navigation.state.params.hasOwnProperty('agentId'); console.log("WALLET DETAILS PROPS", this.props); /*console.log("isHomeRootView", isHomeRootView); console.log("this.props.loading || this.props.loadingTransferCommission", this.props.loading || this.props.loadingTransferCommission); */ return ( <> { !isHomeRootView ? ( (this.props.loading || this.props.loadingTransferCommission) ? this.renderDetailWallet(this.getWalletParam()) : ( ((this.props.result !== null)) ? this.renderDetailWallet(this.props.result.response.filter((item) => item.id === this.state.wallet.id)) : this.renderDetailWallet(this.props.navigation.state.params.wallet) ) ) : ((this.props.loading || this.props.loadingTransferCommission) ? this.renderLoader() : ( this.props.result !== null && this.renderDetailWallet(this.props.result.response.filter((item) => item.id === this.state.wallet.id)) ) ) } ); } } const mapStateToProps = state => ({ loading: state.walletDetailReducer.loading, result: state.walletDetailReducer.result, error: state.walletDetailReducer.error, loadingTransaction: state.walletHistoryReducer.loadingTransaction, resultTransaction: state.walletHistoryReducer.resultTransaction, errorTransaction: state.walletHistoryReducer.errorTransaction, loadingTransferCommission: state.walletTransferCommissionReducer.loadingTransferCommission, resultTransferCommission: state.walletTransferCommissionReducer.resultTransferCommission, errorTransferCommission: state.walletTransferCommissionReducer.errorTransferCommission, }); const mapDispatchToProps = dispatch => bindActionCreators({ getWalletDetailActivated, getWalletTransactionHistory, transferCommissionAction, resetWalletListDetailReducer, resetCommissionReducer, getWalletTransactionHistoryReset, depositActionReset }, dispatch); export default connect(mapStateToProps, mapDispatchToProps)(WalletDetail); const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: Color.containerBackgroundColor }, indicator: { height: 2 }, tab: { width: Utils.getWidthDevice() / 2 }, tabbar: { height: 40 }, imgBanner: { width: '100%', height: 250, position: 'absolute', }, containField: { padding: 10, marginBottom: 20, borderWidth: 0.5, shadowOffset: { width: 1.5, height: 1.5 }, shadowOpacity: 1.0, elevation: 5, flexDirection: "row", height: 140, borderRadius: 10 }, paymentItem: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", borderBottomWidth: 1, paddingVertical: 5, width: responsiveWidth(100), marginBottom: 15 }, iconContent: { width: 60, marginRight: 10, alignItems: "center" }, contentLeftItem: { flex: 1, paddingTop: 40, paddingLeft: 10, paddingRight: 10, alignItems: "center" }, tagFollow: { width: 150, margin: 10 }, profileItem: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", paddingBottom: 20, paddingTop: 20 }, checkDefault: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", borderBottomWidth: 1, paddingVertical: 10, marginTop: 5 }, blockView: { paddingVertical: 10, borderBottomWidth: 0.5, }, circlePoint: { width: 50, height: 50, borderRadius: 25, marginRight: 5, alignItems: 'center', justifyContent: 'center', }, transactionContainer: { flexDirection: 'row', paddingTop: 10, }, containerTouch: { flex: 1, flexDirection: 'row', alignItems: 'center', shadowColor: Color.borderColor, borderColor: Color.borderColor, borderWidth: 0.5, shadowOffset: { width: 1.5, height: 1.5 }, shadowOpacity: 1.0, elevation: 5, borderRadius: 10, backgroundColor: Color.cardBackgroundColor }, contain: { flexDirection: 'row', justifyContent: 'space-between', }, imageBanner: { marginTop: 15, marginLeft: 5, width: Utils.scaleWithPixel(30), height: Utils.scaleWithPixel(30) }, content: { height: Utils.scaleWithPixel(60), paddingHorizontal: 10, justifyContent: 'space-between', alignItems: 'flex-start', flex: 1, }, contentTitle: { paddingTop: 5, } })