Browse Source

Merge #222

222: Prevent zero-priced CFD offers r=klochowicz a=klochowicz

Simplify the UI code and disable the button for posting a new offer if there is
no price set.

Co-authored-by: Mariusz Klochowicz <mariusz@klochowicz.com>
feature/integration-tests
bors[bot] 3 years ago
committed by GitHub
parent
commit
a62dcfb9ac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      frontend/src/MakerApp.tsx

46
frontend/src/MakerApp.tsx

@ -1,4 +1,3 @@
import { RepeatIcon } from "@chakra-ui/icons";
import { import {
Box, Box,
Button, Button,
@ -7,7 +6,7 @@ import {
Grid, Grid,
GridItem, GridItem,
HStack, HStack,
IconButton, Switch,
Tab, Tab,
TabList, TabList,
TabPanel, TabPanel,
@ -17,7 +16,7 @@ import {
useToast, useToast,
VStack, VStack,
} from "@chakra-ui/react"; } from "@chakra-ui/react";
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import { useAsync } from "react-async"; import { useAsync } from "react-async";
import { useEventSource } from "react-sse-hooks"; import { useEventSource } from "react-sse-hooks";
import { CfdTable } from "./components/cfdtables/CfdTable"; import { CfdTable } from "./components/cfdtables/CfdTable";
@ -44,7 +43,13 @@ export default function App() {
let [minQuantity, setMinQuantity] = useState<string>("10"); let [minQuantity, setMinQuantity] = useState<string>("10");
let [maxQuantity, setMaxQuantity] = useState<string>("100"); let [maxQuantity, setMaxQuantity] = useState<string>("100");
let [orderPrice, setOrderPrice] = useState<string>("0"); let [orderPrice, setOrderPrice] = useState<string>("0");
let [hasEnteredPrice, setHasEnteredPrice] = useState(false); let [autoRefresh, setAutoRefresh] = useState(true);
useEffect(() => {
if (autoRefresh && priceInfo) {
setOrderPrice((priceInfo.ask * SPREAD).toFixed(2).toString());
}
}, [priceInfo, autoRefresh]);
let { run: makeNewCfdSellOrder, isLoading: isCreatingNewCfdOrder } = useAsync({ let { run: makeNewCfdSellOrder, isLoading: isCreatingNewCfdOrder } = useAsync({
deferFn: async ([payload]: any[]) => { deferFn: async ([payload]: any[]) => {
@ -110,19 +115,18 @@ export default function App() {
<CurrencyInputField <CurrencyInputField
onChange={(valueString: string) => { onChange={(valueString: string) => {
setOrderPrice(parse(valueString)); setOrderPrice(parse(valueString));
setHasEnteredPrice(true); setAutoRefresh(false);
}} }}
value={priceToDisplay(hasEnteredPrice, orderPrice, priceInfo)} value={format(orderPrice)}
/> />
<IconButton <HStack>
aria-label="Reduce" <Switch
icon={<RepeatIcon />} id="auto-refresh"
onClick={() => { isChecked={autoRefresh}
if (priceInfo) { onChange={() => setAutoRefresh(!autoRefresh)}
setOrderPrice((priceInfo.ask * SPREAD).toString());
}
}}
/> />
<Text>Auto-refresh</Text>
</HStack>
</HStack> </HStack>
<Text>Leverage:</Text> <Text>Leverage:</Text>
@ -138,7 +142,7 @@ export default function App() {
<GridItem colSpan={2} textAlign="center"> <GridItem colSpan={2} textAlign="center">
<Button <Button
disabled={isCreatingNewCfdOrder} disabled={isCreatingNewCfdOrder || orderPrice === "0"}
variant={"solid"} variant={"solid"}
colorScheme={"blue"} colorScheme={"blue"}
onClick={() => { onClick={() => {
@ -194,18 +198,6 @@ export default function App() {
); );
} }
function priceToDisplay(hasEnteredPrice: boolean, orderPrice: string, priceInfo: PriceInfo | null) {
if (!priceInfo) {
return format("0");
}
if (!hasEnteredPrice) {
return format((priceInfo.ask * SPREAD).toString());
}
return format(orderPrice);
}
function format(val: any) { function format(val: any) {
return `$` + val; return `$` + val;
} }

Loading…
Cancel
Save