Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { useSharedDelegatingTargetReferendumVote } from "next-common/context/referenda/myVote/delegating";
import { useOnchainData } from "next-common/context/post";
import DelegatingVotePanel from "./delegatingPanel";
import { Conviction, isAye } from "next-common/utils/referendumCommon";

export default function DelegatedVotePanel({ delegating }) {
const [targetVote] = useSharedDelegatingTargetReferendumVote();
const { referendumIndex } = useOnchainData();
if (!targetVote || !targetVote.isCasting) {
if (!targetVote || targetVote.type !== "Casting") {
return null;
}
const casting = targetVote.asCasting;
const voteItem = casting.votes.find(
(item) => item[0].toNumber() === referendumIndex,
);
const casting = targetVote.value;
const voteItem = casting.votes.find((item) => item[0] === referendumIndex);
if (!voteItem) {
return null;
}

const vote = voteItem[1];
if (!vote.isStandard) {
if (vote.type !== "Standard") {
return null;
}
const balance = delegating.balance.toString();
const conviction = delegating.conviction.toNumber();
const conviction = Conviction[delegating.conviction?.type] ?? 0;

return (
<DelegatingVotePanel
target={delegating.target.toString()}
balance={balance}
conviction={conviction}
aye={vote.asStandard.vote.isAye}
aye={isAye(vote.value.vote)}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import { memo } from "react";
function CastingVoteInfo({ casting }) {
const { referendumIndex } = useOnchainData();
const votes = casting?.votes || [];
const voteItem = votes.find((item) => item[0].toNumber() === referendumIndex);
const voteItem = votes.find((item) => item[0] === referendumIndex);
if (!voteItem) {
return null;
}

const vote = voteItem[1];
if (vote.isStandard) {
if (vote.type === "Standard") {
return (
<StandardVotePanel
standard={vote.asStandard}
standard={vote.value}
delegations={casting.delegations}
/>
);
} else if (vote.isSplit) {
return <SplitVotePanel split={vote.asSplit} />;
} else if (vote.isSplitAbstain) {
return <SplitAbstainVotePanel splitAbstain={vote.asSplitAbstain} />;
} else if (vote.type === "Split") {
return <SplitVotePanel split={vote.value} />;
} else if (vote.type === "SplitAbstain") {
return <SplitAbstainVotePanel splitAbstain={vote.value} />;
} else {
return null;
}
Expand Down Expand Up @@ -57,9 +57,9 @@ export default function MyVoteOnActiveReferendum() {

if (!voting) {
return null;
} else if (voting.isCasting) {
return <MemoCastingVoteInfo casting={voting.asCasting} />;
} else if (voting.isDelegating) {
return <MemoDelegatingVoteInfo delegating={voting.asDelegating} />;
} else if (voting.type === "Casting") {
return <MemoCastingVoteInfo casting={voting.value} />;
} else if (voting.type === "Delegating") {
return <MemoDelegatingVoteInfo delegating={voting.value} />;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import VotePanel from "./panel";
import { isAye, getConviction } from "next-common/utils/referendumCommon";
import { memo } from "react";

function StandardVotePanel({ standard, delegations }) {
Expand All @@ -9,9 +10,9 @@ function StandardVotePanel({ standard, delegations }) {
const { vote, balance } = standard;
const normalized = {
isStandard: true,
aye: vote.isAye,
aye: isAye(vote),
balance: balance.toString(),
conviction: vote.conviction.toNumber(),
conviction: getConviction(vote),
};

return (
Expand Down
16 changes: 9 additions & 7 deletions packages/next-common/context/referenda/myVote/delegating.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createStateContext } from "react-use";
import { useContextApi } from "next-common/context/api";
import { useContextPapi } from "next-common/context/papi";
import { latestHeightSelector } from "next-common/store/reducers/chainSlice";
import { useSelector } from "react-redux";
import { useEffect } from "react";
Expand All @@ -8,19 +8,21 @@ const [useSharedDelegatingTargetReferendumVote, Provider] =
createStateContext(null);

function DataUpdater({ trackId, address, children }) {
const api = useContextApi();
const { api, checkPallet } = useContextPapi();
const [, setTargetVote] = useSharedDelegatingTargetReferendumVote();
const height = useSelector(latestHeightSelector);

useEffect(() => {
if (!api || !address || !api.query?.convictionVoting?.votingFor) {
if (!api || !address || !checkPallet("ConvictionVoting", "VotingFor")) {
return;
}

api.query.convictionVoting.votingFor(address, trackId).then((voting) => {
setTargetVote(voting);
});
}, [api, address, trackId, height, setTargetVote]);
api.query.ConvictionVoting.VotingFor.getValue(address, trackId).then(
(voting) => {
setTargetVote(voting);
},
);
}, [api, address, trackId, height, setTargetVote, checkPallet]);

return children;
}
Expand Down
12 changes: 7 additions & 5 deletions packages/next-common/context/referenda/myVote/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createStateContext } from "react-use";
import { useContextApi } from "next-common/context/api";
import { useContextPapi } from "next-common/context/papi";
import { useEffect } from "react";
import { latestHeightSelector } from "next-common/store/reducers/chainSlice";
import { useSelector } from "react-redux";
Expand All @@ -8,19 +8,21 @@ import PopupOpenStateProvider from "next-common/context/popup/switch";
const [useSharedMyReferendumVote, Provider] = createStateContext(null);

function DataUpdater({ trackId, address, children }) {
const api = useContextApi();
const { api, checkPallet } = useContextPapi();
const [, setMyVote] = useSharedMyReferendumVote();
const height = useSelector(latestHeightSelector);

useEffect(() => {
if (!api || !address || !api.query?.convictionVoting?.votingFor) {
if (!api || !address || !checkPallet("ConvictionVoting", "VotingFor")) {
return;
}

api.query.convictionVoting.votingFor(address, trackId).then((voting) => {
api.query.ConvictionVoting.VotingFor.getValue(address, trackId, {
at: "best",
}).then((voting) => {
setMyVote(voting);
});
}, [api, address, trackId, height, setMyVote]);
}, [api, address, trackId, height, setMyVote, checkPallet]);

return children;
}
Expand Down
Loading