Skip to content

Commit

Permalink
Update prefilledState data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoulloa committed Nov 2, 2024
1 parent bfce608 commit 910abd3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
22 changes: 13 additions & 9 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ export default function Home() {
});

useEffect(() => {
const newXlmToken =
xlmTokenList.find((tList) => tList.network === activeChain?.id)?.assets[0].contract ?? null;
setXlmToken(newXlmToken);
if (prefilledState.INPUT?.currencyId == null) {
const newXlmToken =
xlmTokenList.find((tList) => tList.network === activeChain?.id)?.assets[0].contract ?? null;
setXlmToken(newXlmToken);

const newPrefilledState = {
[Field.INPUT]: { currencyId: newXlmToken },
[Field.OUTPUT]: { currencyId: null },
};
setPrefilledState(newPrefilledState);
const newPrefilledState = {
[Field.INPUT]: { currencyId: newXlmToken },
[Field.OUTPUT]: { currencyId: null },
};
setPrefilledState(newPrefilledState);
}
}, [activeChain, xlmToken]);

return (
<>
<SEO title="Soroswap" data-testid="SEO" />
{xlmToken && <SwapComponent prefilledState={prefilledState} />}
{xlmToken && (
<SwapComponent prefilledState={prefilledState} setPrefilledState={setPrefilledState} />
)}
</>
);
}
28 changes: 23 additions & 5 deletions src/components/Swap/SwapComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ const INITIAL_SWAP_STATE = {

export function SwapComponent({
prefilledState = {},
setPrefilledState,
disableTokenInputs = false,
handleDoSwap,
}: {
prefilledState?: Partial<SwapState>;
setPrefilledState?: (value: Partial<SwapState>) => void;
disableTokenInputs?: boolean;
handleDoSwap?: (setSwapState: (value: SetStateAction<SwapStateProps>) => void) => void;
}) {
const sorobanContext = useSorobanReact();
const { refetch } = useGetMyBalances()
const { refetch } = useGetMyBalances();
const { SnackbarContext } = useContext(AppContext);
const [showPriceImpactModal, setShowPriceImpactModal] = useState<boolean>(false);
const [txError, setTxError] = useState<boolean>(false);
Expand Down Expand Up @@ -153,7 +155,10 @@ export function SwapComponent({
} = useDerivedSwapInfo(state);

useEffect(() => {
if (typeof currencyBalances[Field.OUTPUT] != 'string' && currencyBalances[Field.OUTPUT].balance === undefined) {
if (
typeof currencyBalances[Field.OUTPUT] != 'string' &&
currencyBalances[Field.OUTPUT].balance === undefined
) {
setNeedTrustline(true);
} else {
setNeedTrustline(false);
Expand Down Expand Up @@ -202,13 +207,25 @@ export function SwapComponent({
const handleInputSelect = useCallback(
(inputCurrency: TokenType) => {
onCurrencySelection(Field.INPUT, inputCurrency);
setPrefilledState
? setPrefilledState({
[Field.INPUT]: { currencyId: inputCurrency.contract },
[Field.OUTPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
})
: null;
},
[onCurrencySelection],
);

const handleOutputSelect = useCallback(
(outputCurrency: TokenType) => {
onCurrencySelection(Field.OUTPUT, outputCurrency);
setPrefilledState
? setPrefilledState({
[Field.INPUT]: { currencyId: prefilledState.OUTPUT?.currencyId },
[Field.OUTPUT]: { currencyId: outputCurrency.contract },
})
: null;
},
[onCurrencySelection],
);
Expand Down Expand Up @@ -309,9 +326,10 @@ export function SwapComponent({
...currentState,
showConfirm: false,
}));
}).finally(() => {
refetch()
nativeBalance.mutate()
})
.finally(() => {
refetch();
nativeBalance.mutate();
});
};

Expand Down

0 comments on commit 910abd3

Please sign in to comment.