Skip to content

Commit

Permalink
feat: intermediate step when uploading refundjson and proper error (#489
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dni committed Mar 8, 2024
1 parent 5902a08 commit ed395bd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ const dict = {
branding: "Branding",
testnet: "Testnet",
broadcasting_claim: "Broadcasting claim transaction...",
open_swap: "Open Swap",
swap_in_progress: "This swap is still in progress.",
},
de: {
language: "Deutsch",
Expand Down Expand Up @@ -347,6 +349,8 @@ const dict = {
branding: "Branding",
testnet: "Testnet",
broadcasting_claim: "Sende claim transaction...",
open_swap: "Swap öffnen",
swap_in_progress: "Dieser Swap ist noch nicht abgeschlossen.",
},
es: {
language: "Español",
Expand Down Expand Up @@ -526,6 +530,8 @@ const dict = {
branding: "Branding",
testnet: "Testnet",
broadcasting_claim: "Enviando transacción de reclamación...",
open_swap: "Abrir intercambio",
swap_in_progress: "Este intercambio aún está en curso.",
},
zh: {
language: "中文",
Expand Down Expand Up @@ -688,6 +694,8 @@ const dict = {
branding: "品牌",
testnet: "测试网",
broadcasting_claim: "正在发送索赔交易...",
open_swap: "打开交换",
swap_in_progress: "此交换仍在进行中。",
},
};

Expand Down
32 changes: 28 additions & 4 deletions src/pages/Refund.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useNavigate } from "@solidjs/router";
import log from "loglevel";
import QrScanner from "qr-scanner";
import { Show, createEffect, createSignal } from "solid-js";
import { Show, createSignal, onMount } from "solid-js";

import BlockExplorer from "../components/BlockExplorer";
import RefundButton from "../components/RefundButton";
Expand All @@ -19,6 +19,8 @@ const Refund = () => {
const { updateSwapStatus, wasmSupported, swaps, t } = useGlobalContext();
const { setTimeoutEta, setTimeoutBlockheight } = usePayContext();

const [swapFound, setSwapFound] = createSignal(null);
const [refundInvalid, setRefundInvalid] = createSignal(false);
const [refundJson, setRefundJson] = createSignal(null);
const [refundTxId, setRefundTxId] = createSignal<string>("");

Expand All @@ -28,10 +30,12 @@ const Refund = () => {
const checkRefundJsonKeys = (input: HTMLInputElement, json: any) => {
log.debug("checking refund json", json);

// Redirect to normal flow if swap is in local storage
// When the swap id is found in the local storage, there is no need for the validation,
// all relevant data is there already and we just need to show the button to redirect
const localStorageSwap = swaps().find((s: any) => s.id === json.id);
if (localStorageSwap !== undefined) {
navigate("/swap/" + json.id);
setSwapFound(json.id);
return;
}

// Compatibility with the old refund files
Expand All @@ -46,6 +50,7 @@ const Refund = () => {
if (valid) {
setRefundJson(json);
} else {
setRefundInvalid(true);
input.setCustomValidity(t("invalid_refund_file"));
}
};
Expand All @@ -55,6 +60,8 @@ const Refund = () => {
const inputFile = input.files[0];
input.setCustomValidity("");
setRefundJson(null);
setSwapFound(null);
setRefundInvalid(false);

if (inputFile.type === "image/png") {
QrScanner.scanImage(inputFile, { returnDetailedScanResult: true })
Expand All @@ -63,6 +70,7 @@ const Refund = () => {
)
.catch((e) => {
log.error("invalid QR code upload", e);
setRefundInvalid(true);
input.setCustomValidity(t("invalid_refund_file"));
});
} else {
Expand All @@ -73,6 +81,7 @@ const Refund = () => {
})
.catch((e) => {
log.error("invalid file upload", e);
setRefundInvalid(true);
input.setCustomValidity(t("invalid_refund_file"));
});
}
Expand All @@ -87,7 +96,7 @@ const Refund = () => {
setRefundableSwaps(refundableSwaps().concat(swap));
};

createEffect(() => {
onMount(() => {
const swapsToRefund = swaps()
.filter(refundSwapsSanityFilter)
.filter((swap) =>
Expand Down Expand Up @@ -143,6 +152,21 @@ const Refund = () => {
accept="application/json,image/png"
onChange={(e) => uploadChange(e)}
/>
<Show when={swapFound() !== null}>
<hr />
<p>{t("swap_in_progress")}</p>
<button
class="btn btn-success"
onClick={() => navigate(`/swap/${swapFound()}`)}>
{t("open_swap")}
</button>
</Show>
<Show when={refundInvalid()}>
<hr />
<button class="btn" disabled={true}>
{t("invalid_refund_file")}
</button>
</Show>
<Show when={refundTxId() === "" && refundJson() !== null}>
<hr />
<RefundButton
Expand Down
74 changes: 71 additions & 3 deletions tests/pages/Refund.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ describe("Refund", () => {
).not.toBeUndefined();
});

test("should not show refund button when no file was uploaded", async () => {
test("should show refund button when file was uploaded", async () => {
const user = userEvent.setup();

render(
() => (
<>
Expand Down Expand Up @@ -55,6 +54,75 @@ describe("Refund", () => {
});
await user.upload(uploadInput, swapFile);

expect(refundFrame.children.length).toEqual(8);
expect(await screen.findAllByText(i18n.en.refund)).not.toBeUndefined();
});

test("should show invalid refund button when invalid file was uploaded", async () => {
const user = userEvent.setup();
render(
() => (
<>
<TestComponent />
<Refund />
</>
),
{
wrapper: contextWrapper,
},
);
const refundFrame = (await screen.findByTestId(
"refundFrame",
)) as HTMLDivElement;
expect(refundFrame.children.length).toEqual(4);

const uploadInput = await screen.findByTestId("refundUpload");
const swapFile = new File(["{}"], "swap.json", {
type: "application/json",
});
(swapFile as any).text = async () =>
JSON.stringify({
asset: "BTC",
});
await user.upload(uploadInput, swapFile);

expect(
await screen.findAllByText(i18n.en.invalid_refund_file),
).not.toBeUndefined();
});

test("should show open swap button when swap is in `swaps()`", async () => {
const user = userEvent.setup();
render(
() => (
<>
<TestComponent />
<Refund />
</>
),
{
wrapper: contextWrapper,
},
);
const swap = {
asset: "BTC",
id: "123",
privateKey: "",
};
globalSignals.setSwaps([swap]);
const refundFrame = (await screen.findByTestId(
"refundFrame",
)) as HTMLDivElement;
expect(refundFrame.children.length).toEqual(4);

const uploadInput = await screen.findByTestId("refundUpload");
const swapFile = new File(["{}"], "swap.json", {
type: "application/json",
});
(swapFile as any).text = async () => JSON.stringify(swap);
await user.upload(uploadInput, swapFile);

expect(
await screen.findAllByText(i18n.en.open_swap),
).not.toBeUndefined();
});
});

0 comments on commit ed395bd

Please sign in to comment.