Skip to content

Commit

Permalink
fix: fix vote button being incorrectly disabled (#1122)
Browse files Browse the repository at this point in the history
This was caused by returning undefined from react query to signal that
the user has no existing vote, but returning undefined is disallowed by
react query.
  • Loading branch information
emccorson committed Sep 18, 2024
1 parent c6b3c0f commit 9856bb9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions apps/namadillo/src/App/Governance/ProposalHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ const ProgressBar: React.FC<{

const VoteButton: React.FC<{
proposal: AtomWithQueryResult<Proposal>;
vote: AtomWithQueryResult<VoteType | undefined>;
vote: AtomWithQueryResult<VoteType | null>;
proposalId: bigint;
}> = ({ proposal, vote, proposalId }) => {
const navigate = useNavigate();
Expand All @@ -317,7 +317,7 @@ const VoteButton: React.FC<{
const disabled =
!isExtensionConnected || !canVote.data || status !== "ongoing";

const voted = typeof vote.data !== "undefined";
const voted = vote.data !== null;
const text = voted ? "Edit Vote" : "Vote";

return {
Expand All @@ -344,9 +344,9 @@ const VoteButton: React.FC<{
};

const VotedLabel: React.FC<{
vote: AtomWithQueryResult<VoteType | undefined>;
vote: AtomWithQueryResult<VoteType | null>;
}> = ({ vote }) => {
if (vote.isSuccess && typeof vote.data !== "undefined") {
if (vote.isSuccess && vote.data !== null) {
return (
<VotedLabelComponent vote={vote.data} className="text-xs min-w-22" />
);
Expand Down
6 changes: 4 additions & 2 deletions apps/namadillo/src/atoms/proposals/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const proposalFamily = atomFamily((id: bigint) =>
);

export const proposalVoteFamily = atomFamily((id: bigint) =>
atomWithQuery<VoteType | undefined>((get) => {
atomWithQuery<VoteType | null>((get) => {
const votedProposals = get(votedProposalsAtom);
const enablePolling = get(shouldUpdateProposalAtom);

Expand All @@ -47,7 +47,9 @@ export const proposalVoteFamily = atomFamily((id: bigint) =>
refetchInterval: enablePolling ? 1000 : false,
queryKey: ["proposal-vote", id.toString()],
...queryDependentFn(async () => {
return votedProposals.data!.find((v) => v.proposalId === id)?.vote;
return (
votedProposals.data!.find((v) => v.proposalId === id)?.vote ?? null
);
}, [votedProposals]),
};
})
Expand Down

1 comment on commit 9856bb9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.