2022-06-15 12:59:17 -07:00
import React from 'react' ;
import { defineMessages , FormattedMessage , useIntl } from 'react-intl' ;
import { fetchPoll , vote } from 'soapbox/actions/polls' ;
import { useAppDispatch } from 'soapbox/hooks' ;
2022-08-31 15:01:58 -07:00
import RelativeTimestamp from '../relative-timestamp' ;
2022-08-08 03:48:42 -07:00
import { Button , HStack , Stack , Text , Tooltip } from '../ui' ;
2022-06-15 12:59:17 -07:00
import type { Selected } from './poll' ;
import type { Poll as PollEntity } from 'soapbox/types/entities' ;
const messages = defineMessages ( {
closed : { id : 'poll.closed' , defaultMessage : 'Closed' } ,
2022-08-08 03:48:42 -07:00
nonAnonymous : { id : 'poll.non_anonymous.label' , defaultMessage : 'Other instances may display the options you voted for' } ,
2022-06-15 12:59:17 -07:00
} ) ;
interface IPollFooter {
2023-02-15 13:26:27 -08:00
poll : PollEntity
showResults : boolean
selected : Selected
2022-06-15 12:59:17 -07:00
}
const PollFooter : React.FC < IPollFooter > = ( { poll , showResults , selected } ) : JSX . Element = > {
const dispatch = useAppDispatch ( ) ;
const intl = useIntl ( ) ;
const handleVote = ( ) = > dispatch ( vote ( poll . id , Object . keys ( selected ) ) ) ;
const handleRefresh : React.EventHandler < React.MouseEvent > = ( e ) = > {
dispatch ( fetchPoll ( poll . id ) ) ;
e . stopPropagation ( ) ;
e . preventDefault ( ) ;
} ;
const timeRemaining = poll . expired ?
intl . formatMessage ( messages . closed ) :
< RelativeTimestamp weight = 'medium' timestamp = { poll . expires_at } futureDate / > ;
2022-07-22 14:48:27 -07:00
let votesCount = null ;
if ( poll . voters_count !== null && poll . voters_count !== undefined ) {
votesCount = < FormattedMessage id = 'poll.total_people' defaultMessage = '{count, plural, one {# person} other {# people}}' values = { { count : poll.get ( 'voters_count' ) } } / > ;
} else {
votesCount = < FormattedMessage id = 'poll.total_votes' defaultMessage = '{count, plural, one {# vote} other {# votes}}' values = { { count : poll.get ( 'votes_count' ) } } / > ;
}
2022-06-15 12:59:17 -07:00
return (
2022-06-16 06:20:33 -07:00
< Stack space = { 4 } data-testid = 'poll-footer' >
2022-06-15 12:59:17 -07:00
{ ( ! showResults && poll ? . multiple ) && (
< Button onClick = { handleVote } theme = 'primary' block >
< FormattedMessage id = 'poll.vote' defaultMessage = 'Vote' / >
< / Button >
) }
2022-09-05 06:18:15 -07:00
< HStack space = { 1.5 } alignItems = 'center' wrap >
2022-08-08 03:48:42 -07:00
{ poll . pleroma . get ( 'non_anonymous' ) && (
< >
< Tooltip text = { intl . formatMessage ( messages . nonAnonymous ) } >
< Text theme = 'muted' weight = 'medium' >
< FormattedMessage id = 'poll.non_anonymous' defaultMessage = 'Public poll' / >
< / Text >
< / Tooltip >
< Text theme = 'muted' > & middot ; < / Text >
< / >
) }
2022-06-15 12:59:17 -07:00
{ showResults && (
< >
2022-06-16 08:40:58 -07:00
< button className = 'text-gray-600 underline' onClick = { handleRefresh } data-testid = 'poll-refresh' >
2022-06-15 12:59:17 -07:00
< Text theme = 'muted' weight = 'medium' >
< FormattedMessage id = 'poll.refresh' defaultMessage = 'Refresh' / >
< / Text >
< / button >
< Text theme = 'muted' > & middot ; < / Text >
< / >
) }
< Text theme = 'muted' weight = 'medium' >
2022-07-22 14:48:27 -07:00
{ votesCount }
2022-06-15 12:59:17 -07:00
< / Text >
{ poll . expires_at && (
< >
< Text theme = 'muted' > & middot ; < / Text >
2022-06-16 06:20:33 -07:00
< Text weight = 'medium' theme = 'muted' data-testid = 'poll-expiration' > { timeRemaining } < / Text >
2022-06-15 12:59:17 -07:00
< / >
) }
< / HStack >
< / Stack >
) ;
} ;
export default PollFooter ;