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

Commit

Permalink
refac: add loading when calculating swap amount (#155)
Browse files Browse the repository at this point in the history
* fix: reset preview amount if first input amount is null
* refac: add loading when calculating amount on swap
* chore: format
  • Loading branch information
pedronauck committed May 19, 2022
1 parent fb0496e commit d7a6e71
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"turbo": "^1.2.8",
"typescript": "^4.6.4"
}
}
}
45 changes: 27 additions & 18 deletions packages/app/src/components/CoinInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NumberFormat from "react-number-format";

import { Button } from "./Button";
import { CoinSelector } from "./CoinSelector";
import { Spinner } from "./Spinner";

import { DECIMAL_UNITS } from "~/config";
import { useBalances } from "~/hooks/useBalances";
Expand Down Expand Up @@ -45,6 +46,7 @@ type CoinInputParameters = UseCoinParams & {
showBalance?: boolean;
showMaxButton?: boolean;
autoFocus?: boolean;
isLoading?: boolean;
isAllowed?: (values: NumberFormatValues) => boolean;
onChange?: (val: string) => void;
setMaxBalance?: () => void;
Expand Down Expand Up @@ -149,6 +151,7 @@ export const CoinInput = forwardRef<HTMLInputElement, CoinInputParameters>(
setMaxBalance,
balance,
autoFocus,
isLoading,
},
ref
) => {
Expand All @@ -163,24 +166,30 @@ export const CoinInput = forwardRef<HTMLInputElement, CoinInputParameters>(

return (
<div className={style.transferPropContainer}>
<NumberFormat
autoFocus={autoFocus}
getInputRef={ref}
allowNegative={false}
defaultValue={initialValue}
value={value}
displayType={displayType}
isAllowed={isAllowed}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
onChange?.(e.target.value);
setValue(e.target.value);
}}
decimalScale={DECIMAL_UNITS}
placeholder="0"
className={style.input}
thousandSeparator={false}
onInput={onInput}
/>
{isLoading ? (
<div className="mt-3 ml-4">
<Spinner />
</div>
) : (
<NumberFormat
autoFocus={autoFocus}
getInputRef={ref}
allowNegative={false}
defaultValue={initialValue}
value={value}
displayType={displayType}
isAllowed={isAllowed}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
onChange?.(e.target.value);
setValue(e.target.value);
}}
decimalScale={DECIMAL_UNITS}
placeholder="0"
className={style.input}
thousandSeparator={false}
onInput={onInput}
/>
)}
<div className={style.rightWrapper}>
<CoinSelector
value={coin}
Expand Down
16 changes: 14 additions & 2 deletions packages/app/src/pages/SwapPage/SwapComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const style = {
type SwapComponentProps = {
previewAmount?: bigint | null;
onChange?: (swapState: SwapState) => void;
isLoading?: boolean;
};

export function SwapComponent({
previewAmount: previewValue,
onChange,
isLoading,
previewAmount: previewValue,
}: SwapComponentProps) {
const [initialAmount, setInitialAmount] = useAtom(swapAmountAtom);
const [initialActiveInput, setInitialActiveInput] =
Expand Down Expand Up @@ -76,6 +78,12 @@ export function SwapComponent({
// Set current input
setInitialActiveInput(activeInput?.current);

// This is used to reset preview amount when set first input value for null
if (amount === null) {
toInput.setAmount(null);
return;
}

// Call on onChange
onChange?.({
from: coinFrom.assetId,
Expand Down Expand Up @@ -107,7 +115,11 @@ export function SwapComponent({
<InvertButton onClick={handleInvertCoins} />
</div>
<div className="mb-4">
<CoinInput {...toInput.getInputProps()} coinSelectorDisabled={true} />
<CoinInput
{...toInput.getInputProps()}
coinSelectorDisabled={true}
isLoading={isLoading}
/>
</div>
</>
);
Expand Down
16 changes: 13 additions & 3 deletions packages/app/src/pages/SwapPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function SwapPage() {
},
}
);

const { mutate: swap, isLoading: isSwaping } = useMutation(
async () => {
if (!swapState) return;
Expand All @@ -73,22 +74,31 @@ export default function SwapPage() {
balances,
swapState.direction === ActiveInput.to ? swapState.to : swapState.from
);

const shouldDisableButton =
isLoading || isSwaping || !hasLiquidity || !previewAmount || hasNotBalance;

const getButtonText = () => {
function getButtonText() {
if (!hasLiquidity) return "Insufficient liquidity";
if (isSwaping) return "Loading...";
return "Swap";
};
}

function handleSwap(state: SwapState) {
setSwapState(state);
}

return (
<PageContent>
<PageContent.Title>
<MdSwapCalls className="text-primary-500" />
Swap
</PageContent.Title>
<SwapComponent previewAmount={previewAmount} onChange={setSwapState} />
<SwapComponent
previewAmount={previewAmount}
onChange={handleSwap}
isLoading={isLoading}
/>
<Button
isFull
size="lg"
Expand Down
2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
packages:
- 'packages/*'
- "packages/*"

0 comments on commit d7a6e71

Please sign in to comment.