Skip to content

Commit 1c02948

Browse files
refactor(web): better-disabling-conditions-for-buttons
1 parent 7cdc962 commit 1c02948

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

web/src/hooks/useTransactionBatcher.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import { useSimulateTransactionBatcherBatchSend, useWriteTransactionBatcherBatch
88

99
export type TransactionBatcherConfig = SimulateContractParameters[];
1010

11+
type TransactionBatcherOptions = {
12+
// determines if simulation query is enabled
13+
enabled: boolean;
14+
};
15+
1116
/**
1217
* @param configs SimulateContractParameters[] - an array of useWriteContract Parameters
18+
* @param options TransactionBatcherOptions - an object containing options to apply to hook behaviour
1319
* @description This takes in multiple write calls and batches them into a single transaction
1420
* @example useTransactionBatcher([
1521
* { address : "contract one address",
@@ -26,15 +32,18 @@ export type TransactionBatcherConfig = SimulateContractParameters[];
2632
* },
2733
* ])
2834
*/
29-
const useTransactionBatcher = (configs?: TransactionBatcherConfig) => {
35+
const useTransactionBatcher = (
36+
configs?: TransactionBatcherConfig,
37+
options: TransactionBatcherOptions = { enabled: true }
38+
) => {
3039
const validatedConfigs = configs ?? [];
3140
const {
3241
data: batchConfig,
3342
isLoading,
3443
isError,
3544
} = useSimulateTransactionBatcherBatchSend({
3645
query: {
37-
enabled: !isUndefined(configs),
46+
enabled: !isUndefined(configs) && options.enabled,
3847
},
3948
args: [
4049
validatedConfigs.map((config) => config?.address),

web/src/pages/Cases/CaseDetails/MaintenanceButtons/DistributeRewards.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { klerosCoreAbi, klerosCoreAddress } from "hooks/contracts/generated";
1010
import useTransactionBatcher, { type TransactionBatcherConfig } from "hooks/useTransactionBatcher";
1111
import { wrapWithToast } from "utils/wrapWithToast";
1212

13+
import { Period } from "src/graphql/graphql";
1314
import { isUndefined } from "src/utils";
1415

1516
import { IBaseMaintenanceButton } from ".";
@@ -21,9 +22,10 @@ const StyledButton = styled(Button)`
2122
interface IDistributeRewards extends IBaseMaintenanceButton {
2223
numberOfVotes?: string;
2324
roundIndex?: string;
25+
period?: string;
2426
}
2527

26-
const DistributeRewards: React.FC<IDistributeRewards> = ({ id, numberOfVotes, roundIndex, setIsOpen }) => {
28+
const DistributeRewards: React.FC<IDistributeRewards> = ({ id, numberOfVotes, roundIndex, setIsOpen, period }) => {
2729
const [isSending, setIsSending] = useState(false);
2830
const [contractConfigs, setContractConfigs] = useState<TransactionBatcherConfig>();
2931
const publicClient = usePublicClient();
@@ -51,12 +53,18 @@ const DistributeRewards: React.FC<IDistributeRewards> = ({ id, numberOfVotes, ro
5153
setContractConfigs(argsArr);
5254
}, [id, roundIndex, numberOfVotes, chainId]);
5355

54-
const { executeBatch, isLoading: isLoadingConfig, isError } = useTransactionBatcher(contractConfigs);
56+
const {
57+
executeBatch,
58+
isLoading: isLoadingConfig,
59+
isError,
60+
} = useTransactionBatcher(contractConfigs, {
61+
enabled: !isUndefined(period) && period === Period.Execution,
62+
});
5563

5664
const isLoading = useMemo(() => isLoadingConfig || isSending, [isLoadingConfig, isSending]);
5765
const isDisabled = useMemo(
58-
() => isUndefined(id) || isUndefined(numberOfVotes) || isError || isLoading,
59-
[id, numberOfVotes, isError, isLoading]
66+
() => isUndefined(id) || isUndefined(numberOfVotes) || isError || isLoading || period !== Period.Execution,
67+
[id, numberOfVotes, isError, isLoading, period]
6068
);
6169

6270
const handleClick = () => {

web/src/pages/Cases/CaseDetails/MaintenanceButtons/DrawButton.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Button } from "@kleros/ui-components-library";
88
import { useSimulateKlerosCoreDraw, useWriteKlerosCoreDraw } from "hooks/contracts/generated";
99
import { wrapWithToast } from "utils/wrapWithToast";
1010

11+
import { Period } from "src/graphql/graphql";
1112
import { isUndefined } from "src/utils";
1213

1314
import { IBaseMaintenanceButton } from ".";
@@ -18,9 +19,10 @@ const StyledButton = styled(Button)`
1819

1920
interface IDrawButton extends IBaseMaintenanceButton {
2021
numberOfVotes?: string;
22+
period?: string;
2123
}
2224

23-
const DrawButton: React.FC<IDrawButton> = ({ id, numberOfVotes, setIsOpen }) => {
25+
const DrawButton: React.FC<IDrawButton> = ({ id, numberOfVotes, setIsOpen, period }) => {
2426
const [isSending, setIsSending] = useState(false);
2527
const publicClient = usePublicClient();
2628

@@ -30,7 +32,7 @@ const DrawButton: React.FC<IDrawButton> = ({ id, numberOfVotes, setIsOpen }) =>
3032
isError,
3133
} = useSimulateKlerosCoreDraw({
3234
query: {
33-
enabled: !isUndefined(id) && !isUndefined(numberOfVotes),
35+
enabled: !isUndefined(id) && !isUndefined(numberOfVotes) && !isUndefined(period) && period === Period.Evidence,
3436
},
3537
args: [BigInt(id ?? 0), BigInt(numberOfVotes ?? 0)],
3638
});
@@ -39,8 +41,8 @@ const DrawButton: React.FC<IDrawButton> = ({ id, numberOfVotes, setIsOpen }) =>
3941

4042
const isLoading = useMemo(() => isLoadingConfig || isSending, [isLoadingConfig, isSending]);
4143
const isDisabled = useMemo(
42-
() => isUndefined(id) || isUndefined(numberOfVotes) || isError || isLoading,
43-
[id, numberOfVotes, isError, isLoading]
44+
() => isUndefined(id) || isUndefined(numberOfVotes) || isError || isLoading || period !== Period.Evidence,
45+
[id, numberOfVotes, isError, isLoading, period]
4446
);
4547
const handleClick = () => {
4648
if (!drawConfig) return;

web/src/pages/Cases/CaseDetails/MaintenanceButtons/ExecuteRuling.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Button } from "@kleros/ui-components-library";
88
import { useSimulateKlerosCoreExecuteRuling, useWriteKlerosCoreExecuteRuling } from "hooks/contracts/generated";
99
import { wrapWithToast } from "utils/wrapWithToast";
1010

11+
import { Period } from "src/graphql/graphql";
1112
import { isUndefined } from "src/utils";
1213

1314
import { IBaseMaintenanceButton } from ".";
@@ -16,9 +17,12 @@ const StyledButton = styled(Button)`
1617
width: 100%;
1718
`;
1819

19-
type IExecuteRulingButton = IBaseMaintenanceButton;
20+
interface IExecuteRulingButton extends IBaseMaintenanceButton {
21+
period?: string;
22+
ruled?: boolean;
23+
}
2024

21-
const ExecuteRulingButton: React.FC<IExecuteRulingButton> = ({ id, setIsOpen }) => {
25+
const ExecuteRulingButton: React.FC<IExecuteRulingButton> = ({ id, setIsOpen, period, ruled }) => {
2226
const [isSending, setIsSending] = useState(false);
2327
const publicClient = usePublicClient();
2428

@@ -28,15 +32,18 @@ const ExecuteRulingButton: React.FC<IExecuteRulingButton> = ({ id, setIsOpen })
2832
isError,
2933
} = useSimulateKlerosCoreExecuteRuling({
3034
query: {
31-
enabled: !isUndefined(id),
35+
enabled: !isUndefined(id) && !isUndefined(period) && period === Period.Execution && !ruled,
3236
},
3337
args: [BigInt(id ?? 0)],
3438
});
3539

3640
const { writeContractAsync: rule } = useWriteKlerosCoreExecuteRuling();
3741

3842
const isLoading = useMemo(() => isLoadingConfig || isSending, [isLoadingConfig, isSending]);
39-
const isDisabled = useMemo(() => isUndefined(id) || isError || isLoading, [id, isError, isLoading]);
43+
const isDisabled = useMemo(
44+
() => isUndefined(id) || isError || isLoading || period !== Period.Execution || ruled,
45+
[id, isError, isLoading, period, ruled]
46+
);
4047
const handleClick = () => {
4148
if (!ruleConfig) return;
4249

web/src/pages/Cases/CaseDetails/MaintenanceButtons/PassPeriodButton.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Button } from "@kleros/ui-components-library";
88
import { useSimulateKlerosCorePassPeriod, useWriteKlerosCorePassPeriod } from "hooks/contracts/generated";
99
import { wrapWithToast } from "utils/wrapWithToast";
1010

11+
import { Period } from "src/graphql/graphql";
1112
import { isUndefined } from "src/utils";
1213

1314
import { IBaseMaintenanceButton } from ".";
@@ -16,9 +17,11 @@ const StyledButton = styled(Button)`
1617
width: 100%;
1718
`;
1819

19-
type IPassPeriodButton = IBaseMaintenanceButton;
20+
interface IPassPeriodButton extends IBaseMaintenanceButton {
21+
period?: string;
22+
}
2023

21-
const PassPeriodButton: React.FC<IPassPeriodButton> = ({ id, setIsOpen }) => {
24+
const PassPeriodButton: React.FC<IPassPeriodButton> = ({ id, setIsOpen, period }) => {
2225
const [isSending, setIsSending] = useState(false);
2326
const publicClient = usePublicClient();
2427

@@ -28,15 +31,18 @@ const PassPeriodButton: React.FC<IPassPeriodButton> = ({ id, setIsOpen }) => {
2831
isError,
2932
} = useSimulateKlerosCorePassPeriod({
3033
query: {
31-
enabled: !isUndefined(id),
34+
enabled: !isUndefined(id) && !isUndefined(period) && period !== Period.Execution,
3235
},
3336
args: [BigInt(id ?? 0)],
3437
});
3538

3639
const { writeContractAsync: passPeriod } = useWriteKlerosCorePassPeriod();
3740

3841
const isLoading = useMemo(() => isLoadingConfig || isSending, [isLoadingConfig, isSending]);
39-
const isDisabled = useMemo(() => isUndefined(id) || isError || isLoading, [id, isError, isLoading]);
42+
const isDisabled = useMemo(
43+
() => isUndefined(id) || isError || isLoading || period === Period.Execution,
44+
[id, isError, isLoading, period]
45+
);
4046
const handleClick = () => {
4147
if (!passPeriodConfig) return;
4248

web/src/pages/Cases/CaseDetails/MaintenanceButtons/index.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,19 @@ const MaintenanceButtons: React.FC = () => {
104104
<PopupContainer>
105105
<EnsureChain>
106106
<>
107-
<DrawButton {...{ id, setIsOpen }} numberOfVotes={dispute?.currentRound.nbVotes} />
108-
<PassPeriodButton {...{ id, setIsOpen }} />
107+
<DrawButton
108+
{...{ id, setIsOpen }}
109+
numberOfVotes={dispute?.currentRound.nbVotes}
110+
period={dispute?.period}
111+
/>
112+
<PassPeriodButton {...{ id, setIsOpen }} period={dispute?.period} />
109113
<DistributeRewards
110114
{...{ id, setIsOpen }}
111115
roundIndex={dispute?.currentRoundIndex}
112116
numberOfVotes={dispute?.currentRound.nbVotes}
117+
period={dispute?.period}
113118
/>
114-
<ExecuteRulingButton {...{ id, setIsOpen }} />
119+
<ExecuteRulingButton {...{ id, setIsOpen }} period={dispute?.period} ruled={dispute?.ruled} />
115120
</>
116121
</EnsureChain>
117122
</PopupContainer>

0 commit comments

Comments
 (0)