import * as React from 'react';
import DB, {session} from '../../components/storage/';
import {
View,
Text,
TouchableOpacity,
Image,
Alert,
} from 'react-native';
import {
Container,
List,
Image as NBImage
} from 'native-base';
import {
savePlainUserInfo,
saveUserInfo
} from "../../redux/actions/AppUserInfoActions";
import { connect } from "react-redux";
import { Divider } from 'react-native-elements';
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 Icon from '../../components/icons.js';
import CustomSafeArea from '../../components/safeArea.component';
class About extends React.PureComponent {
constructor(props) {
super(props)
}
state = {
connected: false,
data: null,
accounts: [], //1, 2, 3, 4
loading: false
}
componentDidMount() {
this.initData()
}
componentWillUnmount() {
}
initData = () => {
NetInfo.netstatus(isConnected => {
if (isConnected) {
this.init();
} else {
Elements.nointernet2();
}
})
}
init = async () => {
let userdata = await DB.profile()
let accounts = await DB.accounts()
console.log("ACCOUNTS ===>", userdata, accounts)
this.setState({ accounts: accounts, data: userdata.data, connected: true, session })
};
switchAccount = async (account, index) => {
Alert.alert(
'Switch Account',
'Do you want to switch account?',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'OK',
onPress: async () => {
let process = await DB.switchaccount(index, account.data.card_number)
if(process == true){
this.props.savePlainUserInfo(account).then(res => {
DB.updateProfile(res, function(){}, function(error){})
})
// Toast.show({
// text:'Successfully switched to account',
// buttonText: '',
// duration: 10000,
// });
setTimeout(() => {
this.props.navigation.reset({
index: 0,
routes: [{name: 'Main'}],
});
}, 300);
} else {
Alert.alert("Error", "Invalid switching account")
}
},
},
],
{cancelable: true},
);
}
removeAccount = (account) => {
Alert.alert(
'Remove this Account?',
'Do you want to remove this account?',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'OK',
onPress: async () => {
DB.removeAccount(account, () => {
let filteredItems = this.state.accounts.filter(function(item){
return item.data.card_number != account.data.card_number;
}).map(function(item){
return item
})
this.setState({ accounts: filteredItems })
console.log(filteredItems)
}, () => {
console.log(filteredItems)
})
},
},
],
{cancelable: true},
);
}
redirectToSetMpin = async (res) => {
if(res.data.mpin == undefined || res.data.mpin == "") {
const USER_PROFILE = await DB.profile()
const SESSION = await DB.session()
console.log(USER_PROFILE, SESSION)
let sessiondata = {
card_number: USER_PROFILE.data.card_number,
lcard_uuid: USER_PROFILE.data.lcard_uuid,
mobile_number: Theme.formatter.PMBL(USER_PROFILE.data.mobile)
}
let data = {
sessiondata: sessiondata,
token: SESSION.token,
type: "addcard"
}
this.props.navigation.navigate("Setmpin", data)
} else {
this.props.navigation.reset({
index: 0,
routes: [{name: 'Mpin'}],
});
}
}
renderAccount = () => {
if (this.state.accounts.length <= 1) return null;
return (
Switch Account
{this.state.accounts.map((account, index) => {
if (account.data.card_number == this.state.data?.card_number) return null;
return (
this.switchAccount(account, index)}>
{this.state.data ? Theme.formatter.NAME(account.data) : null}
{this.state.data ? Theme.formatter.CN(account.data.card_number) : null}
this.removeAccount(account)}>
Remove
);
})}
);
};
render() {
if (!this.state.connected) {
return (
this.init()}
/>
);
}
return (
this.props.navigation.goBack()}
menu={false}
navigation={this.props.navigation}
/>
{this.state.data ? Theme.formatter.NAME(this.state.data) : null}
{this.state.data ? Theme.formatter.CN(this.state.data.card_number) : null}
{this.renderAccount()}
{this.state.accounts.length < 5 ?
{
if(this.state.accounts.length > 5) return
this.props.navigation.navigate('AddAccountCard', {
data: this.state.data,
type: "addcard",
callback: (result) => {
this.init();
// Toast.show({
// text:
// result == 'success'
// ? 'Successfully switched to account'
// : 'Try Again',
// buttonText: '',
// duration: 5000,
// });
setTimeout(() => {
Alert.alert(
'New Card',
'New card successfully added',
[
{
text: 'OK',
onPress: async () => {
const SESSION = await DB.session()
this.setState({ loading: true })
this.props.saveUserInfo({ token: SESSION.token, card_number: SESSION.user.card_number }).then(res => {
DB.updateProfile(res, success => {
this.setState({ loading: false })
this.redirectToSetMpin(res)
}, error => {
console.log(error)
this.setState({ loading: false })
})
})
},
},
],
{cancelable: true},
);
}, 1000)
},
})
}}>
Add a Card
: null}
);
}
}
const mapStateToProps = (state) => {
return {
userinfo: state.appUserInfoReducer.userinfo,
app_theme: state.appThemeReducer.theme
}
}
const mapDispatchToProps = {
savePlainUserInfo,
saveUserInfo
}
export default connect(mapStateToProps, mapDispatchToProps)(About);