bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/polling/component.jsx

232 lines
6.7 KiB
React
Raw Normal View History

2018-10-24 22:17:13 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-06-30 06:00:06 +08:00
import Button from '/imports/ui/components/button/component';
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
import { defineMessages, injectIntl } from 'react-intl';
import cx from 'classnames';
2018-01-08 14:17:18 +08:00
import { styles } from './styles.scss';
import AudioService from '/imports/ui/components/audio/service';
2020-12-01 00:09:35 +08:00
import {Meteor} from "meteor/meteor";
2016-05-06 02:50:18 +08:00
2017-04-25 10:08:18 +08:00
const intlMessages = defineMessages({
pollingTitleLabel: {
id: 'app.polling.pollingTitle',
},
pollAnswerLabel: {
id: 'app.polling.pollAnswerLabel',
},
pollAnswerDesc: {
id: 'app.polling.pollAnswerDesc',
2017-04-25 10:08:18 +08:00
},
2021-02-05 01:55:18 +08:00
pollSendLabel: {
id: 'app.polling.pollSendLabel',
},
2017-04-25 10:08:18 +08:00
});
2018-10-24 22:17:13 +08:00
class Polling extends Component {
constructor(props) {
super(props);
2021-02-05 01:55:18 +08:00
this.state = {
checkedAnswers: []
}
2018-10-24 22:17:13 +08:00
this.play = this.play.bind(this);
2021-02-05 01:55:18 +08:00
this.renderButtonAnswers = this.renderButtonAnswers.bind(this);
this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.renderCheckboxAnswers = this.renderCheckboxAnswers.bind(this);
2018-10-24 22:17:13 +08:00
}
componentDidMount() {
this.play();
}
play() {
AudioService.playAlertSound(`${Meteor.settings.public.app.cdn
2020-12-01 00:09:35 +08:00
+ Meteor.settings.public.app.basename
+ Meteor.settings.public.app.instanceId}`
+ '/resources/sounds/Poll.mp3');
2018-10-24 22:17:13 +08:00
}
2021-02-05 01:55:18 +08:00
renderButtonAnswers() {
const {
isMeteorConnected,
intl,
poll,
handleVote,
pollAnswerIds,
} = this.props;
return poll.answers.map((pollAnswer) => {
const formattedMessageIndex = pollAnswer.key.toLowerCase();
let label = pollAnswer.key;
if (pollAnswerIds[formattedMessageIndex]) {
label = intl.formatMessage(pollAnswerIds[formattedMessageIndex]);
}
return (
<div
key={pollAnswer.id}
className={styles.pollButtonWrapper}
>
<Button
disabled={!isMeteorConnected}
className={styles.pollingButton}
color="primary"
size="md"
label={label}
key={pollAnswer.key}
onClick={() => handleVote(poll.pollId, [pollAnswer.id])}
aria-labelledby={`pollAnswerLabel${pollAnswer.key}`}
aria-describedby={`pollAnswerDesc${pollAnswer.key}`}
/>
<div
className={styles.hidden}
id={`pollAnswerLabel${pollAnswer.key}`}
>
{intl.formatMessage(intlMessages.pollAnswerLabel, { 0: label })}
</div>
<div
className={styles.hidden}
id={`pollAnswerDesc${pollAnswer.key}`}
>
{intl.formatMessage(intlMessages.pollAnswerDesc, { 0: label })}
</div>
</div>
);
})
}
handleCheckboxChange(pollId, answerId) {
const {checkedAnswers} = this.state
if (checkedAnswers.includes(answerId)) {
checkedAnswers.splice(checkedAnswers.indexOf(answerId), 1)
} else {
checkedAnswers.push(answerId)
}
checkedAnswers.sort();
}
handleSubmit(pollId) {
const {handleVote} = this.props;
const {checkedAnswers} = this.state
handleVote(pollId, checkedAnswers)
}
renderCheckboxAnswers() {
const {
isMeteorConnected,
intl,
poll,
pollAnswerIds,
} = this.props;
return (
<div>
<div>
{poll.answers.map((pollAnswer) => {
const formattedMessageIndex = pollAnswer.key.toLowerCase();
let label = pollAnswer.key;
if (pollAnswerIds[formattedMessageIndex]) {
label = intl.formatMessage(pollAnswerIds[formattedMessageIndex]);
}
return (
<div
key={pollAnswer.id}
>
<input
type="checkbox"
disabled={!isMeteorConnected}
id={`answerInput${pollAnswer.key}`}
onChange={() => this.handleCheckboxChange(poll.pollId, pollAnswer.id)}
aria-labelledby={`pollAnswerLabel${pollAnswer.key}`}
aria-describedby={`pollAnswerDesc${pollAnswer.key}`}
/>
<label htmlFor={`answerInput${pollAnswer.key}`}>
{label}
</label>
<div
className={styles.hidden}
id={`pollAnswerLabel${pollAnswer.key}`}
>
{intl.formatMessage(intlMessages.pollAnswerLabel, { 0: label })}
</div>
<div
className={styles.hidden}
id={`pollAnswerDesc${pollAnswer.key}`}
>
{intl.formatMessage(intlMessages.pollAnswerDesc, { 0: label })}
</div>
</div>
);
})}
</div>
<div>
<Button
disabled={!isMeteorConnected}
className={styles.pollingButton}
color="primary"
size="md"
label={intl.formatMessage(intlMessages.pollSendLabel)}
onClick={() => this.handleSubmit(poll.pollId)}
/>
</div>
</div>
)
}
2018-10-24 22:17:13 +08:00
render() {
2019-05-23 02:00:44 +08:00
const {
isMeteorConnected,
intl,
poll,
handleVote,
pollAnswerIds,
2019-05-23 02:00:44 +08:00
} = this.props;
if (!poll) return null;
const { stackOptions, answers } = poll;
const pollAnswerStyles = {
[styles.pollingAnswers]: true,
[styles.removeColumns]: answers.length === 1,
[styles.stacked]: stackOptions,
};
2018-10-24 22:17:13 +08:00
return (
<div className={styles.overlay}>
<div
className={cx({
[styles.pollingContainer]: true,
[styles.autoWidth]: stackOptions,
})}
role="alert"
>
<div className={styles.pollingTitle}>
{intl.formatMessage(intlMessages.pollingTitleLabel)}
</div>
<div className={cx(pollAnswerStyles)}>
2021-02-05 01:55:18 +08:00
{poll.isMultipleChoice ? this.renderCheckboxAnswers() : this.renderButtonAnswers()}
</div>
2018-10-24 22:17:13 +08:00
</div>
</div>);
}
}
2017-04-25 10:08:18 +08:00
export default injectIntl(injectWbResizeEvent(Polling));
Polling.propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
handleVote: PropTypes.func.isRequired,
poll: PropTypes.shape({
pollId: PropTypes.string.isRequired,
2018-03-13 00:29:22 +08:00
answers: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number.isRequired,
key: PropTypes.string.isRequired,
}).isRequired).isRequired,
}).isRequired,
};