|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import styled, { css } from "styled-components"; |
| 3 | + |
| 4 | +import { useNavigate } from "react-router-dom"; |
| 5 | +import { useDebounce } from "react-use"; |
| 6 | + |
| 7 | +import { Button } from "@kleros/ui-components-library"; |
| 8 | + |
| 9 | +import { AliasArray, Answer, useNewDisputeContext } from "context/NewDisputeContext"; |
| 10 | + |
| 11 | +import { useDisputeDetailsQuery } from "queries/useDisputeDetailsQuery"; |
| 12 | +import { usePopulatedDisputeData } from "queries/usePopulatedDisputeData"; |
| 13 | +import { useRoundDetailsQuery } from "queries/useRoundDetailsQuery"; |
| 14 | + |
| 15 | +import { isUndefined } from "src/utils"; |
| 16 | + |
| 17 | +import { landscapeStyle } from "styles/landscapeStyle"; |
| 18 | +import { responsiveSize } from "styles/responsiveSize"; |
| 19 | + |
| 20 | +import { Divider } from "components/Divider"; |
| 21 | +import LabeledInput from "components/LabeledInput"; |
| 22 | + |
| 23 | +import Header from "./Header"; |
| 24 | + |
| 25 | +const Container = styled.div` |
| 26 | + display: flex; |
| 27 | + flex-direction: column; |
| 28 | + gap: 24px; |
| 29 | + align-items: center; |
| 30 | + width: 84vw; |
| 31 | +
|
| 32 | + ${landscapeStyle( |
| 33 | + () => css` |
| 34 | + width: ${responsiveSize(442, 700, 900)}; |
| 35 | +
|
| 36 | + padding-bottom: 240px; |
| 37 | + gap: 48px; |
| 38 | + ` |
| 39 | + )} |
| 40 | +`; |
| 41 | + |
| 42 | +const ErrorMsg = styled.small` |
| 43 | + font-size: 16px; |
| 44 | + color: ${({ theme }) => theme.error}; |
| 45 | +`; |
| 46 | + |
| 47 | +const Landing: React.FC = () => { |
| 48 | + const navigate = useNavigate(); |
| 49 | + |
| 50 | + const [disputeID, setDisputeID] = useState<string>(); |
| 51 | + const [debouncedDisputeID, setDebouncedDisputeID] = useState<string>(); |
| 52 | + const { disputeData, setDisputeData } = useNewDisputeContext(); |
| 53 | + useDebounce(() => setDebouncedDisputeID(disputeID), 500, [disputeID]); |
| 54 | + |
| 55 | + const { data: dispute } = useDisputeDetailsQuery(debouncedDisputeID); |
| 56 | + const { |
| 57 | + data: populatedDispute, |
| 58 | + isError: isErrorPopulatedDisputeQuery, |
| 59 | + isLoading, |
| 60 | + } = usePopulatedDisputeData(debouncedDisputeID, dispute?.dispute?.arbitrated.id as `0x${string}`); |
| 61 | + |
| 62 | + // we want the genesis round's court and numberOfJurors |
| 63 | + const { data: roundData, isError: isErrorRoundQuery } = useRoundDetailsQuery(debouncedDisputeID, 0); |
| 64 | + |
| 65 | + const isInvalidDispute = |
| 66 | + !isLoading && |
| 67 | + !isUndefined(populatedDispute) && |
| 68 | + (isErrorRoundQuery || isErrorPopulatedDisputeQuery || Object.keys(populatedDispute).length === 0); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + if (isUndefined(populatedDispute) || isUndefined(roundData) || isInvalidDispute) return; |
| 72 | + |
| 73 | + const answers = populatedDispute.answers.reduce<Answer[]>((acc, val) => { |
| 74 | + acc.push({ ...val, id: parseInt(val.id, 16).toString() }); |
| 75 | + return acc; |
| 76 | + }, []); |
| 77 | + |
| 78 | + let aliasesArray: AliasArray[] | undefined; |
| 79 | + if (!isUndefined(populatedDispute.aliases)) { |
| 80 | + aliasesArray = Object.entries(populatedDispute.aliases).map(([key, value], index) => ({ |
| 81 | + name: key, |
| 82 | + address: value, |
| 83 | + id: (index + 1).toString(), |
| 84 | + })); |
| 85 | + } |
| 86 | + |
| 87 | + setDisputeData({ |
| 88 | + ...disputeData, |
| 89 | + title: populatedDispute.title, |
| 90 | + description: populatedDispute.description, |
| 91 | + category: populatedDispute.category, |
| 92 | + policyURI: populatedDispute.policyURI, |
| 93 | + question: populatedDispute.question, |
| 94 | + courtId: roundData.round?.court.id, |
| 95 | + numberOfJurors: roundData.round?.nbVotes, |
| 96 | + answers, |
| 97 | + aliasesArray, |
| 98 | + }); |
| 99 | + }, [populatedDispute, roundData, isInvalidDispute]); |
| 100 | + |
| 101 | + const showContinueButton = |
| 102 | + !isUndefined(disputeData) && !isUndefined(populatedDispute) && !isInvalidDispute && !isUndefined(roundData); |
| 103 | + return ( |
| 104 | + <Container> |
| 105 | + <Header text="Create a case" /> |
| 106 | + <Button text="Create from Scratch" onClick={() => navigate("/resolver/title")} /> |
| 107 | + |
| 108 | + <Divider /> |
| 109 | + <LabeledInput |
| 110 | + value={disputeID} |
| 111 | + onChange={(e) => setDisputeID(e.target.value)} |
| 112 | + placeholder="Dispute ID" |
| 113 | + label="Duplicate an exiting case." |
| 114 | + type="number" |
| 115 | + onInput={(e) => { |
| 116 | + const value = e.currentTarget.value.replace(/[^0-9]/g, ""); |
| 117 | + |
| 118 | + e.currentTarget.value = value; |
| 119 | + return e; |
| 120 | + }} |
| 121 | + /> |
| 122 | + {isInvalidDispute ? <ErrorMsg>Error loading dispute data. Please use another dispute.</ErrorMsg> : null} |
| 123 | + {showContinueButton ? <Button small text="Continue" onClick={() => navigate("/resolver/title")} /> : null} |
| 124 | + </Container> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default Landing; |
0 commit comments