Skip to content

Commit 5241361

Browse files
authored
Merge pull request #1894 from kleros/refactor/sdk-and-voting-tab-improvements
Refactor/sdk and voting tab improvements
2 parents 489ad78 + eb9bbe0 commit 5241361

File tree

5 files changed

+88
-51
lines changed

5 files changed

+88
-51
lines changed

kleros-sdk/src/dataMappings/utils/populateTemplate.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ export const populateTemplate = (mustacheTemplate: string, data: any): DisputeDe
1111
throw validation.error;
1212
}
1313

14-
// Filter out any existing answer with id 0 and add our standard Refuse to Arbitrate option
15-
(dispute as DisputeDetails).answers = [
16-
RefuseToArbitrateAnswer,
17-
...((dispute as DisputeDetails).answers.filter((answer) => answer.id && BigInt(answer.id) !== BigInt(0)) || []),
18-
];
14+
return findAndUpdateRTA(dispute);
15+
};
16+
17+
// Filter out any existing answer with id 0 and add customised Refuse to Arbitrate option
18+
const findAndUpdateRTA = (dispute: DisputeDetails) => {
19+
const templateRTAIndex = (dispute as DisputeDetails).answers.findIndex(
20+
(answer) => answer.id && BigInt(answer.id) === BigInt(0)
21+
);
22+
23+
if (templateRTAIndex !== -1) {
24+
dispute.answers[templateRTAIndex] = {
25+
...RefuseToArbitrateAnswer,
26+
description: dispute.answers[templateRTAIndex].description ?? RefuseToArbitrateAnswer.description,
27+
};
28+
} else {
29+
dispute.answers = [RefuseToArbitrateAnswer, ...dispute.answers];
30+
}
1931

2032
return dispute;
2133
};

kleros-sdk/test/getDispute.test.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,14 @@ describe("getDispute", () => {
105105
expect(result?.answers[2].id).toBe("0x2");
106106
});
107107

108-
it("should overwrite existing answer with id 0x0 or 0x00", async () => {
108+
it("should only overwrite existing answer with id 0x0 or 0x00's title and not overwrite description", async () => {
109+
const customRTAAnswer = {
110+
id: "0x0",
111+
title: "Custom Refuse Title",
112+
description: "Custom Refuse Description",
113+
reserved: true,
114+
};
115+
109116
// Test with 0x0
110117
const mockTemplate0x0 = {
111118
disputeTemplate: {
@@ -114,12 +121,7 @@ describe("getDispute", () => {
114121
description: "Test Description",
115122
question: "Test Question",
116123
answers: [
117-
{
118-
id: "0x0",
119-
title: "Custom Refuse Title",
120-
description: "Custom Refuse Description",
121-
reserved: true,
122-
},
124+
customRTAAnswer,
123125
{
124126
id: "0x1",
125127
title: "Yes",
@@ -145,7 +147,8 @@ describe("getDispute", () => {
145147
});
146148

147149
expect(result?.answers).toHaveLength(2);
148-
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
150+
expect(result?.answers[0].title).toEqual(standardRefuseToArbitrateAnswer.title);
151+
expect(result?.answers[0].description).toEqual(customRTAAnswer.description);
149152
expect(result?.answers[1].id).toBe("0x1");
150153

151154
// Test with 0x00
@@ -156,12 +159,7 @@ describe("getDispute", () => {
156159
description: "Test Description",
157160
question: "Test Question",
158161
answers: [
159-
{
160-
id: "0x00",
161-
title: "Custom Refuse Title",
162-
description: "Custom Refuse Description",
163-
reserved: true,
164-
},
162+
customRTAAnswer,
165163
{
166164
id: "0x1",
167165
title: "Yes",
@@ -186,7 +184,8 @@ describe("getDispute", () => {
186184
});
187185

188186
expect(result?.answers).toHaveLength(2);
189-
expect(result?.answers[0]).toEqual(standardRefuseToArbitrateAnswer);
187+
expect(result?.answers[0].title).toEqual(standardRefuseToArbitrateAnswer.title);
188+
expect(result?.answers[0].description).toEqual(customRTAAnswer.description);
190189
expect(result?.answers[1].id).toBe("0x1");
191190
});
192191

web/src/pages/Cases/CaseDetails/Voting/Classic/OptionsContainer.tsx

+41-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, { useCallback, useState } from "react";
1+
import React, { useCallback, useMemo, useState } from "react";
22
import styled from "styled-components";
33

44
import ReactMarkdown from "react-markdown";
55
import { useParams } from "react-router-dom";
66

7-
import { Button } from "@kleros/ui-components-library";
7+
import { Button, Tooltip } from "@kleros/ui-components-library";
88

99
import { usePopulatedDisputeData } from "hooks/queries/usePopulatedDisputeData";
1010
import { isUndefined } from "utils/index";
@@ -13,10 +13,13 @@ import { EnsureChain } from "components/EnsureChain";
1313

1414
import JustificationArea from "./JustificationArea";
1515
import { Answer } from "@kleros/kleros-sdk";
16+
import { RefuseToArbitrateAnswer } from "@kleros/kleros-sdk/src/dataMappings/utils/disputeDetailsSchema";
1617

1718
const MainContainer = styled.div`
1819
width: 100%;
1920
height: auto;
21+
display: flex;
22+
flex-direction: column;
2023
`;
2124

2225
const OptionsContainer = styled.div`
@@ -40,6 +43,9 @@ const RefuseToArbitrateContainer = styled.div`
4043
justify-content: center;
4144
`;
4245

46+
const StyledEnsureChain = styled(EnsureChain)`
47+
align-self: center;
48+
`;
4349
interface IOptions {
4450
arbitrable: `0x${string}`;
4551
handleSelection: (arg0: bigint) => Promise<void>;
@@ -53,6 +59,12 @@ const Options: React.FC<IOptions> = ({ arbitrable, handleSelection, justificatio
5359
const [chosenOption, setChosenOption] = useState(BigInt(-1));
5460
const [isSending, setIsSending] = useState(false);
5561

62+
const updatedRTA = useMemo(() => {
63+
const RTAFromTemplate = disputeDetails?.answers?.find((answer) => BigInt(answer.id) === BigInt(0));
64+
if (!RTAFromTemplate) return RefuseToArbitrateAnswer;
65+
return RTAFromTemplate;
66+
}, [disputeDetails]);
67+
5668
const onClick = useCallback(
5769
async (id: bigint) => {
5870
setIsSending(true);
@@ -71,30 +83,36 @@ const Options: React.FC<IOptions> = ({ arbitrable, handleSelection, justificatio
7183
{!isUndefined(justification) && !isUndefined(setJustification) ? (
7284
<JustificationArea {...{ justification, setJustification }} />
7385
) : null}
74-
<OptionsContainer>
75-
{disputeDetails?.answers?.map((answer: Answer) => {
76-
return (
77-
<EnsureChain key={answer.title}>
78-
<Button
79-
text={answer.title}
80-
disabled={isSending}
81-
isLoading={chosenOption === BigInt(answer.id)}
82-
onClick={() => onClick(BigInt(answer.id))}
83-
/>
84-
</EnsureChain>
85-
);
86-
})}
87-
</OptionsContainer>
86+
{isUndefined(disputeDetails?.answers) ? null : (
87+
<StyledEnsureChain>
88+
<OptionsContainer>
89+
{disputeDetails?.answers?.map((answer: Answer) => {
90+
return BigInt(answer.id) !== BigInt(0) ? (
91+
<Tooltip text={answer.description} key={answer.title}>
92+
<Button
93+
text={answer.title}
94+
disabled={isSending}
95+
isLoading={chosenOption === BigInt(answer.id)}
96+
onClick={() => onClick(BigInt(answer.id))}
97+
/>
98+
</Tooltip>
99+
) : null;
100+
})}
101+
</OptionsContainer>
102+
</StyledEnsureChain>
103+
)}
88104
</MainContainer>
89105
<RefuseToArbitrateContainer>
90106
<EnsureChain>
91-
<Button
92-
variant="secondary"
93-
text="Refuse to Arbitrate"
94-
disabled={isSending}
95-
isLoading={chosenOption === BigInt(0)}
96-
onClick={() => onClick(BigInt(0))}
97-
/>
107+
<Tooltip text={updatedRTA.description}>
108+
<Button
109+
variant="secondary"
110+
text={updatedRTA.title}
111+
disabled={isSending}
112+
isLoading={chosenOption === BigInt(0)}
113+
onClick={() => onClick(BigInt(0))}
114+
/>
115+
</Tooltip>
98116
</EnsureChain>
99117
</RefuseToArbitrateContainer>
100118
</>

web/src/pages/Cases/CaseDetails/Voting/Classic/Reveal.tsx

+14-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import InfoCard from "components/InfoCard";
2121

2222
import JustificationArea from "./JustificationArea";
2323
import { Answer } from "@kleros/kleros-sdk";
24+
import { EnsureChain } from "components/EnsureChain";
2425

2526
const Container = styled.div`
2627
width: 100%;
@@ -35,6 +36,10 @@ const StyledButton = styled(Button)`
3536
margin: 16px auto;
3637
`;
3738

39+
const StyledEnsureChain = styled(EnsureChain)`
40+
margin: 8px auto;
41+
`;
42+
3843
const ReactMarkdownWrapper = styled.div``;
3944
interface IReveal {
4045
arbitrable: `0x${string}`;
@@ -106,13 +111,15 @@ const Reveal: React.FC<IReveal> = ({ arbitrable, voteIDs, setIsOpen, commit, isR
106111
<ReactMarkdown>{disputeDetails?.question ?? ""}</ReactMarkdown>
107112
</ReactMarkdownWrapper>
108113
<JustificationArea {...{ justification, setJustification }} />
109-
<StyledButton
110-
variant="secondary"
111-
text="Justify & Reveal"
112-
disabled={isSending || isUndefined(disputeDetails)}
113-
isLoading={isSending}
114-
onClick={handleReveal}
115-
/>
114+
<StyledEnsureChain>
115+
<StyledButton
116+
variant="secondary"
117+
text="Justify & Reveal"
118+
disabled={isSending || isUndefined(disputeDetails)}
119+
isLoading={isSending}
120+
onClick={handleReveal}
121+
/>
122+
</StyledEnsureChain>
116123
</>
117124
) : (
118125
<StyledInfoCard msg="Your vote was successfully commited, please wait until reveal period to reveal it." />

web/src/utils/getVoteChoice.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Answer } from "@kleros/kleros-sdk";
22
import { isUndefined } from ".";
3+
import { RefuseToArbitrateAnswer } from "@kleros/kleros-sdk/src/dataMappings/utils/disputeDetailsSchema";
34

45
export const getVoteChoice = (vote: string, answers: Answer[]) => {
56
// answer.id is hexadecimal number
@@ -8,6 +9,6 @@ export const getVoteChoice = (vote: string, answers: Answer[]) => {
89
if (!isUndefined(selectedAnswer)) {
910
return selectedAnswer.title;
1011
} else {
11-
return `Answer 0x${vote}`;
12+
return BigInt(vote) === BigInt(0) ? RefuseToArbitrateAnswer.title : `Answer 0x${vote}`;
1213
}
1314
};

0 commit comments

Comments
 (0)