Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
fix: add proper handling of invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 5, 2024
1 parent 46f4504 commit 5d30dde
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions frontend/src/screens/wallet/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { Label } from "src/components/ui/label";
import { LoadingButton } from "src/components/ui/loading-button";
import { useToast } from "src/components/ui/use-toast";
import { useBalances } from "src/hooks/useBalances";
import { useInfo } from "src/hooks/useInfo";
import { useCSRF } from "src/hooks/useCSRF";
import { useInfo } from "src/hooks/useInfo";
import { copyToClipboard } from "src/lib/clipboard";
import { PayInvoiceResponse } from "src/types";
import { request } from "src/utils/request";
Expand All @@ -39,6 +39,20 @@ export default function Send() {
return <Loading />;
}

const handleContinue = () => {
try {
new Invoice({ pr: invoice });
setShowConfirmation(true);
} catch (error) {
toast({
variant: "destructive",
title: "Invalid Payment Request",
});
console.error(error);
setInvoice("");
}
};

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
try {
Expand All @@ -47,7 +61,7 @@ export default function Send() {
}
setLoading(true);
const payInvoiceResponse = await request<PayInvoiceResponse>(
`/api/payments/${invoice.trim()}`,
`/api/payments/${invoice}`,
{
method: "POST",
headers: {
Expand All @@ -69,6 +83,8 @@ export default function Send() {
variant: "destructive",
title: "Failed to send: " + e,
});
setInvoice("");
setShowConfirmation(false);
console.error(e);
}
setLoading(false);
Expand All @@ -81,7 +97,7 @@ export default function Send() {

const paste = async () => {
const text = await navigator.clipboard.readText();
setInvoice(text);
setInvoice(text.trim());
};

return (
Expand Down Expand Up @@ -175,7 +191,7 @@ export default function Send() {
autoFocus
placeholder="Enter an invoice"
onChange={(e) => {
setInvoice(e.target.value);
setInvoice(e.target.value.trim());
}}
/>
<Button
Expand All @@ -189,10 +205,7 @@ export default function Send() {
</div>
</div>
<div>
<Button
onClick={() => setShowConfirmation(true)}
disabled={!invoice}
>
<Button onClick={handleContinue} disabled={!invoice}>
Continue
</Button>
</div>
Expand Down

0 comments on commit 5d30dde

Please sign in to comment.