@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
58 lines (57 loc) • 3.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const styles_1 = require("@mui/material/styles");
const react_core_1 = require("@selfcommunity/react-core");
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const LinearProgress_1 = tslib_1.__importDefault(require("@mui/material/LinearProgress"));
const lab_1 = require("@mui/lab");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const constants_1 = require("../../constants");
const classes = {
root: `${constants_1.PREFIX}-poll-object-choices-root`,
label: `${constants_1.PREFIX}-poll-object-choices-label`,
vote: `${constants_1.PREFIX}-poll-object-choices-vote`,
progress: `${constants_1.PREFIX}-poll-object-choices-progress`
};
const Root = (0, styles_1.styled)(material_1.Card, {
name: constants_1.PREFIX,
slot: 'PollObjectChoicesRoot'
})(() => ({}));
function LinearProgressWithLabel(_a) {
var { className } = _a, props = tslib_1.__rest(_a, ["className"]);
return ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: className }, { children: [(0, jsx_runtime_1.jsx)(LinearProgress_1.default, Object.assign({ variant: "determinate" }, props)), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", color: "text.secondary" }, { children: `${Math.round(props.value)}%` }))] })));
}
function Choice(props) {
//PROPS
const { className = null, choiceObj = null, feedObject = null, vote = null, votes = null, isVoting = null, votable = null } = props, rest = tslib_1.__rest(props, ["className", "choiceObj", "feedObject", "vote", "votes", "isVoting", "votable"]);
const disabled = !feedObject;
// CONTEXT
const scContext = (0, react_core_1.useSCContext)();
const scUserContext = (0, react_core_1.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 ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.label }, { children: choiceObj.choice })), (0, jsx_runtime_1.jsx)(LinearProgressWithLabel, { className: classes.progress, value: renderVotes(choiceObj.vote_count, votes) }), (0, jsx_runtime_1.jsx)(lab_1.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 ? ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: "check" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedObject.poll.choice.vote", defaultMessage: "ui.feedObject.poll.choice.vote" })) }))] })));
}
exports.default = Choice;