UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

55 lines (54 loc) 2.88 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { styled } from '@mui/material/styles'; import { useSCContext, useSCUser } from '@selfcommunity/react-core'; import { Box, Card, Typography } from '@mui/material'; import { FormattedMessage } from 'react-intl'; import Icon from '@mui/material/Icon'; import LinearProgress from '@mui/material/LinearProgress'; import { LoadingButton } from '@mui/lab'; import classNames from 'classnames'; import { PREFIX } from '../../constants'; const classes = { root: `${PREFIX}-poll-object-choices-root`, label: `${PREFIX}-poll-object-choices-label`, vote: `${PREFIX}-poll-object-choices-vote`, progress: `${PREFIX}-poll-object-choices-progress` }; const Root = styled(Card, { name: PREFIX, slot: 'PollObjectChoicesRoot' })(() => ({})); function LinearProgressWithLabel(_a) { var { className } = _a, props = __rest(_a, ["className"]); return (_jsxs(Box, Object.assign({ className: className }, { children: [_jsx(LinearProgress, Object.assign({ variant: "determinate" }, props)), _jsx(Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: `${Math.round(props.value)}%` }))] }))); } export default function Choice(props) { //PROPS const { className = null, choiceObj = null, feedObject = null, vote = null, votes = null, isVoting = null, votable = null } = props, rest = __rest(props, ["className", "choiceObj", "feedObject", "vote", "votes", "isVoting", "votable"]); const disabled = !feedObject; // CONTEXT const scContext = useSCContext(); const scUserContext = useSCUser(); const handleVoteAction = () => { if (!scUserContext.user) { scContext.settings.handleAnonymousAction(); } else { vote(choiceObj); } }; /** * Renders total votes in percentage */ function renderVotes(voteCount, totalVotes) { if (totalVotes === 0) { return 0; } return (100 * voteCount) / totalVotes; } /** * Renders root element */ return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsx(Typography, Object.assign({ className: classes.label }, { children: choiceObj.choice })), _jsx(LinearProgressWithLabel, { className: classes.progress, value: renderVotes(choiceObj.vote_count, votes) }), _jsx(LoadingButton, Object.assign({ loading: isVoting === choiceObj.id, variant: choiceObj.voted ? 'contained' : 'outlined', size: "small", disabled: disabled || isVoting !== null || votable, className: classes.vote, onClick: handleVoteAction }, { children: choiceObj.voted ? (_jsx(Icon, { children: "check" })) : (_jsx(FormattedMessage, { id: "ui.feedObject.poll.choice.vote", defaultMessage: "ui.feedObject.poll.choice.vote" })) }))] }))); }