import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { defineMessages, injectIntl } from 'react-intl'; import _ from 'lodash'; import { Session } from 'meteor/session'; import Button from '/imports/ui/components/button/component'; import LiveResult from './live-result/component'; import { styles } from './styles.scss'; const intlMessages = defineMessages({ pollPaneTitle: { id: 'app.poll.pollPaneTitle', description: 'heading label for the poll menu', }, closeLabel: { id: 'app.poll.closeLabel', description: 'label for poll pane close button', }, hidePollDesc: { id: 'app.poll.hidePollDesc', description: 'aria label description for hide poll button', }, customPollLabel: { id: 'app.poll.customPollLabel', description: 'label for custom poll button', }, startCustomLabel: { id: 'app.poll.startCustomLabel', description: 'label for button to start custom poll', }, customPollInstruction: { id: 'app.poll.customPollInstruction', description: 'instructions for using custom poll', }, quickPollInstruction: { id: 'app.poll.quickPollInstruction', description: 'instructions for using pre configured polls', }, activePollInstruction: { id: 'app.poll.activePollInstruction', description: 'instructions displayed when a poll is active', }, customPlaceholder: { id: 'app.poll.customPlaceholder', description: 'custom poll input field placeholder text', }, tf: { id: 'app.poll.tf', description: 'label for true / false poll', }, yn: { id: 'app.poll.yn', description: 'label for Yes / No poll', }, a2: { id: 'app.poll.a2', description: 'label for A / B poll', }, a3: { id: 'app.poll.a3', description: 'label for A / B / C poll', }, a4: { id: 'app.poll.a4', description: 'label for A / B / C / D poll', }, a5: { id: 'app.poll.a5', description: 'label for A / B / C / D / E poll', }, }); const MAX_CUSTOM_FIELDS = Meteor.settings.public.poll.max_custom; class Poll extends Component { constructor(props) { super(props); this.state = { customPollReq: false, isPolling: false, customPollValues: [], }; this.inputEditor = []; this.toggleCustomFields = this.toggleCustomFields.bind(this); this.renderQuickPollBtns = this.renderQuickPollBtns.bind(this); this.renderCustomView = this.renderCustomView.bind(this); this.renderInputFields = this.renderInputFields.bind(this); this.handleInputChange = this.handleInputChange.bind(this); this.handleBackClick = this.handleBackClick.bind(this); } handleInputChange(index, event) { // This regex will replace any instance of 2 or more consecutive white spaces // with a single white space character. const option = event.target.value.replace(/\s{2,}/g, ' ').trim(); this.inputEditor[index] = option === '' ? '' : option; this.setState({ customPollValues: this.inputEditor }); } renderInputFields() { const { intl } = this.props; const items = []; items = _.range(1, MAX_CUSTOM_FIELDS + 1).map((ele, index) => ( this.handleInputChange(index, event)} defaultValue={this.state.customPollValues[index]} /> )); return items; } toggleCustomFields() { const { customPollReq } = this.state; this.inputEditor = []; return this.setState({ customPollReq: !customPollReq }); } renderQuickPollBtns() { const { pollTypes, startPoll, intl } = this.props; const btns = pollTypes.map((type) => { if (type === 'custom') return; const label = intl.formatMessage( // regex removes the - to match the message id intlMessages[type.replace(/-/g, '').toLowerCase()]); return (