import * as React from 'react'; import { TouchableOpacity, View, Text, Image, Alert, Platform } from 'react-native'; import { savePlainUserInfo } from "../../redux/actions/AppUserInfoActions"; import { openModal } from '../../redux/actions/AlertActions'; import { connect } from "react-redux"; import NetInfo from "../../components/netstatus"; import CustomHeader from '../../components/header.js'; import Assets from '../../components/assets.manager.js'; import Elements from '../../components/elements.js'; import Theme from '../../components/theme.style.js'; import DB from '../../components/storage/'; import REQUEST from '../../components/api/'; import CustomInput from '../../components/custominput'; import CustomSafeArea from '../../components/safeArea.component'; class AddAccountCard extends React.PureComponent { constructor(props) { super(props) } state = { profile: {}, loading: false, cardnumber: "", valid: false, } componentDidMount() { this.init() } componentWillUnmount() { } init = () => { this.setState({ profile: this.props.route.params.data }) } setCardNumber = (cn) => { this.setState({ valid: cn.length == 16 ? true : false }) } submitAddAccountCard = async () => { let accounts = await DB.accounts(); let included = accounts.find((account) => { return account.data.card_number === this.state.cardnumber }) if(included != undefined && included != null){ Platform.OS == 'ios' ? setTimeout(() => { this.props.openModal({ open: true, title: "Information", body: "Account has already been added previously in your profile. Would you like to switch to that account now?", yesCB: async () => { let process = await DB.switchaccount(0, this.state.cardnumber) if(process == true){ this.props.savePlainUserInfo(included).then(res => { // DB.updateProfile(res, function(){}, function(error){}) }) this.props.openModal({ open: true, title: "Account", body: "Successfully switched to account", yesButtonOnly: true, yesText: "Okay", theme: this.props.app_theme }) setTimeout(() => { this.props.navigation.reset({ index: 0, routes: [{name: 'Main'}], }); }, 700); } else { Alert.alert("Information", '\n' + JSON.stringify(process)) } }, theme: this.props.app_theme }) }, 300) : Alert.alert("Information", '\n' + res.message) } else { this.setState({ loading: true }) this.validateCard(success => { if(success.status == 1) { let sessiondata = { card_number: success.data.card_number, lcard_uuid: success.data.user_id, mobile_number: success.data.number } this.requestOTP(sessiondata.lcard_uuid, sessiondata.mobile_number, success => { this.setState({ loading: false }) if(success.status == 1) { this.props.navigation.navigate("AccountOtp", { data: sessiondata, callback: this.props.route.params?.callback, type: this.props.route.params?.type }) } }, err => { Alert.alert("Information", `\n${err.message}`); this.setState({ loading: false }) }) } else { this.setState({ loading: false }) Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("Information", '\n' + success.message) }, 300) : Alert.alert("Information", '\n' + success.message) } }, err => { Alert.alert("Information", `\n${err.message}`); this.setState({ loading: false }) }) } } validateCard = async (successCallback, errorCallback) => { let uuid = await DB.get("deviceUUID") await REQUEST("card_validation", "post", {}, {}, { card_number: this.state.cardnumber, deviceUUID: uuid }, function(res){ successCallback(res) }, function(error){ errorCallback(error) }, "CardValidationEnroll", "Submit") } onSubmit = () => { NetInfo.netstatus(isConnected => { if(isConnected){ if(this.state.valid) this.submitAddAccountCard() }else{ Elements.nointernet2(this.props) } }) } requestOTP = async (lcard_uuid, mobile_number, successCallback, errorCallback) => { this.setState({ loading: true }) let params = `lcard_uuid=${lcard_uuid}&mobile=${Theme.formatter.PMBL(mobile_number)}&is_resend=false`; await REQUEST("requestOTP", "get", {}, params, {}, function(res){ successCallback(res) }, function(error){ errorCallback(error) }, "OTP", "Request" ) } render() { return ( {/* { setOpenModal({ open: false, track: undefined }) }} onClose={() => setOpenModal({ open: false, track: undefined })} /> */} this.props.navigation.goBack()} back={true} /> Add Card Enter your 16-digit card number. { this.setState({ cardnumber: val, valid: val.length == 16 ? true : false }) }} /> this.onSubmit()} disabled={!this.state.valid} style={{padding: 20, paddingTop: 15, width: Theme.screen.w - 80, paddingBottom: 15, borderRadius: 10, backgroundColor: this.state.valid ? Theme.colors.primary : this.props.app_theme?.theme.dark ? this.props.app_theme?.theme.colors.border : Theme.colors.primary + "15"}}> Submit or this.props.navigation.navigate("AccountEnrollActivate", {callback: this.props.route.params.callback, type: "activatecard"}) } style={{padding: 15, paddingTop: 12, width: Theme.screen.w - 80, paddingBottom: 12, borderRadius: 10, borderWidth: 2, borderColor: Theme.colors.primary}}> Activate your Card ) } } const mapStateToProps = (state) => { return { userinfo: state.appUserInfoReducer.userinfo, app_theme: state.appThemeReducer.theme } } const mapDispatchToProps = { savePlainUserInfo, openModal } export default connect(mapStateToProps, mapDispatchToProps)(AddAccountCard);