// @flow
import React, { useCallback } from 'react';
import { View, Text, FlatList, TouchableOpacity } from 'react-native';
import AbstractPollResults from '../AbstractPollResults';
import type { AbstractProps, AnswerInfo } from '../AbstractPollResults';
import { chatStyles, dialogStyles, resultsStyles } from './styles';
/**
* Component that renders the poll results.
*
* @param {Props} props - The passed props.
* @returns {React.Node}
*/
const PollResults = (props: AbstractProps) => {
const {
answers,
changeVote,
haveVoted,
showDetails,
question,
t,
toggleIsDetailed
} = props;
/* eslint-disable react/no-multi-comp */
/**
* Render a header summing up answer information.
*
* @param {string} answer - The name of the answer.
* @param {number} percentage - The percentage of voters.
* @param {number} nbVotes - The number of collected votes.
* @returns {React.Node}
*/
const renderHeader = (answer: string, percentage: number, nbVotes: number) => (
{ answer }
({nbVotes}) {percentage}%
{/* { answer } - { percentage }%
{ t('polls.answer.vote', { count: nbVotes }) }
*/}
);
/**
* Render voters of and answer.
*
* @param {AnswerInfo} answer - The answer info.
* @returns {React.Node}
*/
const renderRow = useCallback((answer: AnswerInfo) => {
const { name, percentage, voters, voterCount } = answer;
if (showDetails) {
return (
{ renderHeader(name, percentage, voterCount) }
{ voters && voterCount > 0
&&
{voters.map(({ id, name: voterName }) =>
{ voterName }
)}
}
);
}
// else, we display a simple list
// We add a progress bar by creating an empty view of width equal to percentage.
return (
{ renderHeader(answer.name, percentage, voterCount) }
);
}, [ showDetails ]);
/* eslint-disable react/jsx-no-bind */
return (
{ question }
index.toString() }
renderItem = { answer => renderRow(answer.item) } />
{showDetails ? t('polls.results.hideDetailedResults') : t('polls.results.showDetailedResults')}
{haveVoted ? t('polls.results.changeVote') : t('polls.results.vote')}
);
};
/*
* We apply AbstractPollResults to fill in the AbstractProps common
* to both the web and native implementations.
*/
// eslint-disable-next-line new-cap
export default AbstractPollResults(PollResults);