Skip to content

Commit b03bc44

Browse files
committed
refactor(web): Error handling logic
1 parent 13880f4 commit b03bc44

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo } from "react";
1+
import React, { useState, useMemo, useEffect } from "react";
22
import styled from "styled-components";
33

44
import { useParams } from "react-router-dom";
@@ -68,11 +68,11 @@ const InputDisplay: React.FC<IInputDisplay> = ({
6868
setAmount,
6969
}) => {
7070
const [debouncedAmount, setDebouncedAmount] = useState("");
71+
const [hasInteracted, setHasInteracted] = useState(false);
72+
const [errorMsg, setErrorMsg] = useState<string | undefined>();
7173
useDebounce(() => setDebouncedAmount(amount), 500, [amount]);
7274
const parsedAmount = useParsedAmount(uncommify(debouncedAmount) as `${number}`);
7375

74-
const [errorMsg, setErrorMsg] = useState<string | undefined>();
75-
7676
const { id } = useParams();
7777
const { address } = useAccount();
7878
const { data: balance } = usePnkBalanceOf({
@@ -89,6 +89,18 @@ const InputDisplay: React.FC<IInputDisplay> = ({
8989
const parsedStake = formatPNK(jurorBalance?.[2] || 0n, 0, true);
9090
const isStaking = useMemo(() => action === ActionType.stake, [action]);
9191

92+
useEffect(() => {
93+
if (!hasInteracted || parsedAmount === 0n) {
94+
setErrorMsg(undefined);
95+
} else if (isStaking && balance && parsedAmount > balance) {
96+
setErrorMsg("Insufficient balance to stake this amount");
97+
} else if (!isStaking && jurorBalance && parsedAmount > jurorBalance[2]) {
98+
setErrorMsg("Insufficient staked amount to withdraw this amount");
99+
} else {
100+
setErrorMsg(undefined);
101+
}
102+
}, [hasInteracted, parsedAmount, isStaking, balance, jurorBalance]);
103+
92104
return (
93105
<>
94106
<LabelArea>
@@ -97,6 +109,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({
97109
onClick={() => {
98110
const amount = isStaking ? parsedBalance : parsedStake;
99111
setAmount(amount);
112+
setHasInteracted(true);
100113
}}
101114
>
102115
{isStaking ? "Stake" : "Withdraw"} all
@@ -108,6 +121,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({
108121
value={uncommify(amount)}
109122
onChange={(e) => {
110123
setAmount(e);
124+
setHasInteracted(true);
111125
}}
112126
placeholder={isStaking ? "Amount to stake" : "Amount to withdraw"}
113127
message={errorMsg ?? undefined}
@@ -124,6 +138,8 @@ const InputDisplay: React.FC<IInputDisplay> = ({
124138
setIsSending,
125139
setIsPopupOpen,
126140
setErrorMsg,
141+
hasInteracted,
142+
setHasInteracted,
127143
}}
128144
/>
129145
</EnsureChainContainer>

web/src/pages/Courts/CourtDetails/StakePanel/StakeWithdrawButton.tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useEffect } from "react";
1+
import React, { useMemo } from "react";
22

33
import { useParams } from "react-router-dom";
44
import { useAccount, usePublicClient } from "wagmi";
@@ -35,6 +35,8 @@ interface IActionButton {
3535
setAmount: (arg0: string) => void;
3636
setIsPopupOpen: (arg0: boolean) => void;
3737
setErrorMsg: (arg0: string | undefined) => void;
38+
hasInteracted: boolean;
39+
setHasInteracted: (arg0: boolean) => void;
3840
}
3941

4042
const StakeWithdrawButton: React.FC<IActionButton> = ({
@@ -43,7 +45,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
4345
isSending,
4446
setIsSending,
4547
setIsPopupOpen,
46-
setErrorMsg,
48+
setHasInteracted,
4749
}) => {
4850
const { id } = useParams();
4951
const { address } = useAccount();
@@ -82,7 +84,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
8284
return 0n;
8385
}, [jurorBalance, parsedAmount, isAllowance, isStaking]);
8486

85-
const { config: increaseAllowanceConfig, error: allowanceError } = usePreparePnkIncreaseAllowance({
87+
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
8688
enabled: isAllowance && !isUndefined(klerosCore) && !isUndefined(targetStake) && !isUndefined(allowance),
8789
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
8890
});
@@ -99,7 +101,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
99101
};
100102

101103
const { config: setStakeConfig, error: setStakeError } = usePrepareKlerosCoreSetStake({
102-
enabled: !isUndefined(targetStake) && !isUndefined(id) && !isAllowance,
104+
enabled: !isUndefined(targetStake) && !isUndefined(id) && !isAllowance && parsedAmount !== 0n,
103105
args: [BigInt(id ?? 0), targetStake],
104106
});
105107
const { writeAsync: setStake } = useKlerosCoreSetStake(setStakeConfig);
@@ -114,19 +116,6 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
114116
}
115117
};
116118

117-
useEffect(() => {
118-
let errorMessage = parsedAmount === 0n
119-
? "Please enter a valid amount to stake or withdraw."
120-
: "There was an error processing your request. Please try again later.";
121-
122-
if (isAllowance && allowanceError?.shortMessage) {
123-
setErrorMsg(errorMessage);
124-
} else if (!isAllowance && setStakeError?.shortMessage) {
125-
setErrorMsg(errorMessage);
126-
}
127-
}, [allowanceError, setStakeError, isAllowance, isStaking, parsedAmount, setErrorMsg]);
128-
129-
130119
const buttonProps = {
131120
[ActionType.allowance]: {
132121
text: "Allow PNK",
@@ -160,10 +149,13 @@ useEffect(() => {
160149
(targetStake !== 0n && targetStake < BigInt(courtDetails.court?.minStake)) ||
161150
(isStaking && !isAllowance && isUndefined(setStakeConfig.request))
162151
}
163-
onClick={onClick}
152+
onClick={() => {
153+
setHasInteracted(true);
154+
onClick();
155+
}}
164156
/>
165157
</EnsureChain>
166158
);
167159
};
168160

169-
export default StakeWithdrawButton;
161+
export default StakeWithdrawButton;

0 commit comments

Comments
 (0)