|
@ -1,4 +1,4 @@ |
|
|
import React, { useMemo } from 'react'; |
|
|
import React, { useEffect, useMemo, useState } from 'react'; |
|
|
import { Grid, Statistic, Tab } from 'semantic-ui-react'; |
|
|
import { Grid, Statistic, Tab } from 'semantic-ui-react'; |
|
|
import PropTypes from 'prop-types'; |
|
|
import PropTypes from 'prop-types'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
@ -7,16 +7,23 @@ import PollChartDonut from './PollChartDonut'; |
|
|
import './styles.css'; |
|
|
import './styles.css'; |
|
|
|
|
|
|
|
|
const PollGraph = (props) => { |
|
|
const PollGraph = (props) => { |
|
|
const { pollOptions, voteCounts, userVoteIndex } = props; |
|
|
const { pollOptions, userVoteIndex, voteCounts, voterNames } = props; |
|
|
|
|
|
const [totalVotes, setTotalVotes] = useState(voteCounts.reduce((accumulator, voteCount) => accumulator + voteCount, 0)); |
|
|
const { t } = useTranslation(); |
|
|
const { t } = useTranslation(); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
setTotalVotes(voteCounts.reduce((accumulator, voteCount) => accumulator + voteCount, 0)); |
|
|
|
|
|
}, [voteCounts]); |
|
|
|
|
|
|
|
|
const footer = useMemo(() => ( |
|
|
const footer = useMemo(() => ( |
|
|
<> |
|
|
<> |
|
|
<Statistic size="mini"> |
|
|
<Statistic size="mini"> |
|
|
<Statistic.Value> |
|
|
<Statistic.Value> |
|
|
{voteCounts.reduce((accumulator, voteCount) => accumulator + voteCount, 0)} |
|
|
{ totalVotes } |
|
|
</Statistic.Value> |
|
|
</Statistic.Value> |
|
|
<Statistic.Label>{t('topic.poll.tab.results.votes')}</Statistic.Label> |
|
|
<Statistic.Label> |
|
|
|
|
|
{ totalVotes !== 1 ? t('topic.poll.tab.results.votes') : t('topic.poll.tab.results.vote') } |
|
|
|
|
|
</Statistic.Label> |
|
|
</Statistic> |
|
|
</Statistic> |
|
|
{userVoteIndex !== -1 |
|
|
{userVoteIndex !== -1 |
|
|
&& ( |
|
|
&& ( |
|
@ -26,7 +33,7 @@ const PollGraph = (props) => { |
|
|
</div> |
|
|
</div> |
|
|
)} |
|
|
)} |
|
|
</> |
|
|
</> |
|
|
), [pollOptions, t, userVoteIndex, voteCounts]); |
|
|
), [pollOptions, t, totalVotes, userVoteIndex]); |
|
|
|
|
|
|
|
|
const panes = useMemo(() => { |
|
|
const panes = useMemo(() => { |
|
|
const chartBarPane = ( |
|
|
const chartBarPane = ( |
|
@ -38,6 +45,7 @@ const PollGraph = (props) => { |
|
|
<PollChartBar |
|
|
<PollChartBar |
|
|
pollOptions={pollOptions} |
|
|
pollOptions={pollOptions} |
|
|
voteCounts={voteCounts} |
|
|
voteCounts={voteCounts} |
|
|
|
|
|
voterNames={voterNames} |
|
|
/> |
|
|
/> |
|
|
</Grid.Column> |
|
|
</Grid.Column> |
|
|
<Grid.Column /> |
|
|
<Grid.Column /> |
|
@ -59,6 +67,7 @@ const PollGraph = (props) => { |
|
|
<PollChartDonut |
|
|
<PollChartDonut |
|
|
pollOptions={pollOptions} |
|
|
pollOptions={pollOptions} |
|
|
voteCounts={voteCounts} |
|
|
voteCounts={voteCounts} |
|
|
|
|
|
voterNames={voterNames} |
|
|
/> |
|
|
/> |
|
|
</Grid.Column> |
|
|
</Grid.Column> |
|
|
<Grid.Column /> |
|
|
<Grid.Column /> |
|
@ -76,7 +85,7 @@ const PollGraph = (props) => { |
|
|
{ menuItem: { key: 'chart-bar', icon: 'chart bar' }, render: () => chartBarPane }, |
|
|
{ menuItem: { key: 'chart-bar', icon: 'chart bar' }, render: () => chartBarPane }, |
|
|
{ menuItem: { key: 'chart-donut', icon: 'chart pie' }, render: () => chartDonutPane }, |
|
|
{ menuItem: { key: 'chart-donut', icon: 'chart pie' }, render: () => chartDonutPane }, |
|
|
]); |
|
|
]); |
|
|
}, [footer, pollOptions, voteCounts]); |
|
|
}, [footer, pollOptions, voteCounts, voterNames]); |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<Tab |
|
|
<Tab |
|
@ -93,6 +102,7 @@ PollGraph.defaultProps = { |
|
|
PollGraph.propTypes = { |
|
|
PollGraph.propTypes = { |
|
|
pollOptions: PropTypes.arrayOf(PropTypes.string).isRequired, |
|
|
pollOptions: PropTypes.arrayOf(PropTypes.string).isRequired, |
|
|
voteCounts: PropTypes.arrayOf(PropTypes.number).isRequired, |
|
|
voteCounts: PropTypes.arrayOf(PropTypes.number).isRequired, |
|
|
|
|
|
voterNames: PropTypes.arrayOf(PropTypes.array).isRequired, |
|
|
userVoteIndex: PropTypes.number, |
|
|
userVoteIndex: PropTypes.number, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|