import isEqual from 'lodash/isEqual';
import isNil from 'lodash/isNil';
import React, {Component} from 'react';
import {
ActivityIndicator,
Alert,
Platform,
ProgressBarAndroid,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
import I18n from 'react-native-i18n';
import {responsiveHeight, responsiveWidth} from 'react-native-responsive-dimensions';
import {ProgressDialog} from 'react-native-simple-dialogs';
import {connect} from 'react-redux';
import omit from 'lodash/omit';
import {bindActionCreators} from 'redux';
import {Color} from '../../config/Color';
import {IlinkEmitter} from '../../utils/events';
import {readUser} from '../../webservice/AuthApi';
import {refundCreditDemandUserAction, refundCreditDemandUserReset} from '../../webservice/user/NanoCreditApi';
import {
getNanoCreditUserHistoryInProgressAction,
getNanoCreditUserHistoryInProgressReset,
} from "../../webservice/NanoCreditApi";
import {Typography} from "../../config/typography";
import {
cutString,
cutStringWithoutDot,
displayTransactionType,
nanoCreditHistoryLabel
} from "../../utils/UtilsFunction";
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import thousands from "thousands";
import * as Utils from "../../utils/DeviceUtils";
import Dialog from "react-native-dialog";
let theme = require('../../utils/theme.json');
let route = require('../../route.json');
class RefundNanoCreditUser extends Component {
constructor(props) {
super(props);
this.state = {
password: null,
idDemand: null,
codeGroup: null,
loading: false,
user: null,
triggerSubmitClick: false,
isSubmitClick: false,
isDataSubmit: false,
isModalConfirmVisible: false,
isGroupToModify: false,
displayModalHistory: false,
historyItemDetail: null,
isDataHasLoaded: false,
displayPasswordModal: false,
wallet: this.props.navigation.state.params.wallet,
};
this.props.getNanoCreditUserHistoryInProgressReset();
}
static navigatorStyle = {
navBarBackgroundColor: Color.primaryColor,
statusBarColor: Color.primaryDarkColor,
navBarTextColor: '#FFFFFF',
navBarButtonColor: '#FFFFFF'
};
static navigationOptions = () => {
return {
drawerLabel: () => null,
headerTitle: I18n.t('REFUND_NANO_CREDIT'),
headerTintColor: 'white',
headerStyle: {
backgroundColor: Color.primaryColor,
marginTop: 0,
color: 'white'
},
headerTitleStyle: {
color: "white"
},
title: I18n.t('REFUND_NANO_CREDIT')
}
};
componentDidMount() {
readUser().then((user) => {
if (user) {
if (user !== undefined) {
this.setState({user});
this.props.getNanoCreditUserHistoryInProgressAction(user.id);
}
}
});
}
ckeckIfFieldIsOK(champ) {
return (isNil(champ) || isEqual(champ.length, 0));
}
renderRefundNanoCreditResponse = () => {
const {result, error} = this.props;
if (error !== null) {
if (typeof error.data !== 'undefined') {
Alert.alert(
I18n.t("ERROR_LABLE"),
error.data.error,
[
{
text: I18n.t("OK"), onPress: () => {
this.props.refundCreditDemandUserReset();
}
}
],
{cancelable: false}
)
}
}
if (result !== null) {
if (result.response !== null) {
Alert.alert(
I18n.t("REFUND_DONE"),
result.response,
[
{
text: I18n.t("OK"), onPress: () => {
this.props.refundCreditDemandUserReset();
IlinkEmitter.emit("refreshWallet");
IlinkEmitter.emit("updateNanoCreditAccount");
this.props.navigation.pop();
}
}
],
{cancelable: false}
)
}
}
}
updateLangue() {
this.props.navigation.setParams({name: I18n.t('REFUND_NANO_CREDIT')})
this.forceUpdate()
}
ckeckIfFieldIsOK(champ) {
return (isNil(champ) || isEqual(champ.length, 0));
}
onSubmitRefundNanoCredit = () => {
const {idDemand, password} = this.state;
if (this.ckeckIfFieldIsOK(idDemand))
this.idDemandAnim.shake(800);
else if (this.ckeckIfFieldIsOK(password))
this.passwordAnim.shake(800);
else {
this.props.refundCreditDemandUserAction({
id_user: this.state.user.id,
id_demande: this.state.idDemand,
password: this.state.password
}, 0);
}
this.setState({
isDataSubmit: true
});
}
renderLoader = () => {
return (
)
}
renderModalHistoryDetail = () => {
return (
{I18n.t('DEMAND_DETAIL')}
{
Type
{I18n.t(displayTransactionType(this.state.historyItemDetail.type_historique))}
{I18n.t('ID_DEMAND')}
{this.state.historyItemDetail.id_demande}
{I18n.t('STATUS')}
{this.state.historyItemDetail.etat}
{I18n.t('DEMAND_DURATION_IN_MONTH')}
{this.state.historyItemDetail.duree_mois}
{I18n.t('AMOUNT')}
{this.state.historyItemDetail.montant + ' ' + this.state.wallet.currency_code}
{I18n.t('AMOUNT_REFUND')}
{this.state.historyItemDetail.montant_rembourse + ' ' + this.state.wallet.currency_code}
{I18n.t('AMOUNT_PARTIALLY_REFUND')}
{this.state.historyItemDetail.partiellement_rembourse + ' ' + this.state.wallet.currency_code}
{I18n.t('TAXES')}
{this.state.historyItemDetail.taxe + ' ' + this.state.wallet.currency_code}
{I18n.t('INTERET')}
{this.state.historyItemDetail.interet + ' ' + this.state.wallet.currency_code}
{I18n.t('WITHDRAWAL_IN_CASH')}
{this.state.historyItemDetail.retrait_cash == 1 ? I18n.t('YES') : I18n.t('NO')}
{I18n.t('CREATION_DATE')}
{this.state.historyItemDetail.date_creation}
{I18n.t('VALIDATION_DATE')}
{this.state.historyItemDetail.date_validation}
{I18n.t('DATE_REMBOURSEMENT_PREVU')}
{this.state.historyItemDetail.date_remboursement_prevu}
{I18n.t('DATE_REMBOURSEMENT')}
{this.state.historyItemDetail.date_remboursement}
}
{
this.setState({
displayModalHistory: !this.state.displayModalHistory,
});
}}/>
{
this.setState({
displayPasswordModal: !this.state.displayPasswordModal,
});
}}/>
);
}
renderPasswordDialog = () => {
return (
{I18n.t('PASSWORD')}
{
this.setState({password})
}}
secureTextEntry={true}
value={this.state.password}
onSubmitEditing={() => {
this.setState({
displayPasswordModal: false,
displayModalHistory: false,
isDataSubmit: true
});
this.props.refundCreditDemandUserAction({
id_user: this.state.user.id,
id_demande: this.state.historyItemDetail.id_demande,
password: this.state.password
});
}}/>
{
this.setState({displayPasswordModal: !this.state.displayPasswordModal});
}}/>
{
this.setState({
displayPasswordModal: false,
displayModalHistory: false,
isDataSubmit: true
});
this.props.refundCreditDemandUserAction({
id_user: this.state.user.id,
id_demande: this.state.historyItemDetail.id_demande,
password: this.state.password
});
}}/>
);
}
renderNanoCreditItem = (item, index) => {
return (
{
this.setState({displayModalHistory: true, historyItemDetail: item})
}} style={[styles.contentService, {borderBottomColor: Color.primaryColor}]}>
{
Object.keys(omit(item, ['id', 'id_demande', 'agent', 'retrait_cash', 'date_remboursement_prevu', 'taxe', 'date_validation', 'duree_mois', 'interet', 'montant_rembourse', 'partiellement_rembourse',])).map((element, i) => (
{
isEqual(element, 'montant') ?
` ${item[element].length > 8 ? cutString(thousands(item[element], ' '), 6) : thousands(item[element], ' ')} ${this.state.wallet.currency_code}`
: isEqual(element, 'date_creation') ?
cutStringWithoutDot(item[element], 16)
: item[element]
}
))
}
);
}
renderNanoCreditList = () => {
const {errorNanoCreditHistory, resultNanoCreditHistory} = this.props;
if (errorNanoCreditHistory !== null) {
if (typeof errorNanoCreditHistory.data !== 'undefined') {
return (
{errorNanoCreditHistory.data.error}
)
} else {
return (
{errorNanoCreditHistory}
)
}
}
if (resultNanoCreditHistory !== null) {
if (resultNanoCreditHistory.response !== null) {
return (
Array.isArray(resultNanoCreditHistory.response) && (resultNanoCreditHistory.response.length) > 0 ?
(
<>
{
nanoCreditHistoryLabel().map((item, index) => (
{I18n.t(item.label)}
))
}
{
resultNanoCreditHistory.response.map((item, index) => (
this.renderNanoCreditItem(item, index)
))
}
>
) :
(
{I18n.t('NO_NANO_CREDIT')}
)
)
}
}
}
renderNanoCreditHistory = () => {
return (
{
this.props.loadingNanoCreditHistory ?
(
{Platform.OS === 'android'
?
(
<>
{I18n.t('LOADING_DOTS')}
>
) :
<>
{I18n.t('LOADING_DOTS')}
>
}
) : this.renderNanoCreditList()
}
);
}
render() {
return (
<>
{(this.props.loading || this.props.loadingGetCommission) && this.renderLoader()}
{this.state.isDataSubmit && this.renderRefundNanoCreditResponse()}
{this.state.displayModalHistory && this.renderModalHistoryDetail()}
{this.state.displayPasswordModal && this.renderPasswordDialog()}
{I18n.t('NANO_CREDIT_LIST')}
{
this.renderNanoCreditHistory()
}
{/* {I18n.t('DEMAND_INFO')}
{
this.idDemandAnim = comp
}}>
{
this.setState({idDemand})
}}
style={styles.input}
>
{
this.passwordAnim = comp
}}>
{
this.setState({password})
}}
style={styles.input}
>
*/}
>
)
}
}
const maptStateToProps = state => ({
loading: state.refundCreditDemandReducer.loading,
result: state.refundCreditDemandReducer.result,
error: state.refundCreditDemandReducer.error,
loadingNanoCreditHistory: state.getNanoCreditUserHistoryInProgressReducer.loading,
resultNanoCreditHistory: state.getNanoCreditUserHistoryInProgressReducer.result,
errorNanoCreditHistory: state.getNanoCreditUserHistoryInProgressReducer.error,
});
const mapDispatchToProps = dispatch => bindActionCreators({
refundCreditDemandUserAction,
refundCreditDemandUserReset,
getNanoCreditUserHistoryInProgressAction,
getNanoCreditUserHistoryInProgressReset
}, dispatch);
export default connect(maptStateToProps, mapDispatchToProps)(RefundNanoCreditUser);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Color.containerBackgroundColor,
paddingHorizontal: 20
},
textbtnvalide: {
color: 'white',
fontWeight: 'bold'
},
bigtitle: {
color: 'white',
fontSize: 20,
flex: 1,
fontWeight: 'bold',
textAlign: 'center',
margin: 20,
},
blockView: {
paddingVertical: 10,
borderBottomWidth: 1
},
subbigtitle: {
color: 'white',
fontSize: 17,
textAlign: 'center',
margin: 5,
},
btnvalide: {
marginTop: 20,
marginLeft: 20,
marginRight: 20,
borderColor: 'transparent',
backgroundColor: Color.accentLightColor,
height: 52
},
btnSubmit: {
marginTop: 20,
borderColor: 'transparent',
backgroundColor: Color.accentLightColor,
height: 52,
width: "30%",
marginLeft: 20,
marginRight: 20,
},
input: {
height: 60,
marginTop: responsiveHeight(2),
marginLeft: responsiveWidth(5),
marginRight: responsiveWidth(5),
borderRadius: 5,
},
circlePoint: {
width: 50,
height: 50,
borderRadius: 25,
marginRight: 5,
alignItems: 'center',
justifyContent: 'center',
},
checkDefault: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
borderBottomWidth: 1,
paddingVertical: 10,
marginTop: 5
},
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(65),
paddingHorizontal: 10,
justifyContent: 'space-between',
alignItems: 'flex-start',
flex: 1,
},
contentTitle: {
paddingTop: 12,
},
contentService: {
paddingVertical: 10,
borderBottomWidth: 0.5,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-between',
},
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"
},
});