Skip to content

Commit

Permalink
feat: Update weighted vote suggestion (#10884)
Browse files Browse the repository at this point in the history
Code suggestion from
#10858



<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->


<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on optimizing the calculation of voting results in the
`WeightedVoteResults` and `Vote` components by replacing manual
accumulation logic with utility functions from `lodash`, enhancing code
readability and maintainability.

### Detailed summary
- In `WeightedVoteResults.tsx`, replaced manual reduction of
`choicesVotes` with `_mergeWith` from `lodash`.
- In `Vote.tsx`, replaced manual construction of `newData` with
`fromPairs` from `lodash` for cleaner code.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
ChefMomota authored Oct 30, 2024
1 parent 2aedd2c commit abf868f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useTranslation } from '@pancakeswap/localization'
import { Box, Flex, Progress, Text } from '@pancakeswap/uikit'
import { formatNumber } from '@pancakeswap/utils/formatBalance'
import _mergeWith from 'lodash/mergeWith'
import { useMemo } from 'react'
import { WeightedVoteState } from 'views/Voting/Proposal/VoteType/types'
import TextEllipsis from '../../components/TextEllipsis'
Expand All @@ -21,13 +22,7 @@ export const WeightedVoteResults: React.FC<WeightedVoteResultsProps> = ({ choice

const percentageResults = useMemo(
() =>
choicesVotes.reduce((acc, item) => {
Object.entries(item).forEach(([key, value]) => {
// eslint-disable-next-line no-param-reassign
acc[key] = (acc[key] || 0) + value
})
return acc
}, {}),
choicesVotes.reduce((acc, item) => _mergeWith(acc, item, (objValue, srcValue) => (objValue || 0) + srcValue), {}),
[choicesVotes],
)

Expand Down
17 changes: 3 additions & 14 deletions apps/web/src/views/Voting/Proposal/Vote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@pancakeswap/uikit'
import { BigNumber } from 'bignumber.js'
import ConnectWalletButton from 'components/ConnectWalletButton'
import fromPairs from 'lodash/fromPairs'
import { useEffect, useMemo, useState } from 'react'
import { Proposal, ProposalState, ProposalTypeName, Vote } from 'state/types'
import { VECAKE_VOTING_POWER_BLOCK } from 'views/Voting/helpers'
Expand Down Expand Up @@ -56,20 +57,8 @@ const VoteComponent: React.FC<React.PropsWithChildren<VoteProps>> = ({
if (type === ProposalTypeName.WEIGHTED && account) {
let newData: null | WeightedVoteState = null
const voteData = votes.find((i) => i.voter.toLowerCase() === account.toLowerCase())

if (voteData) {
newData = choices.reduce((acc, _, index) => {
// eslint-disable-next-line no-param-reassign
acc[index + 1] = voteData?.choice[index + 1] ?? 0
return acc
}, {})
} else {
newData = choices.reduce((acc, _, index) => {
// eslint-disable-next-line no-param-reassign
acc[index + 1] = 0
return acc
}, {})
}
const pairs = choices.map((_, index) => [index + 1, voteData?.choice[index + 1] ?? 0])
newData = fromPairs(pairs)

if (newData) {
setVote(newData)
Expand Down

0 comments on commit abf868f

Please sign in to comment.