|
1 |
| -import React, { useMemo } from "react"; |
| 1 | +import React, { useMemo, useState } from "react"; |
2 | 2 | import styled, { css } from "styled-components";
|
3 | 3 |
|
4 |
| -import { AlertMessage, DropdownCascader, DropdownSelect } from "@kleros/ui-components-library"; |
| 4 | +import { AlertMessage, Checkbox, DropdownCascader, DropdownSelect, Field } from "@kleros/ui-components-library"; |
5 | 5 |
|
6 |
| -import { useNewDisputeContext } from "context/NewDisputeContext"; |
| 6 | +import { DisputeKits } from "consts/index"; |
| 7 | +import { IGatedDisputeData, useNewDisputeContext } from "context/NewDisputeContext"; |
7 | 8 | import { rootCourtToItems, useCourtTree } from "hooks/queries/useCourtTree";
|
8 | 9 | import { useDisputeKitAddressesAll } from "hooks/useDisputeKitAddresses";
|
9 | 10 | import { isUndefined } from "utils/index";
|
@@ -62,56 +63,149 @@ const StyledDropdownSelect = styled(DropdownSelect)`
|
62 | 63 | )}
|
63 | 64 | `;
|
64 | 65 |
|
| 66 | +const StyledField = styled(Field)` |
| 67 | + width: 84vw; |
| 68 | + margin-top: 24px; |
| 69 | + ${landscapeStyle( |
| 70 | + () => css` |
| 71 | + width: ${responsiveSize(442, 700, 900)}; |
| 72 | + ` |
| 73 | + )} |
| 74 | + > small { |
| 75 | + margin-top: 16px; |
| 76 | + } |
| 77 | +`; |
| 78 | + |
| 79 | +const StyledCheckbox = styled(Checkbox)` |
| 80 | + width: 84vw; |
| 81 | + margin-top: 24px; |
| 82 | + ${landscapeStyle( |
| 83 | + () => css` |
| 84 | + width: ${responsiveSize(442, 700, 900)}; |
| 85 | + ` |
| 86 | + )} |
| 87 | +`; |
| 88 | + |
65 | 89 | const Court: React.FC = () => {
|
66 | 90 | const { disputeData, setDisputeData } = useNewDisputeContext();
|
| 91 | + const [isGatedDisputeKit, setIsGatedDisputeKit] = useState(false); |
67 | 92 | const { data: courtTree } = useCourtTree();
|
68 | 93 | const { data: supportedDisputeKits } = useSupportedDisputeKits(disputeData.courtId);
|
69 | 94 | const items = useMemo(() => !isUndefined(courtTree?.court) && [rootCourtToItems(courtTree.court)], [courtTree]);
|
70 | 95 | const { availableDisputeKits } = useDisputeKitAddressesAll();
|
| 96 | + |
71 | 97 | const disputeKitOptions = useMemo(() => {
|
72 | 98 | return (
|
73 |
| - supportedDisputeKits?.court?.supportedDisputeKits.map((dk) => ({ |
74 |
| - text: availableDisputeKits[dk.address.toLowerCase()] ?? "", |
75 |
| - value: dk.id, |
76 |
| - })) || [] |
| 99 | + supportedDisputeKits?.court?.supportedDisputeKits.map((dk) => { |
| 100 | + const text = availableDisputeKits[dk.address.toLowerCase()] ?? ""; |
| 101 | + return { |
| 102 | + text, |
| 103 | + value: dk.id, |
| 104 | + gated: text === DisputeKits.Gated || text === DisputeKits.GatedShutter, |
| 105 | + }; |
| 106 | + }) || [] |
77 | 107 | );
|
78 | 108 | }, [supportedDisputeKits, availableDisputeKits]);
|
79 | 109 |
|
80 |
| - const defaultDisputeKitValue = useMemo(() => { |
| 110 | + const selectedDisputeKitId = useMemo(() => { |
| 111 | + // If there's only 1 supported dispute kit, select it by default |
81 | 112 | if (disputeKitOptions.length === 1) {
|
82 |
| - return disputeKitOptions[0].value; // If there's only 1 supported dispute kit, select it by default |
| 113 | + return disputeKitOptions[0].value; |
83 | 114 | }
|
84 |
| - return disputeData.disputeKitId ?? 1; |
| 115 | + // If there's no saved selection, select nothing |
| 116 | + return disputeData.disputeKitId ?? -1; |
85 | 117 | }, [disputeKitOptions, disputeData.disputeKitId]);
|
86 | 118 |
|
87 |
| - const handleCourtWrite = (courtId: string) => { |
88 |
| - setDisputeData({ ...disputeData, courtId, disputeKitId: undefined }); |
| 119 | + const handleCourtChange = (courtId: string) => { |
| 120 | + if (disputeData.courtId !== courtId) { |
| 121 | + setDisputeData({ ...disputeData, courtId, disputeKitId: undefined }); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + const handleDisputeKitChange = (newValue: string | number) => { |
| 126 | + const options = disputeKitOptions.find((dk) => dk.value === String(newValue)); |
| 127 | + const isNewValueGated = options?.gated ?? false; |
| 128 | + const gatedDisputeKitData: IGatedDisputeData | undefined = isNewValueGated |
| 129 | + ? { |
| 130 | + type: "gated", |
| 131 | + tokenGate: "", |
| 132 | + isERC1155: false, |
| 133 | + tokenId: "0", |
| 134 | + } |
| 135 | + : undefined; |
| 136 | + setIsGatedDisputeKit(isNewValueGated); |
| 137 | + setDisputeData({ ...disputeData, disputeKitId: Number(newValue), disputeKitData: gatedDisputeKitData }); |
| 138 | + }; |
| 139 | + |
| 140 | + const handleTokenAddressChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 141 | + const currentData = disputeData.disputeKitData as IGatedDisputeData; |
| 142 | + setDisputeData({ |
| 143 | + ...disputeData, |
| 144 | + disputeKitData: { ...currentData, tokenGate: event.target.value }, |
| 145 | + }); |
89 | 146 | };
|
90 | 147 |
|
91 |
| - const handleDisputeKitChange = (newValue: string | number) => |
92 |
| - setDisputeData({ ...disputeData, disputeKitId: Number(newValue) }); |
| 148 | + const handleERC1155TokenChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 149 | + const currentData = disputeData.disputeKitData as IGatedDisputeData; |
| 150 | + setDisputeData({ |
| 151 | + ...disputeData, |
| 152 | + disputeKitData: { ...currentData, isERC1155: event.target.checked }, |
| 153 | + }); |
| 154 | + }; |
| 155 | + |
| 156 | + const handleTokenIdChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 157 | + const currentData = disputeData.disputeKitData as IGatedDisputeData; |
| 158 | + setDisputeData({ |
| 159 | + ...disputeData, |
| 160 | + disputeKitData: { ...currentData, tokenId: event.target.value }, |
| 161 | + }); |
| 162 | + }; |
93 | 163 |
|
94 | 164 | return (
|
95 | 165 | <Container>
|
96 | 166 | <Header text="Select a court to arbitrate the case" />
|
97 | 167 | {items ? (
|
98 | 168 | <StyledDropdownCascader
|
99 | 169 | items={items}
|
100 |
| - onSelect={(path: string | number) => typeof path === "string" && handleCourtWrite(path.split("/").pop()!)} |
| 170 | + onSelect={(path: string | number) => typeof path === "string" && handleCourtChange(path.split("/").pop()!)} |
101 | 171 | placeholder="Select Court"
|
102 | 172 | value={`/courts/${disputeData.courtId}`}
|
103 | 173 | />
|
104 | 174 | ) : (
|
105 | 175 | <StyledSkeleton width={240} height={42} />
|
106 | 176 | )}
|
107 |
| - {disputeData?.courtId && ( |
| 177 | + {disputeData?.courtId && disputeKitOptions.length > 0 && ( |
108 | 178 | <StyledDropdownSelect
|
109 | 179 | items={disputeKitOptions}
|
110 | 180 | placeholder={{ text: "Select Dispute Kit" }}
|
111 |
| - defaultValue={defaultDisputeKitValue} |
| 181 | + defaultValue={selectedDisputeKitId} |
112 | 182 | callback={handleDisputeKitChange}
|
113 | 183 | />
|
114 | 184 | )}
|
| 185 | + {isGatedDisputeKit && ( |
| 186 | + <> |
| 187 | + <StyledField |
| 188 | + dir="auto" |
| 189 | + onChange={handleTokenAddressChange} |
| 190 | + value={(disputeData.disputeKitData as IGatedDisputeData)?.tokenGate ?? ""} |
| 191 | + placeholder="Eg. 0xda10009cbd5d07dd0cecc66161fc93d7c9000da1" |
| 192 | + /> |
| 193 | + <StyledCheckbox |
| 194 | + onChange={handleERC1155TokenChange} |
| 195 | + checked={(disputeData.disputeKitData as IGatedDisputeData)?.isERC1155 ?? false} |
| 196 | + label="ERC-1155 token" |
| 197 | + small={true} |
| 198 | + /> |
| 199 | + {(disputeData.disputeKitData as IGatedDisputeData)?.isERC1155 && ( |
| 200 | + <StyledField |
| 201 | + dir="auto" |
| 202 | + onChange={handleTokenIdChange} |
| 203 | + value={(disputeData.disputeKitData as IGatedDisputeData)?.tokenId ?? "0"} |
| 204 | + placeholder="Eg. 1" |
| 205 | + /> |
| 206 | + )} |
| 207 | + </> |
| 208 | + )} |
115 | 209 | <AlertMessageContainer>
|
116 | 210 | <AlertMessage
|
117 | 211 | title="Check the courts available beforehand"
|
|
0 commit comments