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

Commit

Permalink
feat: support for optional memo input field (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespierog committed Mar 22, 2022
1 parent 88ada8d commit 04dc90e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
17 changes: 12 additions & 5 deletions components/generate-invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ type LnInvoiceObject = {
}

const LN_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql`
mutation lnInvoiceCreateOnBehalfOfRecipient($walletId: WalletId!, $amount: SatAmount!) {
mutation lnInvoiceCreateOnBehalfOfRecipient(
$walletId: WalletId!
$amount: SatAmount!
$memo: Memo
) {
mutationData: lnInvoiceCreateOnBehalfOfRecipient(
input: { recipientWalletId: $walletId, amount: $amount }
input: { recipientWalletId: $walletId, amount: $amount, memo: $memo }
) {
errors {
message
Expand All @@ -30,9 +34,10 @@ const LN_USD_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql`
mutation lnUsdInvoiceCreateOnBehalfOfRecipient(
$walletId: WalletId!
$amount: CentAmount!
$memo: memo
) {
mutationData: lnUsdInvoiceCreateOnBehalfOfRecipient(
input: { recipientWalletId: $walletId, amount: $amount }
input: { recipientWalletId: $walletId, amount: $amount, memo: $memo }
) {
errors {
message
Expand All @@ -53,12 +58,14 @@ function GenerateInvoice({
amountInBase,
regenerate,
currency,
memo,
}: {
recipientWalletId: string
recipientWalletCurrency: string
amountInBase: number
regenerate: () => void
currency: string
memo: string
}) {
const [invoiceStatus, setInvoiceStatus] = useState<
"loading" | "new" | "need-update" | "expired"
Expand All @@ -85,7 +92,7 @@ function GenerateInvoice({
}
useEffect(() => {
createInvoice({
variables: { walletId: recipientWalletId, amount: amountInBase },
variables: { walletId: recipientWalletId, amount: amountInBase, memo: memo },
})
if (currency !== "SATS" || recipientWalletCurrency === "USD") {
timerIds.current.push(
Expand All @@ -99,7 +106,7 @@ function GenerateInvoice({
window.setTimeout(() => setInvoiceStatus("expired"), INVOICE_EXPIRE_INTERVAL),
)
return clearAllTimers
}, [recipientWalletId, amountInBase, currency, createInvoice])
}, [recipientWalletId, amountInBase, currency, createInvoice, memo])

let errorString: string | null = error?.message || null
let invoice
Expand Down
4 changes: 4 additions & 0 deletions components/receive-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ const satsFormatter = new Intl.NumberFormat("en-US", {
export default function ReceiveAmount({
recipientWalletId,
recipientWalletCurrency,
memo,
}: {
recipientWalletId: string
recipientWalletCurrency: string
memo: string
}) {
const router = useRouter()
const { satsToUsd, usdToSats } = useSatPrice()
Expand Down Expand Up @@ -78,6 +80,7 @@ export default function ReceiveAmount({
recipientWalletCurrency === "USD"
? triggerRegenerateUsdInvoice
: triggerRegenerateBtcInvoice

return (
<>
<div className="amount-input">
Expand All @@ -102,6 +105,7 @@ export default function ReceiveAmount({
amountInBase={amountInBase}
regenerate={triggerRegenerateInvoice}
currency={currency}
memo={memo}
/>
)}
</>
Expand Down
10 changes: 6 additions & 4 deletions components/receive-no-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type LnInvoiceObject = {
}

const LN_NOAMOUNT_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql`
mutation lnNoAmountInvoiceCreateOnBehalfOfRecipient($walletId: WalletId!) {
mutation lnNoAmountInvoiceCreateOnBehalfOfRecipient($memo: Memo, $walletId: WalletId!) {
mutationData: lnNoAmountInvoiceCreateOnBehalfOfRecipient(
input: { recipientWalletId: $walletId }
input: { memo: $memo, recipientWalletId: $walletId }
) {
errors {
message
Expand All @@ -29,9 +29,11 @@ const LN_NOAMOUNT_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql`
export default function ReceiveNoAmount({
recipientWalletId,
onSetAmountClick,
memo,
}: {
recipientWalletId: string
onSetAmountClick: () => void
memo: string
}) {
const [createInvoice, { loading, error, data }] = useMutation<{
mutationData: {
Expand All @@ -42,9 +44,9 @@ export default function ReceiveNoAmount({

useEffect(() => {
createInvoice({
variables: { walletId: recipientWalletId },
variables: { memo: memo, walletId: recipientWalletId },
})
}, [createInvoice, recipientWalletId])
}, [createInvoice, recipientWalletId, memo])

if (error) {
return <div className="error">{error.message}</div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"@types/prettier": "^2.4.1",
"@types/react-dev-utils": "^9.0.7",
"@types/react-lottie": "^1.2.6",
"prettier": "^2.4.1"
"prettier": "2.4.1"
}
}
19 changes: 19 additions & 0 deletions pages/[username].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Card from "react-bootstrap/Card"
import Container from "react-bootstrap/Container"
import Image from "react-bootstrap/Image"
import { gql, useQuery } from "@apollo/client"
import { useState } from "react"
import { useDebouncedCallback } from "use-debounce"

import ReceiveAmount from "../components/receive-amount"
import ReceiveNoAmount from "../components/receive-no-amount"
Expand All @@ -24,6 +26,11 @@ export default function Receive() {
const router = useRouter()
const { username, amount } = router.query

const [memo, setMemo] = useState("")
const debouncedMemo = useDebouncedCallback((memo) => {
setMemo(memo)
}, 1000)

const { error, loading, data } = useQuery(RECIPIENT_WALLET_ID, {
variables: {
username,
Expand Down Expand Up @@ -53,15 +60,27 @@ export default function Receive() {
<Card className="text-center">
<Card.Header>Pay {username}</Card.Header>

<div className="memo-container">
<input
className="memo"
type="text"
maxLength={159}
placeholder="Optional memo"
onChange={(e) => debouncedMemo(e.target.value)}
/>
</div>

{isAmountInvoice ? (
<ReceiveAmount
recipientWalletId={recipientWalletId}
recipientWalletCurrency={recipientWalletCurrency}
memo={memo}
/>
) : (
<ReceiveNoAmount
recipientWalletId={recipientWalletId}
onSetAmountClick={onSetAmountClick}
memo={memo}
/>
)}

Expand Down
12 changes: 12 additions & 0 deletions pages/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ code {
.invoice-container {
max-width: 390px;
}

.memo-container {
padding: 0.5rem 0rem;
display: flex;
align-items: center;
justify-content: center;
}

.memo {
flex-grow: 2;
max-width: 300px;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3510,10 +3510,10 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

prettier@^2.4.1:
version "2.5.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.0.tgz#a6370e2d4594e093270419d9cc47f7670488f893"
integrity sha512-FM/zAKgWTxj40rH03VxzIPdXmj39SwSjwG0heUcNFwI+EMZJnY93yAiKXM3dObIKAM5TA88werc8T/EwhB45eg==
[email protected]:
version "2.4.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.1.tgz#671e11c89c14a4cfc876ce564106c4a6726c9f5c"
integrity sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA==

[email protected]:
version "0.11.10"
Expand Down

0 comments on commit 04dc90e

Please sign in to comment.