Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Fix conversion bug and initial data (#456)
Browse files Browse the repository at this point in the history
* fix: initial data

* fix: sats conversion bug
  • Loading branch information
ntheile authored Apr 14, 2023
1 parent 2a249d2 commit 2197b53
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 139 deletions.
35 changes: 12 additions & 23 deletions components/ParsePOSPayment/Receive-Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Tooltip from "react-bootstrap/Tooltip"
import { QRCode } from "react-qrcode-logo"
import { useTimer } from "react-timer-hook"
import { useScreenshot } from "use-react-screenshot"
import { AmountUnit } from "."

import { URL_HOST_DOMAIN, USD_INVOICE_EXPIRE_INTERVAL } from "../../config/config"
import useCreateInvoice from "../../hooks/use-Create-Invoice"
Expand All @@ -20,6 +19,7 @@ import { ACTION_TYPE } from "../../pages/_reducer"
import PaymentOutcome from "../PaymentOutcome"
import { Share } from "../Share"
import styles from "./parse-payment.module.css"
import { safeAmount } from "../../utils/utils"

interface Props {
recipientWalletCurrency?: string
Expand Down Expand Up @@ -86,30 +86,19 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:
)

const paymentAmount = React.useMemo(() => {
const finalReturnValue =
recipientWalletCurrency === "USD" ? Number(amount) * 100 : usdToSats(Number(amount))
if (amount === "0.00" || isNaN(Number(amount))) {
if ((!unit || !amount || !sats) && recipientWalletCurrency === "USD") {
return (Number(state.currentAmount) * 100).toString()
}
if (sats && unit === AmountUnit.Sat) {
if (recipientWalletCurrency === "USD") {
return satsToUsd(Number(state.currentAmount)).toString()
}
return sats
} else if (Number(state.currentAmount) > 0) {
return usdToSats(Number(state.currentAmount)).toFixed(2)
}
if (!router.query.sats || typeof router.query.sats !== "string") {
alert("No sats amount provided")
return
}

if (sats && unit === AmountUnit.Sat) {
if (recipientWalletCurrency === "USD") {
return (Number(amount) * 100).toString()
}
return sats
let amt = safeAmount(router.query.sats)
if (recipientWalletCurrency === "USD") {
const usdAmount = satsToUsd(Number(amt))
if (isNaN(usdAmount)) return
const cents = parseFloat(usdAmount.toFixed(2)) * 100
amt = Number(cents.toFixed())
}

return finalReturnValue.toFixed(2)
if (amt === null) return
return safeAmount(amt).toString()
}, [
amount,
unit,
Expand Down
14 changes: 11 additions & 3 deletions components/ParsePOSPayment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const defaultCurrencyMetadata: Currency = {

function ParsePayment({ defaultWalletCurrency, walletId, dispatch, state }: Props) {
const router = useRouter()
const { username, amount, sats, unit, memo } = router.query
const { username, amount, sats, unit, memo, currency } = router.query
const { display } = parseDisplayCurrency(router.query)
const { currencyToSats, satsToCurrency, hasLoaded } = useRealtimePrice(display)
const { currencyList } = useDisplayCurrency()
Expand All @@ -66,6 +66,14 @@ function ParsePayment({ defaultWalletCurrency, walletId, dispatch, state }: Prop

const prevUnit = React.useRef(AmountUnit.Cent)

if (!currency) {
const queryString = window.location.search
const searchParams = new URLSearchParams(queryString)
searchParams.set("currency", defaultWalletCurrency ?? "BTC")
const newQueryString = searchParams.toString()
window.history.pushState(null, "", "?" + newQueryString)
}

// onload
// set all query params on first load, even if they are not passed
useEffect(() => {
Expand Down Expand Up @@ -198,7 +206,7 @@ function ParsePayment({ defaultWalletCurrency, walletId, dispatch, state }: Prop
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(handleAmountChange, [currentAmount, hasLoaded])
React.useEffect(handleAmountChange, [currentAmount, hasLoaded.current])

React.useEffect(() => {
setCurrentAmount(state.currentAmount)
Expand Down Expand Up @@ -296,7 +304,7 @@ function ParsePayment({ defaultWalletCurrency, walletId, dispatch, state }: Prop
}`}
>
{unit === "CENT" ? "≈" : ""} {formatOperand(valueInSats.toString())} sat
{!hasLoaded && (
{!hasLoaded.current && (
<span
style={{
fontSize: "1rem",
Expand Down
Loading

0 comments on commit 2197b53

Please sign in to comment.