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 Button from '/imports/ui/components/button/component';
|
2018-10-10 23:49:58 +08:00
|
|
|
import { styles } from './styles';
|
2018-11-02 02:20:35 +08:00
|
|
|
import Service from './service';
|
2018-10-10 23:49:58 +08:00
|
|
|
|
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 {
|
2019-01-17 02:25:24 +08:00
|
|
|
static getDerivedStateFromProps(nextProps) {
|
|
|
|
const { currentPoll, getUser } = nextProps;
|
2018-10-31 03:18:32 +08:00
|
|
|
|
|
|
|
if (!currentPoll) return null;
|
|
|
|
|
2019-01-17 02:25:24 +08:00
|
|
|
const {
|
|
|
|
answers, responses, users, numRespondents,
|
|
|
|
} = currentPoll;
|
2018-10-31 03:18:32 +08:00
|
|
|
|
|
|
|
let userAnswers = responses
|
|
|
|
? [...users, ...responses.map(u => u.userId)]
|
|
|
|
: [...users];
|
|
|
|
|
|
|
|
userAnswers = userAnswers.map(id => getUser(id))
|
|
|
|
.filter(user => user.connectionStatus === 'online')
|
|
|
|
.map((user) => {
|
|
|
|
let answer = '-';
|
|
|
|
|
|
|
|
if (responses) {
|
|
|
|
const response = responses.find(r => r.userId === user.userId);
|
|
|
|
if (response) answer = answers[response.answerId].key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: user.name,
|
|
|
|
answer,
|
|
|
|
};
|
|
|
|
})
|
2018-11-02 02:20:35 +08:00
|
|
|
.sort(Service.sortUsers)
|
2018-10-31 03:18:32 +08:00
|
|
|
.reduce((acc, user) => [
|
|
|
|
...acc,
|
|
|
|
<div className={styles.item} key={_.uniqueId('stats-')}>{user.name}</div>,
|
|
|
|
<div className={styles.itemR} key={_.uniqueId('stats-')}>{user.answer}</div>,
|
|
|
|
], []);
|
|
|
|
|
2018-10-10 23:49:58 +08:00
|
|
|
const pollStats = [];
|
|
|
|
|
2018-10-31 21:28:32 +08:00
|
|
|
answers.map((obj) => {
|
|
|
|
const pct = Math.round(obj.numVotes / numRespondents * 100);
|
2019-04-15 04:23:14 +08:00
|
|
|
const pctFotmatted = `${Number.isNaN(pct) ? 0 : pct}%`;
|
|
|
|
|
|
|
|
const calculatedWidth = {
|
|
|
|
width: pctFotmatted,
|
|
|
|
};
|
2018-10-31 21:28:32 +08:00
|
|
|
|
2018-12-18 23:15:51 +08:00
|
|
|
return pollStats.push(
|
|
|
|
<div className={styles.main} key={_.uniqueId('stats-')}>
|
|
|
|
<div className={styles.left}>
|
|
|
|
{obj.key}
|
|
|
|
</div>
|
|
|
|
<div className={styles.center}>
|
2019-04-15 04:23:14 +08:00
|
|
|
<div className={styles.barShade} style={calculatedWidth} />
|
|
|
|
<div className={styles.barVal}>{obj.numVotes || 0}</div>
|
2018-12-18 23:15:51 +08:00
|
|
|
</div>
|
|
|
|
<div className={styles.right}>
|
2019-04-15 04:23:14 +08:00
|
|
|
{pctFotmatted}
|
2018-12-18 23:15:51 +08:00
|
|
|
</div>
|
|
|
|
</div>,
|
|
|
|
);
|
2018-10-31 21:28:32 +08:00
|
|
|
});
|
2018-10-10 23:49:58 +08:00
|
|
|
|
2019-01-17 02:25:24 +08:00
|
|
|
return {
|
|
|
|
userAnswers,
|
|
|
|
pollStats,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
userAnswers: null,
|
|
|
|
pollStats: null,
|
|
|
|
};
|
2018-10-10 23:49:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-24 22:17:13 +08:00
|
|
|
const {
|
2019-01-17 02:25:24 +08:00
|
|
|
intl, publishPoll, stopPoll, handleBackClick, currentPoll,
|
2018-10-24 22:17:13 +08:00
|
|
|
} = this.props;
|
2018-10-16 03:05:50 +08:00
|
|
|
|
2019-01-17 02:25:24 +08:00
|
|
|
const { userAnswers, pollStats } = this.state;
|
|
|
|
|
2019-04-15 04:23:14 +08:00
|
|
|
const namesList = [];
|
|
|
|
const answersList = [];
|
|
|
|
if (typeof userAnswers === 'object' && userAnswers) {
|
|
|
|
Object.keys(userAnswers).forEach((key) => {
|
|
|
|
userAnswers[key].props.className.includes('itemR')
|
|
|
|
? answersList.push(userAnswers[key])
|
|
|
|
: namesList.push(userAnswers[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-10 23:49:58 +08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className={styles.stats}>
|
2019-01-17 02:25:24 +08:00
|
|
|
{pollStats}
|
2018-10-10 23:49:58 +08:00
|
|
|
</div>
|
2019-01-17 02:25:24 +08:00
|
|
|
{currentPoll
|
|
|
|
? (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
publishPoll();
|
|
|
|
stopPoll();
|
|
|
|
}}
|
|
|
|
label={intl.formatMessage(intlMessages.publishLabel)}
|
|
|
|
color="primary"
|
|
|
|
className={styles.btn}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
handleBackClick();
|
|
|
|
}}
|
|
|
|
label={intl.formatMessage(intlMessages.backLabel)}
|
|
|
|
color="default"
|
|
|
|
className={styles.btn}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2018-10-10 23:49:58 +08:00
|
|
|
<div className={styles.container}>
|
2019-04-15 04:23:14 +08:00
|
|
|
<div className={styles.usersWrapper}>
|
|
|
|
<h3 className={styles.usersHeading}>
|
|
|
|
{intl.formatMessage(intlMessages.usersTitle)}
|
|
|
|
</h3>
|
|
|
|
{namesList}
|
|
|
|
</div>
|
|
|
|
<div className={styles.answersWrapper}>
|
|
|
|
<h3 className={styles.responseHeading}>
|
|
|
|
{intl.formatMessage(intlMessages.responsesTitle)}
|
|
|
|
</h3>
|
|
|
|
{answersList}
|
|
|
|
</div>
|
2018-10-10 23:49:58 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:05:50 +08:00
|
|
|
export default injectIntl(LiveResult);
|
2018-10-16 03:58:07 +08:00
|
|
|
|
2019-01-17 02:25:24 +08:00
|
|
|
LiveResult.defaultProps = { currentPoll: null };
|
|
|
|
|
2018-10-16 03:58:07 +08:00
|
|
|
LiveResult.propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
2019-01-17 02:25:24 +08:00
|
|
|
currentPoll: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(Object),
|
|
|
|
PropTypes.shape({
|
|
|
|
answers: PropTypes.arrayOf(PropTypes.object),
|
|
|
|
users: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
}),
|
|
|
|
]),
|
2018-12-18 23:15:51 +08:00
|
|
|
publishPoll: PropTypes.func.isRequired,
|
|
|
|
stopPoll: PropTypes.func.isRequired,
|
|
|
|
handleBackClick: PropTypes.func.isRequired,
|
2018-10-16 03:58:07 +08:00
|
|
|
};
|