2019-06-07 21:49:50 +08:00
|
|
|
import React, { Component } from 'react';
|
2019-09-19 04:06:59 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2019-06-07 21:49:50 +08:00
|
|
|
import { Session } from 'meteor/session';
|
|
|
|
import { withModalMounter } from '/imports/ui/components/modal/service';
|
|
|
|
import BreakoutJoinConfirmation from '/imports/ui/components/breakout-join-confirmation/container';
|
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
const BREAKOUT_MODAL_DELAY = 200;
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
mountModal: PropTypes.func.isRequired,
|
|
|
|
currentBreakoutUser: PropTypes.shape({
|
|
|
|
insertedTime: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
getBreakoutByUser: PropTypes.func.isRequired,
|
|
|
|
breakoutUserIsIn: PropTypes.shape({
|
|
|
|
sequence: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
breakouts: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
freeJoin: PropTypes.bool.isRequired,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
currentBreakoutUser: undefined,
|
|
|
|
breakoutUserIsIn: undefined,
|
|
|
|
breakouts: [],
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
const openBreakoutJoinConfirmation = (breakout, breakoutName, mountModal) => mountModal(
|
|
|
|
<BreakoutJoinConfirmation
|
|
|
|
breakout={breakout}
|
|
|
|
breakoutName={breakoutName}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeBreakoutJoinConfirmation = mountModal => mountModal(null);
|
|
|
|
|
|
|
|
class BreakoutRoomInvitation extends Component {
|
2019-09-19 04:06:59 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
didSendBreakoutInvite: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
// use dummy old data on mount so it works if no data changes
|
|
|
|
this.checkBreakouts({ breakouts: [] });
|
|
|
|
}
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
componentDidUpdate(oldProps) {
|
2019-09-19 04:06:59 +08:00
|
|
|
this.checkBreakouts(oldProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkBreakouts(oldProps) {
|
2019-06-07 21:49:50 +08:00
|
|
|
const {
|
|
|
|
breakouts,
|
|
|
|
mountModal,
|
|
|
|
currentBreakoutUser,
|
|
|
|
getBreakoutByUser,
|
2019-09-19 04:06:59 +08:00
|
|
|
breakoutUserIsIn,
|
2020-01-23 04:13:53 +08:00
|
|
|
amIModerator,
|
2019-06-07 21:49:50 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
const {
|
|
|
|
didSendBreakoutInvite,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
const hadBreakouts = oldProps.breakouts.length > 0;
|
|
|
|
const hasBreakouts = breakouts.length > 0;
|
2019-06-07 21:49:50 +08:00
|
|
|
if (!hasBreakouts && hadBreakouts) {
|
|
|
|
closeBreakoutJoinConfirmation(mountModal);
|
|
|
|
}
|
|
|
|
|
2020-01-23 04:13:53 +08:00
|
|
|
if (hasBreakouts && !breakoutUserIsIn && !amIModerator) {
|
2019-09-19 04:06:59 +08:00
|
|
|
// Have to check for freeJoin breakouts first because currentBreakoutUser will
|
|
|
|
// populate after a room has been joined
|
2020-01-31 02:14:27 +08:00
|
|
|
const breakoutRoom = getBreakoutByUser(currentBreakoutUser);
|
2019-09-19 04:06:59 +08:00
|
|
|
const freeJoinBreakout = breakouts.find(breakout => breakout.freeJoin);
|
|
|
|
if (freeJoinBreakout) {
|
|
|
|
if (!didSendBreakoutInvite) {
|
2020-01-31 02:14:27 +08:00
|
|
|
this.inviteUserToBreakout(breakoutRoom || freeJoinBreakout);
|
2019-09-19 04:06:59 +08:00
|
|
|
this.setState({ didSendBreakoutInvite: true });
|
|
|
|
}
|
|
|
|
} else if (currentBreakoutUser) {
|
|
|
|
const currentInsertedTime = currentBreakoutUser.insertedTime;
|
|
|
|
const oldCurrentUser = oldProps.currentBreakoutUser || {};
|
|
|
|
const oldInsertedTime = oldCurrentUser.insertedTime;
|
|
|
|
if (currentInsertedTime !== oldInsertedTime) {
|
|
|
|
const breakoutId = Session.get('lastBreakoutOpened');
|
|
|
|
if (breakoutRoom.breakoutId !== breakoutId) {
|
|
|
|
this.inviteUserToBreakout(breakoutRoom);
|
|
|
|
}
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 04:06:59 +08:00
|
|
|
|
|
|
|
if (!hasBreakouts && didSendBreakoutInvite) {
|
|
|
|
this.setState({ didSendBreakoutInvite: false });
|
|
|
|
}
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inviteUserToBreakout(breakout) {
|
|
|
|
const {
|
|
|
|
mountModal,
|
|
|
|
} = this.props;
|
2019-09-19 04:06:59 +08:00
|
|
|
// There's a race condition on page load with modals. Only one modal can be shown at a
|
|
|
|
// time and new ones overwrite old ones. We delay the opening of the breakout modal
|
|
|
|
// because it should always be on top if breakouts are running.
|
|
|
|
setTimeout(() => {
|
|
|
|
openBreakoutJoinConfirmation.call(this, breakout, breakout.name, mountModal);
|
|
|
|
}, BREAKOUT_MODAL_DELAY);
|
2019-06-07 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:06:59 +08:00
|
|
|
BreakoutRoomInvitation.propTypes = propTypes;
|
|
|
|
BreakoutRoomInvitation.defaultProps = defaultProps;
|
|
|
|
|
2019-06-07 21:49:50 +08:00
|
|
|
export default withModalMounter(BreakoutRoomInvitation);
|