Skip to content

Commit

Permalink
Updated poll multi-choice votes counter
Browse files Browse the repository at this point in the history
  • Loading branch information
dkildar committed Jun 7, 2024
1 parent 2c0502c commit bffa20f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/common/features/polls/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./get-poll-details-query";
export * from "./sign-poll-vote";
export * from "./polls-votes-management";
65 changes: 65 additions & 0 deletions src/common/features/polls/api/polls-votes-management.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { GetPollDetailsQueryResponse } from "./get-poll-details-query";
import { ActiveUser } from "../../../store/active-user/types";

export namespace PollsVotesManagement {
export function processVoting(
activeUser: ActiveUser | null,
data: GetPollDetailsQueryResponse,
choiceNums: number[]
): GetPollDetailsQueryResponse {
const existingVotes = data.poll_voters?.filter((pv) => pv.name === activeUser!!.username);
const existingUserChoices = data.poll_choices?.filter(
(pc) => !!existingVotes?.some((ev) => ev.choice_num === pc.choice_num)
);
const currentUserChoices = data.poll_choices?.filter((pc) =>
choiceNums.includes(pc.choice_num)
);

const notTouchedChoices = data.poll_choices?.filter(
(pc) =>
![
...existingUserChoices?.map((puc) => puc.choice_num),
...currentUserChoices?.map((c) => c.choice_num)
].includes(pc.choice_num)
);
const nonActiveUserVotes =
data.poll_voters?.filter((pv) => pv.name !== activeUser!!.username) ?? [];

return {
...data,
poll_choices: [
...notTouchedChoices,

...(existingUserChoices
.filter((choice) => currentUserChoices.every((c) => choice.choice_text !== c.choice_text))
.map((pv) => ({
...pv,
votes: {
hive_hp_incl_proxied: pv.votes?.hive_hp_incl_proxied!,
total_votes: (pv?.votes?.total_votes ?? 0) - 1
}
})) ?? []),

...currentUserChoices.map((choice) => ({
...choice,
votes: {
hive_hp_incl_proxied: choice.votes?.hive_hp_incl_proxied!,
total_votes:
(choice?.votes?.total_votes ?? 0) +
(existingUserChoices.every((pv) => pv.choice_text !== choice.choice_text) ? 1 : 0)
}
}))
],
poll_voters: [
...nonActiveUserVotes,
...choiceNums.map((num) => ({ name: activeUser!.username, choice_num: num }))
],
poll_stats: {
...data.poll_stats,
total_voting_accounts_num:
data.poll_stats.total_voting_accounts_num +
(currentUserChoices.length - (existingVotes?.length ?? 0))
}
};
}
}
52 changes: 2 additions & 50 deletions src/common/features/polls/api/sign-poll-vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { _t } from "../../../i18n";
import { useMappedStore } from "../../../store/use-mapped-store";
import { broadcastPostingJSON } from "../../../api/operations";
import { QueryIdentifiers } from "../../../core";
import { PollsVotesManagement } from "./polls-votes-management";

export function useSignPollVoteByKey(poll: ReturnType<typeof useGetPollDetailsQuery>["data"]) {
const { activeUser } = useMappedStore();
Expand Down Expand Up @@ -42,56 +43,7 @@ export function useSignPollVoteByKey(poll: ReturnType<typeof useGetPollDetailsQu
return data;
}

const existingVotes = data.poll_voters?.filter((pv) => pv.name === activeUser!!.username);
const previousUserChoices = data.poll_choices?.filter((pc) =>
existingVotes?.some((ev) => ev.choice_num === pc.choice_num)
);
const choices = data.poll_choices?.filter((pc) => !!resp.choiceNums[pc.choice_num]);

const notTouchedChoices = data.poll_choices?.filter(
(pc) =>
![
...previousUserChoices?.map((puc) => puc.choice_num),
choices?.map((c) => c.choice_num)
].includes(pc.choice_num)
);
const otherVoters =
data.poll_voters?.filter((pv) => pv.name !== activeUser!!.username) ?? [];

return {
...data,
poll_choices: [
...notTouchedChoices,
...(previousUserChoices
.filter((pv) => choices.every((c) => pv.choice_text !== c.choice_text))
.map((pv) => ({
...pv,
votes: {
total_votes: (pv?.votes?.total_votes ?? 0) - 1
}
})) ?? []),
...choices.map((choice) => ({
...choice,
votes: {
total_votes:
(choice?.votes?.total_votes ?? 0) +
(previousUserChoices.every((pv) => pv.choice_text !== choice.choice_text)
? 1
: 0)
}
}))
].filter((el) => !!el),
poll_voters: [
...otherVoters,
...resp.choiceNums.map((num) => ({ name: activeUser?.username, choice_num: num }))
],
poll_stats: {
...data.poll_stats,
total_voting_accounts_num:
data.poll_stats.total_voting_accounts_num +
(choices.length - (existingVotes?.length ?? 0))
}
} as ReturnType<typeof useGetPollDetailsQuery>["data"];
return PollsVotesManagement.processVoting(activeUser, data, resp.choiceNums);
}
)
});
Expand Down
7 changes: 6 additions & 1 deletion src/common/features/polls/components/poll-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export function PollWidget({ poll, isReadOnly, entry }: Props) {
{_t("polls.account-age-hint", { n: poll.filters.accountAge })}
</div>
)}
{!resultsMode && (
<div className="text-sm text-gray-600 dark:text-gray-400">
{_t("polls.max-votes-hint", { n: poll.maxChoicesVoted ?? 1 })}
</div>
)}
<div className="flex flex-col gap-3">
{poll.choices.map((choice) =>
resultsMode ? (
Expand Down Expand Up @@ -173,7 +178,7 @@ export function PollWidget({ poll, isReadOnly, entry }: Props) {
</div>
{showVote && (
<Button
disabled={isReadOnly || !activeChoices || isVoting}
disabled={isReadOnly || activeChoices.size === 0 || isVoting}
icon={<UilPanelAdd />}
iconPlacement="left"
size="lg"
Expand Down
3 changes: 2 additions & 1 deletion src/common/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,7 @@
"interpretation": "Interpretation",
"creating-in-progress": "Creating in progress...",
"invalid-time": "Invalid time format. Use HH:MM",
"max-choices-voted": "Max choices voted by user"
"max-choices-voted": "Max choices voted by user",
"max-votes-hint": "You may select {{n}} choices"
}
}

0 comments on commit bffa20f

Please sign in to comment.