Skip to content

Commit

Permalink
Merge pull request #2038 from zeitgeistpm/staging
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Dec 4, 2023
2 parents 5ec1ba3 + d2f4ca9 commit 2f965fa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
11 changes: 8 additions & 3 deletions components/court/CourtVoteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import { HiOutlineDocumentDownload } from "react-icons/hi";
export type CourtVoteFormProps = {
caseId: number;
market: FullMarketFragment;
onVote?: () => void;
};

export const CourtVoteForm: React.FC<CourtVoteFormProps> = ({
caseId,
market,
onVote,
}) => {
const [sdk, id] = useSdkv2();
const queryClient = useQueryClient();
Expand Down Expand Up @@ -65,6 +67,7 @@ export const CourtVoteForm: React.FC<CourtVoteFormProps> = ({
commitVote();
queryClient.invalidateQueries([id, voteDrawsRootKey, caseId]);
queryClient.invalidateQueries([id, voteDrawsRootKey, "all"]);
onVote?.();
},
},
);
Expand All @@ -82,7 +85,7 @@ export const CourtVoteForm: React.FC<CourtVoteFormProps> = ({
<div className="mb-8 mt-6">
<MarketContextActionOutcomeSelector
market={market}
selected={vote}
selected={vote ?? outcomeAssets[0]}
options={outcomeAssets}
disabled={committed}
onChange={(assetId) => {
Expand Down Expand Up @@ -142,8 +145,10 @@ export const CourtVoteForm: React.FC<CourtVoteFormProps> = ({
<code className="mb-4 block text-xs">
<span>
vote_item = VoteItem::Outcome(OutcomeReport::Categorical(
{vote.CategoricalOutcome[1]})) {"->"}{" "}
{market.categories?.[vote.CategoricalOutcome[1]].ticker}
{vote?.CategoricalOutcome[1] ?? "null"})) {"->"}{" "}
{vote
? market.categories?.[vote.CategoricalOutcome[1]]?.ticker
: "--"}
</span>
<br />
<span className="text-black">salt</span> ={" "}
Expand Down
4 changes: 2 additions & 2 deletions components/court/CourtVoteRevealForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const CourtVoteRevealForm: React.FC<CourtVoteRevealFormProps> = ({

const { send, isReady, isLoading, isBroadcasting } = useExtrinsic(
() => {
if (isRpcSdk(sdk) && salt) {
if (isRpcSdk(sdk) && salt && vote) {
return sdk.api.tx.court.revealVote(
caseId,
{
Expand Down Expand Up @@ -128,7 +128,7 @@ export const CourtVoteRevealForm: React.FC<CourtVoteRevealFormProps> = ({
<div className="mb-8 mt-6">
<MarketContextActionOutcomeSelector
market={market}
selected={vote}
selected={vote ?? outcomeAssets[0]}
options={outcomeAssets}
onChange={onChangeSelectedOutcome}
disabled={committed}
Expand Down
16 changes: 14 additions & 2 deletions lib/state/court/useVoteOutcome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import { useWallet } from "../wallet";
import { useAtom } from "jotai";

export type UseVourtVote = {
vote: CategoricalAssetId;
vote?: CategoricalAssetId;
committed: boolean;
setVote: (assetId: CategoricalAssetId) => void;
commitVote: () => void;
unCommitVote: () => void;
};

export type UseCourtVoteProps = {
caseId: number;
marketId: number;
defaultValue: CategoricalAssetId;
defaultValue?: CategoricalAssetId;
};

const courtVotesAtom = persistentAtom<
Expand Down Expand Up @@ -70,10 +71,21 @@ export const useCourtVote = ({
}));
};

const unCommitVote = () => {
setCourtVotes((prev) => ({
...prev,
[id]: {
...prev[id],
committed: false,
},
}));
};

return {
vote: vote?.assetId ?? defaultValue,
committed: vote?.committed ?? false,
setVote,
commitVote,
unCommitVote,
};
};
45 changes: 39 additions & 6 deletions pages/court/[caseid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import { useRouter } from "next/router";
import { NextPage } from "next/types";
import NotFoundPage from "pages/404";
import { IGetPlaiceholderReturn, getPlaiceholder } from "plaiceholder";
import { useMemo } from "react";
import { useMemo, useState } from "react";
import { AiOutlineEye } from "react-icons/ai";
import { LuVote } from "react-icons/lu";
import { LuReplace, LuVote } from "react-icons/lu";
import { PiBooks } from "react-icons/pi";
import {
DAY_SECONDS,
Expand All @@ -43,6 +43,8 @@ import {
} from "lib/constants";
import { CourtAppealForm } from "components/court/CourtAppealForm";
import { CourtDocsArticle } from "components/court/learn/CourtDocsArticle";
import { useCourtVote } from "lib/state/court/useVoteOutcome";
import { useConfirmation } from "lib/state/confirm-modal/useConfirmation";

const QuillViewer = dynamic(() => import("../../components/ui/QuillViewer"), {
ssr: false,
Expand Down Expand Up @@ -160,28 +162,59 @@ const CasePage: NextPage = ({
}
}, [time, market, courtCase]);

const { prompt } = useConfirmation();
const [recastVoteEnabled, setRecastVoteEnabled] = useState(false);

const { unCommitVote } = useCourtVote({
caseId,
marketId: market.marketId,
});

const onClickRecastVote = async () => {
if (
await prompt({
title: "Recast Vote",
description: "Are you sure you want to recast your vote?",
})
) {
unCommitVote();
setRecastVoteEnabled(true);
}
};

const onVote = () => {
setRecastVoteEnabled(false);
};

const actionSection = (
<>
{stage?.type === "vote" && (
<>
{isDrawnJuror && (
{(isDrawnJuror || recastVoteEnabled) && (
<>
<CourtVoteForm market={market} caseId={caseId} />
<CourtVoteForm market={market} caseId={caseId} onVote={onVote} />
</>
)}

{hasSecretVote && (
{hasSecretVote && !recastVoteEnabled && (
<div className="overflow-hidden rounded-xl px-6 py-6 shadow-lg">
<div className="flex flex-col items-center gap-3">
<div className="text-blue-500">
<LuVote size={64} />
</div>
<h3 className="text mb-2 text-blue-500">You have voted</h3>
<p className="text-center text-sm text-gray-500">
<p className="mb-3 text-center text-sm text-gray-500">
Your vote is secret during voting, but when court goes into
aggregation you can reveal your vote to the public by coming
back to this page.
</p>
<button
className="center gap-3 rounded-md bg-blue-500 px-4 py-2 text-white"
onClick={onClickRecastVote}
>
Recast Vote
<LuReplace size={14} />
</button>
</div>
</div>
)}
Expand Down

0 comments on commit 2f965fa

Please sign in to comment.