import gql from "graphql-tag"; import { Query } from "react-apollo"; import * as React from "react"; import { Link } from "react-router-dom"; import { formatRoute } from "react-router-named-routes"; import { CivilContext, ICivilContext, LoadingMessage, ErrorLoadingData, ViewTransactionLink, QuestionToolTip, } from "@joincivil/components"; import { AccountTransactionsTable, NoWrapTd } from "./AccountStyledComponents"; import { TransactionsTitleText } from "./AccountTextComponents"; import { routes } from "../../../constants"; import { UserManagementSection } from "../UserManagement"; export const paymentHistoryQuery = gql` query { currentUser { userChannel { paymentsMadeByChannel { __typename status reaction comment currencyCode exchangeRate amount createdAt updatedAt usdEquivalent ... on PaymentEther { transactionID } post { id postType channel { handle newsroom { name contractAddress } } ... on PostBoost { title } ... on PostExternalLink { openGraphData { title } } } } } } } `; interface PostData { id: string; postType: string; channel: { handle?: string; newsroom: { name: string; contractAddress: string; }; }; title?: string; openGraphData?: { title: string; }; } interface TransactionData { __typename: string; id: string; status?: string; currencyCode: string; exchangeRate: number; amount: number; createdAt: string; usdEquivalent: number; transactionID?: string; post: PostData; } interface PaymentHistoryData { currentUser: { userChannel: { paymentsMadeByChannel?: TransactionData[]; }; }; } const SUPPORTED_PAYMENT_TYPES = ["PaymentStripe", "PaymentEther"]; const SUPPORTED_POST_TYPES = ["externallink", "boost"]; export const AccountTransactions: React.FunctionComponent = () => { const context = React.useContext(CivilContext); if (context.auth.loading) { return Loading User; } else if (!context.currentUser) { return <>Please log in to view your transaction history; } return ( }> query={paymentHistoryQuery}> {({ loading, data, error }) => { if (loading) { return Loading Transactions; } else if (error || !data) { console.error("error querying currentUser for paymentsMadeByChannel:", error || "no data returned"); return ; } else if (!data.currentUser.userChannel.paymentsMadeByChannel) { return (

You have made no transactions yet. Head on over to the{" "} Civil story feed to find great work that deserves your support!

); } const transactions = data.currentUser.userChannel.paymentsMadeByChannel .filter( transaction => SUPPORTED_PAYMENT_TYPES.indexOf(transaction.__typename) !== -1 && SUPPORTED_POST_TYPES.indexOf(transaction.post.postType) !== -1, ) .sort((tA, tB) => new Date(tB.createdAt).getTime() - new Date(tA.createdAt).getTime()); return ( Date Newsroom Title Method Status Amount {transactions.map(transaction => ( ))} ); }}
); }; const TransactionRow: React.FunctionComponent = props => { const context = React.useContext(CivilContext); const date = new Date(props.createdAt); return ( {props.post.channel.newsroom.name} {props.post.title || props.post.openGraphData!.title} {props.__typename === "PaymentEther" ? "ETH" : "Card"} {props.__typename === "PaymentEther" ? ( ) : ( "complete" )} ${props.usdEquivalent.toFixed(2)} {props.currencyCode !== "usd" && ( )} ); };