Browse Source

Display error toast from failed actions from CfdTable

This code concerns both maker and taker UIs.
fix/sql-oddness
Mariusz Klochowicz 3 years ago
parent
commit
89f28fca7f
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 7
      frontend/src/TakerApp.tsx
  2. 18
      frontend/src/components/cfdtables/CfdTable.tsx

7
frontend/src/TakerApp.tsx

@ -21,6 +21,7 @@ import { useEventSource } from "react-sse-hooks";
import { CfdTable } from "./components/cfdtables/CfdTable"; import { CfdTable } from "./components/cfdtables/CfdTable";
import CurrencyInputField from "./components/CurrencyInputField"; import CurrencyInputField from "./components/CurrencyInputField";
import useLatestEvent from "./components/Hooks"; import useLatestEvent from "./components/Hooks";
import { HttpError } from "./components/HttpError";
import { Cfd, intoCfd, intoOrder, Order, StateGroupKey, WalletInfo } from "./components/Types"; import { Cfd, intoCfd, intoOrder, Order, StateGroupKey, WalletInfo } from "./components/Types";
import Wallet from "./components/Wallet"; import Wallet from "./components/Wallet";
@ -43,7 +44,8 @@ async function postCfdOrderRequest(payload: CfdOrderRequestPayload) {
let res = await fetch(`/api/cfd/order`, { method: "POST", body: JSON.stringify(payload) }); let res = await fetch(`/api/cfd/order`, { method: "POST", body: JSON.stringify(payload) });
if (!res.status.toString().startsWith("2")) { if (!res.status.toString().startsWith("2")) {
throw new Error("failed to create new CFD order request: " + res.status + ", " + res.statusText); const resp = await res.json();
throw new HttpError(resp);
} }
} }
@ -51,7 +53,8 @@ async function getMargin(payload: MarginRequestPayload): Promise<MarginResponse>
let res = await fetch(`/api/calculate/margin`, { method: "POST", body: JSON.stringify(payload) }); let res = await fetch(`/api/calculate/margin`, { method: "POST", body: JSON.stringify(payload) });
if (!res.status.toString().startsWith("2")) { if (!res.status.toString().startsWith("2")) {
throw new Error("failed to create new CFD order request: " + res.status + ", " + res.statusText); const resp = await res.json();
throw new HttpError(resp);
} }
return res.json(); return res.json();

18
frontend/src/components/cfdtables/CfdTable.tsx

@ -31,6 +31,8 @@ import {
import React from "react"; import React from "react";
import { useAsync } from "react-async"; import { useAsync } from "react-async";
import { Column, Row, useExpanded, useSortBy, useTable } from "react-table"; import { Column, Row, useExpanded, useSortBy, useTable } from "react-table";
import createErrorToast from "../ErrorToast";
import { HttpError } from "../HttpError";
import Timestamp from "../Timestamp"; import Timestamp from "../Timestamp";
import { Action, Cfd } from "../Types"; import { Action, Cfd } from "../Types";
@ -48,15 +50,7 @@ export function CfdTable(
try { try {
await doPostAction(orderId, action); await doPostAction(orderId, action);
} catch (e) { } catch (e) {
const description = typeof e === "string" ? e : JSON.stringify(e); createErrorToast(toast, e);
toast({
title: "Error",
description,
status: "error",
duration: 9000,
isClosable: true,
});
} }
}, },
}); });
@ -400,8 +394,12 @@ export function Table({ columns, tableData, hiddenColumns, renderDetails }: Tabl
} }
async function doPostAction(id: string, action: string) { async function doPostAction(id: string, action: string) {
await fetch( let res = await fetch(
`/api/cfd/${id}/${action}`, `/api/cfd/${id}/${action}`,
{ method: "POST", credentials: "include" }, { method: "POST", credentials: "include" },
); );
if (!res.status.toString().startsWith("2")) {
const resp = await res.json();
throw new HttpError(resp);
}
} }

Loading…
Cancel
Save