import * as React from 'react'; import { connect } from "react-redux"; import { SafeAreaView, TouchableOpacity, View, Text, FlatList, Image, StyleSheet, Alert, Modal } from 'react-native'; import { returnIcon } from '../../utils/card'; import { AdjustableText } from '../../components/text'; import REQUEST from '../../components/api'; import Icon from '../../components/icons.js'; import CustomHeader from '../../components/header.js'; import Theme from '../../components/theme.style.js'; import Assets from '../../components/assets.manager.js'; import Elements from '../../components/elements.js'; import DB from '../../components/storage/'; import CustomSafeArea from '../../components/safeArea.component'; import Tokenization from '../../components/paymaya/tokenization'; import { ENV } from '../../components/environment'; class TopupPaymentList extends React.Component { constructor(props) { super(props) } state = { cards: [], defaultCardIndex: -1, defaultSelectedCard: null, showModal: false, showCardAdded: false, showCardDeleted: false, deleteCard: null, loading: false, merchant: {}, newlyAddedCard: null, isShowSelectionCard: this.props.route.params?.displaySelectionCard } componentDidMount() { this.getSelectedCard(); this.getFunding(); } getFunding = async () => { this.setState({ loading: true, cards: [] }) const USER_PROFILE = await DB.profile(); const SESSION = await DB.session(); await REQUEST("paymaya_tokens", "get", {'Authorization': SESSION.token}, {noID: true, value: USER_PROFILE.data.card_number}, {}, async (res) => { if(res.data && res.data.cards && res.data.cards.length > 0) { const getDefault = res.data.cards.find(card => card.default); await DB.set( "topupPaymentCards", JSON.stringify(getDefault), () => {}, () => {} ) this.props.route?.params.onSelectedPaymentCard && this.props.route?.params.onSelectedPaymentCard(getDefault); this.setState({cards: this.filterData(res.data.cards), merchant: res.data, defaultSelectedCard: getDefault}) } else if(res.data && !res.data.cards) { await DB.set( "topupPaymentCards", JSON.stringify(null), () => {}, () => {} ) this.props.route?.params.onSelectedPaymentCard && this.props.route?.params.onSelectedPaymentCard(null); this.setState({cards: [], merchant: {}, defaultSelectedCard: null}) } this.setState({ loading: false }) }, (error) => { this.onError(error); }, "Funding", "Fetch") } onError = (err) => { if(err.toJSON().message === 'Network Error') { Elements.nointernet2() } else { Alert.alert("Information", `\n${err.message}`); } this.setState({ loading: false }) } filterData = (data) => { const defaultcard = data.find(card => card.default); const cards = data.filter(card => !card.default); cards.unshift(defaultcard); return cards; } getSelectedCard = async () => { const card = await DB.get("topupPaymentCards"); if(card) { this.setState({ defaultSelectedCard: JSON.parse(card) }); } } deleteCard = async () => { this.setState({ loading: true, showModal: false }) const SESSION = await DB.session() const { uuid } = this.state.deleteCard; await REQUEST("paymaya_tokens/" + uuid, "delete", {'Authorization': SESSION.token}, {}, {}, async (res) => { if(res.status === 1){ this.setState({ loading: false, showCardDeleted: true, deleteCard: null }) } else { this.setState({ loading: false }) } }, (err) => { this.onError(err); }, "Card", "Delete") } submitPaymentCard = async (defaultSelectedCard) => { this.setState({ loading: true }) const SESSION = await DB.session() await REQUEST("paymaya_tokens/" + defaultSelectedCard.uuid, "patch", {'Authorization': SESSION.token}, {}, {}, async (res) => { if(res.status === 1) { if(defaultSelectedCard == null) { return } await DB.set( "topupPaymentCards", JSON.stringify(defaultSelectedCard), () => { this.props.route?.params.onSelectedPaymentCard && this.props.route?.params.onSelectedPaymentCard(defaultSelectedCard) this.props.navigation.goBack(); }, () => {} ) } this.setState({ loading: false }) }, (err) => { this.onError(err); }, "Card", "Add") } returnCardName = (card) => { let newCard = card.cardType; if(newCard.includes("-")) { newCard = newCard.split("-")[0]; } return newCard.toUpperCase(); } onAddNewCard = async (response) => { const { paymaya_token_uuid } = response; this.setState({showCardAdded: true, newlyAddedCard: paymaya_token_uuid}); this.getFunding() } renderDeleteErrorModal = () => { return ( {}} style={styles.centeredView}> Confirmation Are you sure you want to delete this card? this.setState({ showModal: false })} style={{ flex: 1, alignItems: 'center', justifyContent: 'center', paddingVertical: 7, marginRight: 6, borderWidth: 1, borderColor: Theme.colors.primary, borderRadius: 5 }}> No this.deleteCard()} style={{ flex: 1, alignItems: 'center', justifyContent: 'center', marginLeft: 6, paddingVertical: 7, borderWidth: 1, borderColor: Theme.colors.primary, backgroundColor: Theme.colors.primary, borderRadius: 5 }}> Yes ) } renderCardAdded = () => { if(!this.state.newlyAddedCard) return null let card = this.state?.cards.find(item => item?.uuid === this.state.newlyAddedCard); if(card && this.state.showCardAdded && !this.state.loading) { return ( {this.returnCardName(card)} ending in {card?.last4} has been{'\n'}successfully added! {this.setState({ showCardAdded: false, newlyAddedCard: null })}} style={{paddingVertical: 10, borderRadius: 5, marginTop: 10, width: 120, backgroundColor: Theme.colors.primary, alignItems: 'center'}}> OK ) } return null } renderCardDeleted = () => { if(this.state.showCardDeleted) { return ( Deleted Successfully Your card has been successfully{'\n'}deleted! { this.setState({ showCardDeleted: false }) this.getFunding(); }} style={{paddingVertical: 5, borderRadius: 5, marginTop: 10, width: 120, backgroundColor: Theme.colors.primary, alignItems: 'center'}}> OK ) } return null; } renderCardList = () => { return ( index.toString()} data={this.state.cards} scrollEnabled={true} showsVerticalScrollIndicator={false} style={this.state.cards.length === 0 && {flex: 1}} contentContainerStyle={this.state.cards.length === 0 && {flex: 1}} ListEmptyComponent={() => ( Empty )} renderItem={({item, index}) => { if(!item) return null; let primary = item.default; return ( { this.submitPaymentCard(item); }} key={index} style={{ flex: 1, alignItems: 'center', justifyContent: 'center', flexDirection: 'row', backgroundColor: this.props.app_theme?.theme.dark ? this.props.app_theme?.theme.colors.background : Theme.colors.white, padding: 12 }}> {primary && } {`**** **** **** ${item.last4}`} {primary && Primary} this.setState({ deleteCard: item, showModal: true })} style={{ paddingVertical: 5 }}> ) }} numColumns={1} /> ) } render() { return ( this.props.navigation.goBack()} back={true} menu={false} navigation={this.props.navigation} /> {this.renderCardList()} this.props.navigation.navigate('TopupPaymentMethod', { onAddNewCard: (newcard) => this.onAddNewCard(newcard), merchant: this.state.merchant })} style={{alignSelf: 'center'}} > Add a Card {this.renderCardAdded()} {this.renderCardDeleted()} {this.renderDeleteErrorModal()} ) } } const mapStateToProps = (state) => { return { app_theme: state.appThemeReducer.theme } } export default connect(mapStateToProps, null)(TopupPaymentList) const styles = StyleSheet.create({ centeredView: { flex: 1, justifyContent: "center", backgroundColor: '#00000090', }, modalView: { margin: 25, borderRadius: 10, paddingHorizontal: 31, paddingVertical: 20, alignItems: "center", }, })