import * as React from "react"; import { CivilContext, ICivilContext, Modal, LoadingMessage } from "@joincivil/components"; import { InvertedButton, buttonSizes, BorderlessButton } from "@joincivil/elements"; import { AccountPaymentSection, AccountPaymentSectionHeader, AccountPaymentTable, AccountPaymentWallet, AccountPaymentWalletAddress, AccountPaymentWalletBalance, } from "./AccountStyledComponents"; import { PaymentTitleText } from "./AccountTextComponents"; import { UserManagementSection } from "../UserManagement"; import { Mutation, MutationFunc, ApolloConsumer } from "react-apollo"; import gql from "graphql-tag"; import { getCurrentUserQuery } from "@joincivil/utils"; import AccountAddCard from "./AccountAddCard"; import { StripeProvider, Elements } from "react-stripe-elements"; import makeAsyncScriptLoader from "react-async-script"; export interface AccountPaymentsState { balance: any; showAddCardModal: boolean; stripeLoaded: boolean; } const removeCardMutation = gql` mutation($paymentMethodID: String!, $channelID: String!) { paymentsRemoveSavedPaymentMethod(paymentMethodID: $paymentMethodID, channelID: $channelID) } `; export class AccountPayments extends React.Component<{}, AccountPaymentsState> { public static contextType = CivilContext; public context!: ICivilContext; constructor(props: any) { super(props); this.state = { balance: 0, showAddCardModal: false, stripeLoaded: false }; } public async componentDidMount(): Promise { const civil = this.context.civil; if (civil) { await civil.currentProviderEnable(); const account = await civil.accountStream.first().toPromise(); if (account) { await this.setState({ balance: await civil.accountBalance(account), }); } } } public render(): JSX.Element { const currentUser = this.context.currentUser; const userChannel = currentUser && currentUser.userChannel; let paymentMethods; let channelID = ""; let email = ""; if (userChannel) { paymentMethods = userChannel.stripeCustomerInfo && userChannel.stripeCustomerInfo.paymentMethods; channelID = userChannel.id; email = userChannel.EmailAddressRestricted; } const AsyncScriptLoader = makeAsyncScriptLoader("https://js.stripe.com/v3/")(LoadingMessage); if (!currentUser) { return <>Log in to view your Account; } if (!this.state.stripeLoaded) { return ( { this.setState({ stripeLoaded: true }); }} /> ); } return ( }>

Credit and debit cards

Card Name Exp. Date {paymentMethods && paymentMethods.map((pm: any) => { return ( {pm.brand + " ending in " + pm.last4Digits} {pm.name} {pm.expMonth + "/" + pm.expYear} {(removeCard: MutationFunc) => { return ( { const res = (await removeCard({ variables: { paymentMethodID: pm.paymentMethodID, channelID, }, refetchQueries: [ { query: getCurrentUserQuery, }, ], })) as any; if (res.data && res.data.paymentsRemoveSavedPaymentMethod) { await this.context.auth.handleInitialState(); } }} > Remove ); }} ); })} this.setState({ showAddCardModal: true })}> Add card {this.state.showAddCardModal && ( {client => ( { this.setState({ showAddCardModal: false }); }} handleAdded={async () => { await this.context.auth.handleInitialState(); this.setState({ showAddCardModal: false }); }} apolloClient={client} /> )} )}

Wallets

{currentUser.ethAddress} {this.state.balance}
); } }