import { useQuery } from '@apollo/react-hooks' import { utils as coinUtils } from '@lamassu/coins' import { makeStyles } from '@material-ui/core/styles' import BigNumber from 'bignumber.js' import classnames from 'classnames' import { format } from 'date-fns/fp' import gql from 'graphql-tag' import QRCode from 'qrcode.react' import * as R from 'ramda' import React, { useState } from 'react' import TableLabel from 'src/components/TableLabel' import Title from 'src/components/Title' import { Tr, Td, THead, TBody, Table } from 'src/components/fake-table/Table' import Sidebar from 'src/components/layout/Sidebar' import { H3, Info1, Info2, Info3, Label1, Label3 } from 'src/components/typography' import CopyToClipboard from 'src/pages/Transactions/CopyToClipboard' import { primaryColor } from 'src/styling/variables' import styles from './Funding.styles' const useStyles = makeStyles(styles) const sizes = { big: 165, time: 140, date: 130 } const GET_FUNDING = gql` { funding { cryptoCode errorMsg fundingAddress fundingAddressUrl confirmedBalance pending fiatConfirmedBalance fiatPending fiatCode display unitScale } } ` const formatAddress = (cryptoCode = '', address = '') => coinUtils.formatCryptoAddress(cryptoCode, address).replace(/(.{4})/g, '$1 ') const sumReducer = (acc, value) => acc.plus(value) const formatNumber = it => new BigNumber(it).toFormat(2) const getConfirmedTotal = list => { return formatNumber( list .filter(it => !it.errorMsg) .map(it => new BigNumber(it.fiatConfirmedBalance)) .reduce(sumReducer, new BigNumber(0)) ) } const getPendingTotal = list => { return formatNumber( list .filter(it => !it.errorMsg) .map(it => new BigNumber(it.fiatPending)) .reduce(sumReducer, new BigNumber(0)) ) } const Funding = () => { const [selected, setSelected] = useState(null) const [viewHistory] = useState(false) const classes = useStyles() const fundingHistory = [ { cryptoAmount: 2.0, balance: 10.23, fiatValue: 1000.0, date: new Date(), performedBy: null, pending: true }, { cryptoAmount: 10.0, balance: 12.23, fiatValue: 12000.0, date: new Date(), performedBy: null }, { cryptoAmount: 5.0, balance: 5.0, fiatValue: 50000.0, date: new Date(), performedBy: null } ] const isSelected = it => { return selected && selected.cryptoCode === it.cryptoCode } const { data: fundingResponse, loading } = useQuery(GET_FUNDING) const funding = R.path(['funding'])(fundingResponse) ?? [] if (funding.length && !selected) { setSelected(funding[0]) } const itemRender = (it, active) => { const itemClass = { [classes.item]: true, [classes.inactiveItem]: !active } const wrapperClass = { [classes.itemWrapper]: true, [classes.error]: it.errorMsg } return (
{it.display}
{!it.errorMsg && ( <>
{formatNumber(it.fiatConfirmedBalance)} {it.fiatCode}
{it.confirmedBalance} {it.cryptoCode}
)}
) } const pendingTotal = getPendingTotal(funding) const signIfPositive = num => (num >= 0 ? '+' : '') return ( <>
Funding {/* */}
it.display} itemRender={itemRender} loading={loading}> {funding.length && (
Total Crypto Balance {getConfirmedTotal(funding)} {funding[0].fiatCode} ({signIfPositive(pendingTotal)} {pendingTotal} pending)
)}
{selected && !viewHistory && selected.errorMsg && (
{selected.errorMsg}
)} {selected && !viewHistory && !selected.errorMsg && (

Balance ({selected.display})

{`${selected.confirmedBalance} ${selected.cryptoCode}`} {`(${signIfPositive(selected.pending)} ${ selected.pending } pending)`}
{`= ${formatNumber(selected.fiatConfirmedBalance)} ${ selected.fiatCode }`} {`(${signIfPositive(selected.fiatPending)} ${formatNumber( selected.fiatPending )} pending)`}

Address

{formatAddress( selected.cryptoCode, selected.fundingAddress )}
Scan to send {selected.display}
)} {selected && viewHistory && (
{fundingHistory.map((it, idx) => ( ))}
Amount Entered Balance After Cash Value Date Time (h:m:s) Performed By
{it.cryptoAmount} {selected.cryptoCode} {it.balance} {selected.cryptoCode} {it.fiatValue} {selected.fiatCode} {format('yyyy-MM-dd', it.date)} {format('hh:mm:ss', it.date)} add
)}
) } export default Funding