2016-05-06 02:50:18 +08:00
|
|
|
import React from 'react';
|
2018-01-08 12:44:42 +08:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { withTracker } from 'meteor/react-meteor-data';
|
2023-12-09 02:51:41 +08:00
|
|
|
import { useMutation } from '@apollo/client';
|
2017-10-06 20:50:01 +08:00
|
|
|
import PollingService from './service';
|
2019-09-07 04:28:02 +08:00
|
|
|
import PollService from '/imports/ui/components/poll/service';
|
2016-05-20 21:46:30 +08:00
|
|
|
import PollingComponent from './component';
|
2022-03-09 02:05:24 +08:00
|
|
|
import { isPollingEnabled } from '/imports/ui/services/features';
|
2023-11-30 19:08:16 +08:00
|
|
|
import useCurrentUser from '/imports/ui/core/hooks/useCurrentUser';
|
2023-12-09 03:22:52 +08:00
|
|
|
import { POLL_SUBMIT_TYPED_VOTE, POLL_SUBMIT_VOTE } from '/imports/ui/components/poll/mutations';
|
2024-01-12 00:51:51 +08:00
|
|
|
import PollingGraphqlContainer from './polling-graphql/component';
|
2016-05-06 02:50:18 +08:00
|
|
|
|
2018-01-08 12:44:42 +08:00
|
|
|
const propTypes = {
|
|
|
|
pollExists: PropTypes.bool.isRequired,
|
2017-10-06 20:50:01 +08:00
|
|
|
};
|
2018-01-08 12:44:42 +08:00
|
|
|
|
|
|
|
const PollingContainer = ({ pollExists, ...props }) => {
|
2023-11-30 19:08:16 +08:00
|
|
|
const { data: currentUserData } = useCurrentUser((user) => ({
|
|
|
|
presenter: user.presenter,
|
|
|
|
}));
|
|
|
|
const showPolling = pollExists && !currentUserData?.presenter && isPollingEnabled();
|
2020-09-21 20:07:36 +08:00
|
|
|
|
2023-12-09 02:51:41 +08:00
|
|
|
const [pollSubmitUserTypedVote] = useMutation(POLL_SUBMIT_TYPED_VOTE);
|
2023-12-09 03:22:52 +08:00
|
|
|
const [pollSubmitUserVote] = useMutation(POLL_SUBMIT_VOTE);
|
2023-12-09 02:51:41 +08:00
|
|
|
|
|
|
|
const handleTypedVote = (pollId, answer) => {
|
|
|
|
pollSubmitUserTypedVote({
|
|
|
|
variables: {
|
|
|
|
pollId,
|
|
|
|
answer,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-12-09 03:22:52 +08:00
|
|
|
const handleVote = (pollId, answerIds) => {
|
|
|
|
pollSubmitUserVote({
|
|
|
|
variables: {
|
|
|
|
pollId,
|
|
|
|
answerIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-02 22:54:37 +08:00
|
|
|
if (showPolling) {
|
2018-01-08 12:44:42 +08:00
|
|
|
return (
|
2023-12-09 03:22:52 +08:00
|
|
|
<PollingComponent handleTypedVote={handleTypedVote} handleVote={handleVote} {...props} />
|
2018-01-08 12:44:42 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
PollingContainer.propTypes = propTypes;
|
|
|
|
|
2024-01-12 00:51:51 +08:00
|
|
|
withTracker(() => {
|
2020-09-21 20:07:36 +08:00
|
|
|
const {
|
2023-12-09 03:22:52 +08:00
|
|
|
pollExists, poll,
|
2020-09-21 20:07:36 +08:00
|
|
|
} = PollingService.mapPolls();
|
2021-11-27 00:30:56 +08:00
|
|
|
const { pollTypes } = PollService;
|
|
|
|
|
2022-03-09 02:05:24 +08:00
|
|
|
if (poll && poll?.pollType) {
|
2021-11-27 00:30:56 +08:00
|
|
|
const isResponse = poll.pollType === pollTypes.Response;
|
|
|
|
Meteor.subscribe('polls', isResponse);
|
|
|
|
}
|
|
|
|
|
2019-05-23 02:00:44 +08:00
|
|
|
return ({
|
|
|
|
pollExists,
|
|
|
|
poll,
|
2019-09-07 04:28:02 +08:00
|
|
|
pollAnswerIds: PollService.pollAnswerIds,
|
2021-11-27 00:30:56 +08:00
|
|
|
pollTypes,
|
2021-06-12 00:55:53 +08:00
|
|
|
isDefaultPoll: PollService.isDefaultPoll,
|
2019-06-28 00:46:14 +08:00
|
|
|
isMeteorConnected: Meteor.status().connected,
|
2019-05-23 02:00:44 +08:00
|
|
|
});
|
2018-01-08 12:44:42 +08:00
|
|
|
})(PollingContainer);
|
2024-01-12 00:51:51 +08:00
|
|
|
|
|
|
|
export default PollingGraphqlContainer;
|