Skip to content

Commit

Permalink
feat: qol clear fields after tx submit
Browse files Browse the repository at this point in the history
  • Loading branch information
zkSoju committed Nov 22, 2022
1 parent 5e37682 commit e20b380
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 54 deletions.
56 changes: 30 additions & 26 deletions components/DammTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ const DammTabContent = () => {
const { callback: toastCallback } = useTriggerToast();

// load up state
const [fields, onUserInput, independentField] = useProvideStore(
(state) => [state.fields, state.onUserInput, state.independentField],
const [fields, onUserInput, independentField, clearFields] = useProvideStore(
(state) => [
state.fields,
state.onUserInput,
state.independentField,
state.clearFields,
],
shallow
);

Expand Down Expand Up @@ -92,9 +97,7 @@ const DammTabContent = () => {
const handleProvideLiquidity = () => {
callback?.()
.then((tx) => {
tx &&
parsedAmounts[Field.CURRENCY_A] &&
parsedAmounts[Field.CURRENCY_B];
parsedAmounts[Field.CURRENCY_A] && parsedAmounts[Field.CURRENCY_B];
toastCallback?.({
title: "Liquidity Added",
description: `${formatTransactionAmount(
Expand All @@ -107,6 +110,7 @@ const DammTabContent = () => {
txid: tx.hash,
type: "success",
});
clearFields();
})
.catch((e) => {
toastCallback?.({
Expand All @@ -120,8 +124,7 @@ const DammTabContent = () => {
const handleApproveA = () => {
approveCallbackA?.()
.then((tx) => {
tx &&
parsedAmounts[Field.CURRENCY_A] &&
parsedAmounts[Field.CURRENCY_A] &&
toastCallback?.({
title: "Token Approved",
description: `${formatTransactionAmount(
Expand All @@ -143,8 +146,7 @@ const DammTabContent = () => {
const handleApproveB = () => {
approveCallbackB?.()
.then((tx) => {
tx &&
parsedAmounts[Field.CURRENCY_B] &&
parsedAmounts[Field.CURRENCY_B] &&
toastCallback?.({
title: "Token Approved",
description: `${formatTransactionAmount(
Expand Down Expand Up @@ -173,10 +175,11 @@ const DammTabContent = () => {

//////////////////////////////////////////////////////////

const [withdrawFields, onUserInputWithdraw] = useWithdrawStore(
(state) => [state.fields, state.onUserInput],
shallow
);
const [withdrawFields, onUserInputWithdraw, clearWithdrawFields] =
useWithdrawStore(
(state) => [state.fields, state.onUserInput, state.clearFields],
shallow
);

const {
parsedAmounts: withdrawAmounts,
Expand All @@ -195,8 +198,7 @@ const DammTabContent = () => {
const handleWithdraw = () => {
withdrawCallback?.()
.then((tx) => {
tx &&
withdrawAmounts[Field.CURRENCY_A] &&
withdrawAmounts[Field.CURRENCY_A] &&
toastCallback?.({
title: "Liquidity Removed",
description: `${formatTransactionAmount(
Expand All @@ -205,6 +207,7 @@ const DammTabContent = () => {
txid: tx.hash,
type: "success",
});
clearWithdrawFields();
})
.catch((e) => {
toastCallback?.({
Expand All @@ -231,8 +234,8 @@ const DammTabContent = () => {
currencyBalances: mintBalance,
} = useDerivedMintInfo();

const [mintFields, onUserInputMint] = useMintStore(
(state) => [state.fields, state.onUserInput],
const [mintFields, onUserInputMint, clearMintField] = useMintStore(
(state) => [state.fields, state.onUserInput, state.clearField],
shallow
);

Expand All @@ -251,15 +254,15 @@ const DammTabContent = () => {
const handleMintA = () => {
mintCallbackA?.()
.then((tx) => {
tx &&
toastCallback?.({
title: "Minted",
description: `${formatTransactionAmount(
currencyAmountToPreciseFloat(mintAmounts[Field.CURRENCY_A])
)} ${mintCurrency[Field.CURRENCY_A]?.symbol}`,
txid: tx.hash,
type: "success",
});
toastCallback?.({
title: "Minted",
description: `${formatTransactionAmount(
currencyAmountToPreciseFloat(mintAmounts[Field.CURRENCY_A])
)} ${mintCurrency[Field.CURRENCY_A]?.symbol}`,
txid: tx.hash,
type: "success",
});
clearMintField(Field.CURRENCY_A);
})
.catch((e) => {
toastCallback?.({
Expand All @@ -282,6 +285,7 @@ const DammTabContent = () => {
txid: tx.hash,
type: "success",
});
clearMintField(Field.CURRENCY_B);
})
.catch((e) => {
toastCallback?.({
Expand Down
60 changes: 32 additions & 28 deletions components/SwapTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ const SwapTabContent = () => {

const { callback: toastCallback } = useTriggerToast();

const [swapCurrencies, fields, onUserInput, independentField] = useSwapStore(
(state) => [
state.swapCurrencies,
state.fields,
state.onUserInput,
state.independentField,
],
shallow
);
const [swapCurrencies, fields, onUserInput, independentField, clearFields] =
useSwapStore(
(state) => [
state.swapCurrencies,
state.fields,
state.onUserInput,
state.independentField,
state.clearFields,
],
shallow
);

const dependentField =
independentField === Field.CURRENCY_A ? Field.CURRENCY_B : Field.CURRENCY_A;
Expand Down Expand Up @@ -109,7 +111,7 @@ const SwapTabContent = () => {
const handleApprove = () => {
approveCallback?.()
.then((tx) => {
if (!parsedAmounts[Field.CURRENCY_A] || !tx) return;
if (!parsedAmounts[Field.CURRENCY_A]) return;
toastCallback?.({
title: "Approved",
description: `${formatCurrencyAmount(
Expand All @@ -132,20 +134,19 @@ const SwapTabContent = () => {
const handleSwap = () => {
swapCallback?.()
.then((tx) => {
tx &&
toastCallback?.({
title: "Swap",
description: `${formatCurrencyAmount(
parsedAmounts[Field.CURRENCY_A],
6
)} ${
currencies[Field.CURRENCY_A]?.symbol
} for ${formatCurrencyAmount(parsedAmounts[Field.CURRENCY_B], 6)} ${
currencies[Field.CURRENCY_B]?.symbol
}`,
txid: tx.hash,
type: "success",
});
toastCallback?.({
title: "Swap",
description: `${formatCurrencyAmount(
parsedAmounts[Field.CURRENCY_A],
6
)} ${currencies[Field.CURRENCY_A]?.symbol} for ${formatCurrencyAmount(
parsedAmounts[Field.CURRENCY_B],
6
)} ${currencies[Field.CURRENCY_B]?.symbol}`,
txid: tx.hash,
type: "success",
});
clearFields();
})
.catch((e) => {
toastCallback?.({
Expand All @@ -162,8 +163,8 @@ const SwapTabContent = () => {

/////////////////////////////

const [mintFields, onUserInputMint] = useMintStore(
(state) => [state.fields, state.onUserInput],
const [mintFields, onUserInputMint, clearMintField] = useMintStore(
(state) => [state.fields, state.onUserInput, state.clearField],
shallow
);

Expand Down Expand Up @@ -199,6 +200,7 @@ const SwapTabContent = () => {
txid: tx.hash,
type: "success",
});
clearMintField(Field.CURRENCY_A);
})
.catch((e) => {
toastCallback?.({
Expand All @@ -223,6 +225,7 @@ const SwapTabContent = () => {
txid: txid.hash,
type: "success",
});
clearMintField(Field.CURRENCY_B);
})
.catch((e) => {
toastCallback?.({
Expand All @@ -243,8 +246,8 @@ const SwapTabContent = () => {

/////////////////////////////

const [burnFields, onUserInputBurn] = useBurnStore(
(state) => [state.fields, state.onUserInput],
const [burnFields, onUserInputBurn, clearBurnFields] = useBurnStore(
(state) => [state.fields, state.onUserInput, state.clearFields],
shallow
);

Expand Down Expand Up @@ -353,6 +356,7 @@ const SwapTabContent = () => {
txid: tx.hash,
type: "success",
});
clearBurnFields();
})
.catch((e) => {
toastCallback?.({
Expand Down
9 changes: 9 additions & 0 deletions state/burn/useBurnStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface BurnStoreState {
fields: { [field in Field]: string };
onUserInput: (field: Field, value: string) => void;
onSwitchTokens: () => void;
clearFields: () => void;
}

export const useBurnStore = create<BurnStoreState>((set, get) => ({
Expand All @@ -41,4 +42,12 @@ export const useBurnStore = create<BurnStoreState>((set, get) => ({
);
},
onSwitchTokens: () => {},
clearFields: () => {
set(
produce((draft) => {
draft.fields[Field.CURRENCY_A] = "";
draft.fields[Field.CURRENCY_B] = "";
})
);
},
}));
8 changes: 8 additions & 0 deletions state/mint/useMintStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface MintStoreState {
fields: { [field in Field]: string };
onUserInput: (field: Field, value: string) => void;
onSwitchTokens: () => void;
clearField: (field: Field) => void;
}

export const useMintStore = create<MintStoreState>((set, get) => ({
Expand All @@ -41,4 +42,11 @@ export const useMintStore = create<MintStoreState>((set, get) => ({
);
},
onSwitchTokens: () => {},
clearField: (field) => {
set(
produce((draft) => {
draft.fields[field] = "";
})
);
},
}));
9 changes: 9 additions & 0 deletions state/provide/useProvideStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ProvideStoreState {
onUserInput: (field: Field, value: string) => void;
onSwitchTokens: () => void;
independentField: Field;
clearFields: () => void;
}

export const useProvideStore = create<ProvideStoreState>(
Expand All @@ -47,6 +48,14 @@ export const useProvideStore = create<ProvideStoreState>(
},
onSwitchTokens: () => {},
independentField: Field.CURRENCY_A,
clearFields: () => {
set(
produce((draft) => {
draft.fields[Field.CURRENCY_A] = "";
draft.fields[Field.CURRENCY_B] = "";
})
);
},
}),
{
name: "ProvideStore",
Expand Down
8 changes: 8 additions & 0 deletions state/withdraw/useWithdrawStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface WithdrawStoreState {
fields: { [field in Field]: string };
onUserInput: (field: Field, value: string) => void;
onSwitchTokens: () => void;
clearFields: () => void;
}

export const useWithdrawStore = create<WithdrawStoreState>((set, get) => ({
Expand All @@ -39,4 +40,11 @@ export const useWithdrawStore = create<WithdrawStoreState>((set, get) => ({
);
},
onSwitchTokens: () => {},
clearFields: () => {
set(
produce((draft) => {
draft.fields[Field.CURRENCY_A] = "";
})
);
},
}));

0 comments on commit e20b380

Please sign in to comment.