2018-10-10 23:49:58 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-10-16 03:58:07 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2018-10-10 23:49:58 +08:00
|
|
|
import _ from 'lodash';
|
2018-10-16 03:05:50 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2018-10-24 22:17:13 +08:00
|
|
|
import { Session } from 'meteor/session';
|
|
|
|
import Button from '/imports/ui/components/button/component';
|
2018-10-10 23:49:58 +08:00
|
|
|
import { styles } from './styles';
|
|
|
|
|
2018-10-16 03:05:50 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
usersTitle: {
|
|
|
|
id: 'app.poll.liveResult.usersTitle',
|
|
|
|
description: 'heading label for poll users',
|
|
|
|
},
|
|
|
|
responsesTitle: {
|
|
|
|
id: 'app.poll.liveResult.responsesTitle',
|
|
|
|
description: 'heading label for poll responses',
|
|
|
|
},
|
2018-10-24 22:17:13 +08:00
|
|
|
publishLabel: {
|
|
|
|
id: 'app.poll.publishLabel',
|
|
|
|
description: 'label for the publish button',
|
|
|
|
},
|
|
|
|
backLabel: {
|
|
|
|
id: 'app.poll.backLabel',
|
|
|
|
description: 'label for the return to poll options button',
|
|
|
|
},
|
2018-10-16 03:05:50 +08:00
|
|
|
});
|
|
|
|
|
2018-10-10 23:49:58 +08:00
|
|
|
class LiveResult extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.getUnresponsive = this.getUnresponsive.bind(this);
|
|
|
|
this.getRespondents = this.getRespondents.bind(this);
|
|
|
|
this.getPollStats = this.getPollStats.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPollStats() {
|
|
|
|
const { currentPoll } = this.props;
|
|
|
|
|
|
|
|
const pollStats = [];
|
|
|
|
|
|
|
|
if (currentPoll) {
|
|
|
|
const {
|
|
|
|
answers,
|
|
|
|
numRespondents,
|
|
|
|
} = currentPoll;
|
|
|
|
|
|
|
|
if (answers) {
|
|
|
|
answers.map((obj) => {
|
|
|
|
const pct = Math.round(obj.numVotes / numRespondents * 100);
|
|
|
|
|
|
|
|
return pollStats.push(<div className={styles.main} key={_.uniqueId('stats-')}>
|
|
|
|
<div className={styles.left}>
|
|
|
|
{obj.key}
|
|
|
|
</div>
|
|
|
|
<div className={styles.center}>
|
|
|
|
{obj.numVotes}
|
|
|
|
</div>
|
|
|
|
<div className={styles.right}>
|
|
|
|
{`${isNaN(pct) ? 0 : pct}%`}
|
|
|
|
</div>
|
|
|
|
</div>);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pollStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRespondents() {
|
|
|
|
const { currentPoll, getUser } = this.props;
|
|
|
|
|
|
|
|
if (!currentPoll) return null;
|
|
|
|
|
|
|
|
const respondedUsers = [];
|
|
|
|
|
|
|
|
if (currentPoll) {
|
|
|
|
const {
|
|
|
|
answers,
|
|
|
|
responses,
|
|
|
|
} = currentPoll;
|
|
|
|
|
|
|
|
if (responses && answers) {
|
2018-10-30 21:17:40 +08:00
|
|
|
responses.forEach((ur) => {
|
2018-10-10 23:49:58 +08:00
|
|
|
const user = getUser(ur.userId);
|
|
|
|
if (user) {
|
2018-10-30 21:17:40 +08:00
|
|
|
answers.forEach((obj) => {
|
2018-10-10 23:49:58 +08:00
|
|
|
if (obj.id === ur.answerId) {
|
|
|
|
respondedUsers.push(<div className={styles.item} key={_.uniqueId('stats-')}>{user.name}</div>);
|
|
|
|
respondedUsers.push(<div className={styles.itemR} key={_.uniqueId('stats-')}>{obj.key}</div>);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return respondedUsers;
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:17:40 +08:00
|
|
|
|
2018-10-10 23:49:58 +08:00
|
|
|
getUnresponsive() {
|
|
|
|
const { currentPoll, getUser } = this.props;
|
|
|
|
|
|
|
|
if (!currentPoll) return null;
|
|
|
|
|
|
|
|
const {
|
|
|
|
users,
|
|
|
|
} = currentPoll;
|
|
|
|
|
|
|
|
const usersToRespond = [];
|
|
|
|
|
|
|
|
const usersList = _.compact(users);
|
|
|
|
|
|
|
|
if (usersList) {
|
2018-10-30 21:17:40 +08:00
|
|
|
usersList.forEach((userId) => {
|
2018-10-10 23:49:58 +08:00
|
|
|
const user = getUser(userId);
|
2018-10-26 01:20:32 +08:00
|
|
|
if (user && user.connectionStatus == 'online') {
|
2018-10-10 23:49:58 +08:00
|
|
|
usersToRespond.push(<div className={styles.item} key={_.uniqueId('stats-')}>{user.name}</div>);
|
2018-10-17 01:31:41 +08:00
|
|
|
usersToRespond.push(<div className={styles.itemR} key={_.uniqueId('stats-')}>-</div>);
|
2018-10-10 23:49:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return usersToRespond;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-24 22:17:13 +08:00
|
|
|
const {
|
2018-10-30 21:17:40 +08:00
|
|
|
intl, publishPoll, stopPoll, handleBackClick,
|
2018-10-24 22:17:13 +08:00
|
|
|
} = this.props;
|
2018-10-16 03:05:50 +08:00
|
|
|
|
2018-10-10 23:49:58 +08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className={styles.stats}>
|
|
|
|
{this.getPollStats()}
|
|
|
|
</div>
|
2018-10-24 22:17:13 +08:00
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
publishPoll();
|
|
|
|
stopPoll();
|
|
|
|
Session.set('isUserListOpen', true);
|
|
|
|
Session.set('isPollOpen', false);
|
|
|
|
Session.set('forcePollOpen', false);
|
|
|
|
}}
|
|
|
|
label={intl.formatMessage(intlMessages.publishLabel)}
|
|
|
|
color="primary"
|
|
|
|
className={styles.btn}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
2018-10-30 21:17:40 +08:00
|
|
|
handleBackClick();
|
2018-10-24 22:17:13 +08:00
|
|
|
}}
|
|
|
|
label={intl.formatMessage(intlMessages.backLabel)}
|
|
|
|
color="default"
|
|
|
|
className={styles.btn}
|
|
|
|
/>
|
2018-10-10 23:49:58 +08:00
|
|
|
<div className={styles.container}>
|
2018-10-16 03:05:50 +08:00
|
|
|
<div className={styles.usersHeading}>{intl.formatMessage(intlMessages.usersTitle)}</div>
|
|
|
|
<div className={styles.responseHeading}>{intl.formatMessage(intlMessages.responsesTitle)}</div>
|
2018-10-10 23:49:58 +08:00
|
|
|
{this.getRespondents()}
|
|
|
|
{this.getUnresponsive()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:05:50 +08:00
|
|
|
export default injectIntl(LiveResult);
|
2018-10-16 03:58:07 +08:00
|
|
|
|
|
|
|
LiveResult.propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
};
|