This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1585 from ecency/feature/polls
Feature/polls
- Loading branch information
Showing
39 changed files
with
1,060 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { QueryIdentifiers } from "../../../core"; | ||
import { Entry } from "../../../store/entries/types"; | ||
import axios from "axios"; | ||
|
||
interface GetPollDetailsQueryResponse { | ||
author: string; | ||
created: string; | ||
end_time: string; | ||
filter_account_age_days: number; | ||
image: string; | ||
parent_permlink: string; | ||
permlink: string; | ||
platform: null; | ||
poll_choices: { choice_num: number; choice_text: string; votes?: { total_votes: number } }[]; | ||
poll_stats: { total_voting_accounts_num: number }; | ||
poll_trx_id: string; | ||
poll_voters?: { name: string; choice_num: number }[]; | ||
post_body: string; | ||
post_title: string; | ||
preferred_interpretation: string; | ||
protocol_version: number; | ||
question: string; | ||
status: string; | ||
tags: string[]; | ||
token: null; | ||
} | ||
|
||
export function useGetPollDetailsQuery(entry?: Entry) { | ||
return useQuery<GetPollDetailsQueryResponse>( | ||
[QueryIdentifiers.POLL_DETAILS, entry?.author, entry?.permlink], | ||
{ | ||
queryFn: () => | ||
axios | ||
.get( | ||
`https://polls.ecency.com/rpc/poll?author=eq.${entry!!.author}&permlink=eq.${ | ||
entry!!.permlink | ||
}` | ||
) | ||
.then((resp) => resp.data[0]), | ||
enabled: !!entry, | ||
refetchOnMount: false | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./get-poll-details-query"; | ||
export * from "./sign-poll-vote"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { useGetPollDetailsQuery } from "./get-poll-details-query"; | ||
import { error } from "../../../components/feedback"; | ||
import { _t } from "../../../i18n"; | ||
import { useMappedStore } from "../../../store/use-mapped-store"; | ||
import { broadcastPostingJSON } from "../../../api/operations"; | ||
import { QueryIdentifiers } from "../../../core"; | ||
|
||
export function useSignPollVoteByKey(poll: ReturnType<typeof useGetPollDetailsQuery>["data"]) { | ||
const { activeUser } = useMappedStore(); | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationKey: ["sign-poll-vote", poll?.author, poll?.permlink], | ||
mutationFn: async ({ choice }: { choice: string }) => { | ||
if (!poll || !activeUser) { | ||
error(_t("polls.not-found")); | ||
return; | ||
} | ||
|
||
const choiceNum = poll.poll_choices?.find((pc) => pc.choice_text === choice)?.choice_num; | ||
if (typeof choiceNum !== "number") { | ||
error(_t("polls.not-found")); | ||
return; | ||
} | ||
|
||
await broadcastPostingJSON(activeUser.username, "polls", { | ||
poll: poll.poll_trx_id, | ||
action: "vote", | ||
choice: choiceNum | ||
}); | ||
|
||
return { choiceNum }; | ||
}, | ||
onSuccess: (resp) => | ||
queryClient.setQueryData<ReturnType<typeof useGetPollDetailsQuery>["data"]>( | ||
[QueryIdentifiers.POLL_DETAILS, poll?.author, poll?.permlink], | ||
(data) => { | ||
if (!data || !resp) { | ||
return data; | ||
} | ||
|
||
const existingVote = data.poll_voters?.find((pv) => pv.name === activeUser!!.username); | ||
const previousUserChoice = data.poll_choices?.find( | ||
(pc) => existingVote?.choice_num === pc.choice_num | ||
); | ||
const choice = data.poll_choices?.find((pc) => pc.choice_num === resp.choiceNum)!!; | ||
|
||
const notTouchedChoices = data.poll_choices?.filter( | ||
(pc) => ![previousUserChoice?.choice_num, choice?.choice_num].includes(pc.choice_num) | ||
); | ||
const otherVoters = | ||
data.poll_voters?.filter((pv) => pv.name !== activeUser!!.username) ?? []; | ||
|
||
return { | ||
...data, | ||
poll_choices: [ | ||
...notTouchedChoices, | ||
previousUserChoice | ||
? { | ||
...previousUserChoice, | ||
votes: { | ||
total_votes: (previousUserChoice?.votes?.total_votes ?? 0) - 1 | ||
} | ||
} | ||
: undefined, | ||
{ | ||
...choice, | ||
votes: { | ||
total_votes: (choice?.votes?.total_votes ?? 0) + 1 | ||
} | ||
} | ||
].filter((el) => !!el), | ||
poll_voters: [ | ||
...otherVoters, | ||
{ name: activeUser?.username, choice_num: resp.choiceNum } | ||
] | ||
} as ReturnType<typeof useGetPollDetailsQuery>["data"]; | ||
} | ||
) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./polls-creation"; | ||
export * from "./poll-widget"; |
53 changes: 53 additions & 0 deletions
53
src/common/features/polls/components/poll-option-with-results.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { classNameObject } from "../../../helper/class-name-object"; | ||
import React, { useMemo } from "react"; | ||
import { PollCheck } from "./poll-option"; | ||
import { useGetPollDetailsQuery } from "../api"; | ||
import { Entry } from "../../../store/entries/types"; | ||
import { _t } from "../../../i18n"; | ||
|
||
export interface Props { | ||
activeChoice?: string; | ||
choice: string; | ||
entry?: Entry; | ||
} | ||
|
||
export function PollOptionWithResults({ choice, activeChoice, entry }: Props) { | ||
const pollDetails = useGetPollDetailsQuery(entry); | ||
|
||
const votesCount = useMemo( | ||
() => | ||
pollDetails.data?.poll_choices.find((pc) => pc.choice_text === choice)?.votes?.total_votes ?? | ||
0, | ||
[choice, pollDetails.data?.poll_choices] | ||
); | ||
const totalVotes = useMemo( | ||
() => Math.max(pollDetails.data?.poll_stats.total_voting_accounts_num ?? 0, 1), | ||
[pollDetails.data?.poll_stats.total_voting_accounts_num] | ||
); | ||
|
||
return ( | ||
<div | ||
className={classNameObject({ | ||
"min-h-[52px] relative overflow-hidden flex items-center gap-4 duration-300 cursor-pointer text-sm px-4 py-3 rounded-2xl": | ||
true, | ||
"bg-gray-200 dark:bg-dark-200": true | ||
})} | ||
> | ||
<div | ||
className={classNameObject({ | ||
"bg-blue-dark-sky bg-opacity-50 min-h-[52px] absolute top-0 left-0 bottom-0": true | ||
})} | ||
style={{ | ||
width: `${((votesCount * 100) / totalVotes).toFixed(2)}%` | ||
}} | ||
/> | ||
{activeChoice === choice && <PollCheck checked={activeChoice === choice} />} | ||
<div className="flex w-full gap-2 justify-between"> | ||
<span>{choice}</span> | ||
<span className="text-xs whitespace-nowrap"> | ||
{((votesCount * 100) / totalVotes).toFixed(2)}% ({votesCount} {_t("polls.votes")}) | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.