2020-08-29 01:23:27 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Modal from '/imports/ui/components/modal/simple/component';
|
|
|
|
import Button from '/imports/ui/components/button/component';
|
2021-04-15 23:12:10 +08:00
|
|
|
import AudioService from '/imports/ui/components/audio/service';
|
2020-08-29 01:23:27 +08:00
|
|
|
import { styles } from './styles';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
noViewers: {
|
|
|
|
id: 'app.modal.randomUser.noViewers.description',
|
|
|
|
description: 'Label displayed when no viewers are avaiable',
|
|
|
|
},
|
|
|
|
selected: {
|
|
|
|
id: 'app.modal.randomUser.selected.description',
|
|
|
|
description: 'Label shown to the selected user',
|
|
|
|
},
|
|
|
|
randUserTitle: {
|
|
|
|
id: 'app.modal.randomUser.title',
|
|
|
|
description: 'Modal title label',
|
|
|
|
},
|
2021-04-15 23:12:10 +08:00
|
|
|
whollbeSelected: {
|
|
|
|
id: 'app.modal.randomUser.who',
|
|
|
|
description: 'Label shown during the selection',
|
|
|
|
},
|
|
|
|
onlyOneViewerTobeSelected: {
|
|
|
|
id: 'app.modal.randomUser.alone',
|
|
|
|
description: 'Label shown when only one viewer to be selected',
|
|
|
|
},
|
2020-08-29 01:23:27 +08:00
|
|
|
reselect: {
|
|
|
|
id: 'app.modal.randomUser.reselect.label',
|
|
|
|
description: 'select new random user button label',
|
|
|
|
},
|
|
|
|
ariaModalTitle: {
|
|
|
|
id: 'app.modal.randomUser.ariaLabel.title',
|
|
|
|
description: 'modal title displayed to screen reader',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
mountModal: PropTypes.func.isRequired,
|
|
|
|
numAvailableViewers: PropTypes.number.isRequired,
|
2020-09-26 03:41:30 +08:00
|
|
|
randomUserReq: PropTypes.func.isRequired,
|
2020-08-29 01:23:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class RandomUserSelect extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2020-09-26 03:41:30 +08:00
|
|
|
if (props.currentUser.presenter) {
|
|
|
|
props.randomUserReq();
|
|
|
|
}
|
2021-04-16 02:33:40 +08:00
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
this.state = {
|
|
|
|
count: 0,
|
|
|
|
};
|
2021-04-16 02:33:40 +08:00
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
this.play = this.play.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterateSelection() {
|
2021-04-16 02:33:40 +08:00
|
|
|
if (this.props.mappedRandomlySelectedUsers.length > 1) {
|
|
|
|
const that = this;
|
2021-04-15 23:12:10 +08:00
|
|
|
setTimeout(delay(that.props.mappedRandomlySelectedUsers, 1), that.props.mappedRandomlySelectedUsers[1][1]);
|
|
|
|
function delay(arr, num) {
|
|
|
|
that.setState({
|
|
|
|
count: num,
|
|
|
|
});
|
2021-04-16 02:33:40 +08:00
|
|
|
if (num < that.props.mappedRandomlySelectedUsers.length - 1) {
|
|
|
|
setTimeout(() => { delay(arr, num + 1); }, arr[num + 1][1]);
|
2021-04-15 23:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (!this.props.currentUser.presenter) {
|
|
|
|
this.iterateSelection();
|
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:07:59 +08:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2021-04-15 23:12:10 +08:00
|
|
|
if (this.props.currentUser.presenter && this.state.count == 0) {
|
|
|
|
this.iterateSelection();
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
2021-04-26 20:07:59 +08:00
|
|
|
|
|
|
|
if (prevState.count !== this.state.count) {
|
|
|
|
this.play();
|
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
play() {
|
|
|
|
AudioService.playAlertSound(`${Meteor.settings.public.app.cdn
|
|
|
|
+ Meteor.settings.public.app.basename
|
|
|
|
+ Meteor.settings.public.app.instanceId}`
|
|
|
|
+ '/resources/sounds/Poll.mp3');
|
|
|
|
}
|
|
|
|
|
|
|
|
reselect() {
|
|
|
|
this.setState({
|
|
|
|
count: 0,
|
|
|
|
});
|
|
|
|
this.props.randomUserReq();
|
|
|
|
}
|
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
mountModal,
|
|
|
|
numAvailableViewers,
|
2020-09-26 03:41:30 +08:00
|
|
|
currentUser,
|
2020-10-19 22:46:41 +08:00
|
|
|
clearRandomlySelectedUser,
|
2021-04-15 23:12:10 +08:00
|
|
|
mappedRandomlySelectedUsers,
|
2020-08-29 01:23:27 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2021-04-16 02:33:40 +08:00
|
|
|
if (mappedRandomlySelectedUsers.length < this.state.count + 1) return null;
|
2021-04-15 23:12:10 +08:00
|
|
|
|
|
|
|
const selectedUser = mappedRandomlySelectedUsers[this.state.count][0];
|
2021-04-16 02:33:40 +08:00
|
|
|
const countDown = mappedRandomlySelectedUsers.length - this.state.count - 1;
|
|
|
|
|
2021-02-04 14:18:40 +08:00
|
|
|
let viewElement;
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
const amISelectedUser = currentUser.userId === selectedUser.userId;
|
|
|
|
if (numAvailableViewers < 1 || (currentUser.presenter && amISelectedUser)) { // there's no viewers to select from,
|
2021-04-16 02:33:40 +08:00
|
|
|
// or when you are the presenter but selected, which happens when the presenter ability is passed to somebody
|
|
|
|
// and people are entering and leaving the meeting
|
2021-02-04 14:18:40 +08:00
|
|
|
// display modal informing presenter that there's no viewers to select from
|
|
|
|
viewElement = (
|
|
|
|
<div className={styles.modalViewContainer}>
|
|
|
|
<div className={styles.modalViewTitle}>
|
|
|
|
{intl.formatMessage(messages.randUserTitle)}
|
|
|
|
</div>
|
|
|
|
<div>{intl.formatMessage(messages.noViewers)}</div>
|
2020-08-29 01:23:27 +08:00
|
|
|
</div>
|
2021-02-04 14:18:40 +08:00
|
|
|
);
|
|
|
|
} else { // viewers are available
|
|
|
|
if (!selectedUser) return null; // rendering triggered before selectedUser is available
|
|
|
|
|
|
|
|
// display modal with random user selection
|
|
|
|
viewElement = (
|
2021-04-16 02:33:40 +08:00
|
|
|
<div className={styles.modalViewContainer}>
|
2021-02-04 14:18:40 +08:00
|
|
|
<div className={styles.modalViewTitle}>
|
2021-04-15 23:12:10 +08:00
|
|
|
{countDown == 0
|
|
|
|
? amISelectedUser
|
2021-04-16 02:33:40 +08:00
|
|
|
? `${intl.formatMessage(messages.selected)}`
|
|
|
|
: numAvailableViewers == 1 && currentUser.presenter
|
|
|
|
? `${intl.formatMessage(messages.onlyOneViewerTobeSelected)}`
|
|
|
|
: `${intl.formatMessage(messages.randUserTitle)}`
|
|
|
|
: `${intl.formatMessage(messages.whollbeSelected)} ${countDown}`}
|
2021-02-04 14:18:40 +08:00
|
|
|
</div>
|
|
|
|
<div aria-hidden className={styles.modalAvatar} style={{ backgroundColor: `${selectedUser.color}` }}>
|
|
|
|
{selectedUser.name.slice(0, 2)}
|
|
|
|
</div>
|
|
|
|
<div className={styles.selectedUserName}>
|
|
|
|
{selectedUser.name}
|
|
|
|
</div>
|
2021-04-15 23:12:10 +08:00
|
|
|
{currentUser.presenter
|
|
|
|
&& countDown == 0
|
2021-02-04 14:18:40 +08:00
|
|
|
&& (
|
|
|
|
<Button
|
|
|
|
label={intl.formatMessage(messages.reselect)}
|
|
|
|
color="primary"
|
|
|
|
size="md"
|
|
|
|
className={styles.selectBtn}
|
2021-04-15 23:12:10 +08:00
|
|
|
onClick={() => this.reselect()}
|
2021-02-04 14:18:40 +08:00
|
|
|
/>
|
2021-04-16 02:33:40 +08:00
|
|
|
)}
|
2020-08-29 01:23:27 +08:00
|
|
|
</div>
|
2021-02-04 14:18:40 +08:00
|
|
|
);
|
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
hideBorder
|
2020-10-19 22:46:41 +08:00
|
|
|
onRequestClose={() => {
|
|
|
|
if (currentUser.presenter) clearRandomlySelectedUser();
|
|
|
|
mountModal(null);
|
|
|
|
}}
|
2020-08-29 01:23:27 +08:00
|
|
|
contentLabel={intl.formatMessage(messages.ariaModalTitle)}
|
|
|
|
>
|
|
|
|
{viewElement}
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomUserSelect.propTypes = propTypes;
|
|
|
|
export default injectIntl(RandomUserSelect);
|