import React, { useState } from "react"; import type { FC } from "react"; import { graphql } from "babel-plugin-relay/macro"; import { useLazyLoadQuery } from "react-relay"; import { BigNumber } from "bignumber.js"; import cx from "classnames"; import { useCurrentUser } from "../../../contexts/UserProvider"; import { Unauthenticated } from "../../../messages/Unauthenticated"; import type { ExchangeQuery } from "./__generated__/ExchangeQuery.graphql"; const tabBaseStyles = "w-full text-base font-bold text-black px-4 py-2 focus:ring-blue-500"; const selectedTabStyles = "bg-blue-600 hover:bg-blue-700 rounded-l-frounded-full text-white"; const inputBaseStyles = "rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent mb-3"; export const Exchange: FC = () => { const { isAuthenticated } = useCurrentUser(); const [exchangeOption, setExchangeOption] = useState<"BUY" | "SELL">("BUY"); const [cryptoDock, setCryptoDock] = useState("0"); const [fiatDock, setFiatDock] = useState("0.00"); const { balances, fiatBalances } = useLazyLoadQuery( graphql` query ExchangeQuery { fiatBalances { edges { node { amountCents } } } balances { edges { node { amount } } } } `, {} ); if (!isAuthenticated) return ; const [crypto] = balances.edges; const [fiat] = fiatBalances.edges; const avaliableCrypto = new BigNumber(crypto.node.amount); const avaliableFiat = ( fiat.node.amountCents ? fiat.node.amountCents / 100 : 0 ).toFixed(2); const handleSellTabClick = () => { setExchangeOption("SELL"); setCryptoDock("0"); }; const handleBuyTabClick = () => { setExchangeOption("BUY"); setCryptoDock("0"); }; const handleCryptoAmountChange = ({ currentTarget: { value }, }: React.ChangeEvent) => { const newCryptoAmount = new BigNumber(value); if (newCryptoAmount.isLessThanOrEqualTo(avaliableCrypto)) { setCryptoDock(value); } }; const handleFiatAmountChange = ({ currentTarget: { value }, }: React.ChangeEvent) => { const newFiatAmount = Number(value); if (Number(avaliableFiat) >= newFiatAmount) { setFiatDock(value); } }; const handleMaxFiatDockButton = () => { setFiatDock(avaliableFiat); }; const handleMaxCryptoButton = () => { setCryptoDock(avaliableCrypto.toString()); }; return (
{exchangeOption === "SELL" ? "CAKE" : "BRL"} disponível:{" "} {exchangeOption === "SELL" ? crypto.node.amount : avaliableFiat}
{exchangeOption === "BUY" ? ( <> ) : ( <> )}
); };