2017-04-26 21:47:44 +08:00
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
2017-10-12 10:00:28 +08:00
|
|
|
import Polls from '/imports/api/polls';
|
2020-10-29 02:33:09 +08:00
|
|
|
import { debounce } from 'lodash';
|
2016-05-06 02:50:18 +08:00
|
|
|
|
2019-04-02 12:07:45 +08:00
|
|
|
const MAX_CHAR_LENGTH = 5;
|
|
|
|
|
2020-10-29 02:33:09 +08:00
|
|
|
const handleVote = (pollId, answerId) => {
|
|
|
|
makeCall('publishVote', pollId, answerId.id);
|
|
|
|
};
|
|
|
|
|
2021-02-09 05:41:43 +08:00
|
|
|
const handleTypedVote = (pollId, answer) => {
|
|
|
|
makeCall('publishTypedVote', pollId, answer);
|
2021-02-09 14:22:41 +08:00
|
|
|
};
|
2021-02-09 05:41:43 +08:00
|
|
|
|
2019-03-28 23:58:10 +08:00
|
|
|
const mapPolls = () => {
|
2017-06-03 03:25:02 +08:00
|
|
|
const poll = Polls.findOne({});
|
2016-05-06 02:50:18 +08:00
|
|
|
if (!poll) {
|
|
|
|
return { pollExists: false };
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:58:10 +08:00
|
|
|
const { answers } = poll;
|
|
|
|
let stackOptions = false;
|
|
|
|
|
|
|
|
answers.map((obj) => {
|
|
|
|
if (stackOptions) return obj;
|
|
|
|
if (obj.key.length > MAX_CHAR_LENGTH) {
|
|
|
|
stackOptions = true;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
|
2017-07-25 20:26:45 +08:00
|
|
|
const amIRequester = poll.requester !== 'userId';
|
2016-05-06 02:50:18 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
poll: {
|
2017-07-25 20:26:45 +08:00
|
|
|
answers: poll.answers,
|
|
|
|
pollId: poll.id,
|
2020-09-21 20:07:36 +08:00
|
|
|
pollType: poll.pollType,
|
2019-03-28 23:58:10 +08:00
|
|
|
stackOptions,
|
2020-09-21 20:07:36 +08:00
|
|
|
question: poll.question,
|
2016-05-06 02:50:18 +08:00
|
|
|
},
|
|
|
|
pollExists: true,
|
2017-06-03 03:25:02 +08:00
|
|
|
amIRequester,
|
2020-10-29 02:33:09 +08:00
|
|
|
handleVote: debounce(handleVote, 500, { leading: true, trailing: false }),
|
2021-02-09 05:41:43 +08:00
|
|
|
handleTypedVote: debounce(handleTypedVote, 500, { leading: true, trailing: false }),
|
2016-05-06 02:50:18 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default { mapPolls };
|