2021-04-20 00:39:02 +08:00
|
|
|
import React, { useContext } from 'react';
|
2020-08-29 01:23:27 +08:00
|
|
|
import { withTracker } from 'meteor/react-meteor-data';
|
2021-10-20 04:35:39 +08:00
|
|
|
import Meetings from '/imports/api/meetings';
|
|
|
|
import Users from '/imports/api/users';
|
2020-08-29 01:23:27 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
|
|
|
import RandomUserSelect from './component';
|
2021-04-20 00:39:02 +08:00
|
|
|
import { UsersContext } from '/imports/ui/components/components-data/users-context/context';
|
2021-11-04 23:16:47 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-03-20 02:52:03 +08:00
|
|
|
const SELECT_RANDOM_USER_ENABLED = Meteor.settings.public.selectRandomUser.enabled;
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
// A value that is used by component to remember
|
|
|
|
// whether it should be open or closed after a render
|
2021-10-28 02:47:48 +08:00
|
|
|
let keepModalOpen = true;
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
// A value that stores the previous indicator
|
2021-10-28 02:47:48 +08:00
|
|
|
let updateIndicator = 1;
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
const toggleKeepModalOpen = () => { keepModalOpen = ! keepModalOpen; };
|
2021-10-28 02:47:48 +08:00
|
|
|
|
2021-04-20 00:39:02 +08:00
|
|
|
const RandomUserSelectContainer = (props) => {
|
|
|
|
const usingUsersContext = useContext(UsersContext);
|
|
|
|
const { users } = usingUsersContext;
|
|
|
|
const { randomlySelectedUser } = props;
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-04-20 00:39:02 +08:00
|
|
|
let mappedRandomlySelectedUsers = [];
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
const currentUser = {
|
|
|
|
userId: Auth.userID,
|
|
|
|
presenter: users[Auth.meetingID][Auth.userID].presenter
|
|
|
|
};
|
2021-10-28 02:47:48 +08:00
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
try {
|
|
|
|
if (!currentUser.presenter // this functionality does not bother presenter
|
|
|
|
&& (!keepModalOpen) // we only ween a change if modal has been closed before
|
|
|
|
&& (randomlySelectedUser[0][1] !== updateIndicator)// if tey are different, a user was generated
|
|
|
|
) { keepModalOpen = true; } // reopen modal
|
|
|
|
if (!currentUser.presenter) { updateIndicator = randomlySelectedUser[0][1]; } // keep indicator up to date
|
|
|
|
} catch (err) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'Random_USer_Error',
|
|
|
|
extraInfo: {
|
|
|
|
stackTrace: err,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'\nIssue in Random User Select container caused by back-end crash'
|
|
|
|
+ '\nValue of 6 randomly selected users was passed as '
|
|
|
|
+ `{${randomlySelectedUser}}`
|
|
|
|
+ '\nHowever, it is handled.'
|
|
|
|
+ '\nError message:'
|
|
|
|
+ `\n${err}`);
|
2021-11-04 02:51:32 +08:00
|
|
|
}
|
2021-10-28 02:47:48 +08:00
|
|
|
|
2021-04-20 00:39:02 +08:00
|
|
|
if (randomlySelectedUser) {
|
|
|
|
mappedRandomlySelectedUsers = randomlySelectedUser.map((ui) => {
|
|
|
|
const selectedUser = users[Auth.meetingID][ui[0]];
|
2023-04-12 23:51:23 +08:00
|
|
|
if (selectedUser){
|
|
|
|
return [{
|
|
|
|
userId: selectedUser.userId,
|
|
|
|
avatar: selectedUser.avatar,
|
|
|
|
color: selectedUser.color,
|
|
|
|
name: selectedUser.name,
|
|
|
|
}, ui[1]];
|
|
|
|
}
|
2021-04-20 00:39:02 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
return (
|
|
|
|
<RandomUserSelect
|
|
|
|
{...props}
|
|
|
|
mappedRandomlySelectedUsers={mappedRandomlySelectedUsers}
|
|
|
|
currentUser={currentUser}
|
|
|
|
keepModalOpen={keepModalOpen}
|
|
|
|
/>
|
|
|
|
);
|
2021-04-20 00:39:02 +08:00
|
|
|
};
|
2023-03-22 01:55:43 +08:00
|
|
|
export default withTracker(() => {
|
2020-09-26 03:41:30 +08:00
|
|
|
const viewerPool = Users.find({
|
2020-08-29 01:23:27 +08:00
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
presenter: { $ne: true },
|
|
|
|
role: { $eq: 'VIEWER' },
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
userId: 1,
|
|
|
|
},
|
|
|
|
}).fetch();
|
|
|
|
|
2020-09-26 03:41:30 +08:00
|
|
|
const meeting = Meetings.findOne({ meetingId: Auth.meetingID }, {
|
|
|
|
fields: {
|
|
|
|
randomlySelectedUser: 1,
|
|
|
|
},
|
|
|
|
});
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-03-20 02:52:03 +08:00
|
|
|
const randomUserReq = () => (SELECT_RANDOM_USER_ENABLED ? makeCall('setRandomUser') : null);
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-03-20 02:52:03 +08:00
|
|
|
const clearRandomlySelectedUser = () => (SELECT_RANDOM_USER_ENABLED ? makeCall('clearRandomlySelectedUser') : null);
|
2020-10-19 22:46:41 +08:00
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
return ({
|
2021-10-28 02:47:48 +08:00
|
|
|
toggleKeepModalOpen,
|
2020-09-26 03:41:30 +08:00
|
|
|
numAvailableViewers: viewerPool.length,
|
|
|
|
randomUserReq,
|
2020-10-19 22:46:41 +08:00
|
|
|
clearRandomlySelectedUser,
|
2021-04-20 00:39:02 +08:00
|
|
|
randomlySelectedUser: meeting.randomlySelectedUser,
|
2020-08-29 01:23:27 +08:00
|
|
|
});
|
2023-03-22 01:55:43 +08:00
|
|
|
})(RandomUserSelectContainer);
|