Skip to content

Commit

Permalink
Added assets amounts to context
Browse files Browse the repository at this point in the history
  • Loading branch information
lmykhailo committed May 15, 2024
1 parent 5f966a7 commit c792e5a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/hooks/swap-form/swap-form.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ interface SwapFormContextValues {
setOutputAsset: React.Dispatch<React.SetStateAction<Asset>>;
inputAsset: Asset;
setInputAsset: React.Dispatch<React.SetStateAction<Asset>>;
inputAssetAmount: string;
setInputAssetAmount: React.Dispatch<React.SetStateAction<string>>;
outputAssetAmount: string;
setOutputAssetAmount: React.Dispatch<React.SetStateAction<string>>;
}

export const SwapFormContext = createContext<SwapFormContextValues>({
outputAsset: DEFAULT_ASSETS_RECORD[USDT],
setOutputAsset: EMPTY_FN,
inputAsset: DEFAULT_ASSETS_RECORD[TON],
setInputAsset: EMPTY_FN
setInputAsset: EMPTY_FN,
inputAssetAmount: '',
setInputAssetAmount: EMPTY_FN,
outputAssetAmount: '',
setOutputAssetAmount: EMPTY_FN
});
2 changes: 1 addition & 1 deletion src/hooks/swap-form/swap-form.hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {useContext} from 'react';

import {SwapFormContext} from './swap-form.context';

export const useSwapFormBalance = () => useContext(SwapFormContext);
export const useSwapForm = () => useContext(SwapFormContext);
13 changes: 12 additions & 1 deletion src/hooks/swap-form/swap-form.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ export const SwapFormProvider: FC<PropsWithChildren> = ({children}) => {
const [outputAsset, setOutputAsset] = useState<Asset>(
DEFAULT_ASSETS_RECORD[USDT]
);
const [inputAssetAmount, setInputAssetAmount] = useState('');
const [outputAssetAmount, setOutputAssetAmount] = useState('');
return (
<SwapFormContext.Provider
value={{inputAsset, outputAsset, setInputAsset, setOutputAsset}}
value={{
inputAsset,
outputAsset,
inputAssetAmount,
outputAssetAmount,
setInputAsset,
setOutputAsset,
setInputAssetAmount,
setOutputAssetAmount
}}
>
{children}
</SwapFormContext.Provider>
Expand Down
15 changes: 11 additions & 4 deletions src/screens/home/swap-form/swap-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useTonConnectUI,
useTonWallet
} from '@tonconnect/ui-react';
import {useContext, useEffect, useMemo, useState} from 'react';
import {useContext, useEffect, useMemo} from 'react';

import {useOutputAssetAmount} from './hooks/use-output-asset-amount.hook.ts';
import styles from './swap-form.module.css';
Expand Down Expand Up @@ -41,9 +41,16 @@ export const SwapForm = () => {
[swapRoutes]
);

const [inputAssetAmount, setInputAssetAmount] = useState('');
const {inputAsset, setInputAsset, outputAsset, setOutputAsset} =
useContext(SwapFormContext);
const {
inputAsset,
setInputAsset,
outputAsset,
setOutputAsset,
inputAssetAmount,
setInputAssetAmount
} = useContext(SwapFormContext);

console.log(inputAssetAmount);

const outputAssetAmount = useOutputAssetAmount(
routes,
Expand Down

0 comments on commit c792e5a

Please sign in to comment.