mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
71 lines • 2.73 kB
JavaScript
/**
* Updates the poll state based on the provided data.
*
* @param {Object} options - The options for updating the poll.
* @param {any} options.data - The data containing poll information.
* @param {any[]} options.polls - The current list of polls.
* @param {any} options.poll - The current poll.
* @param {string} options.member - The member identifier.
* @param {string} options.islevel - The level of the member.
* @param {Function} options.showAlert - Function to show alerts.
* @param {Function} options.updatePolls - Function to update the list of polls.
* @param {Function} options.updatePoll - Function to update the current poll.
* @param {Function} options.updateIsPollModalVisible - Function to update the visibility of the poll modal.
* @returns {Promise<void>} A promise that resolves when the poll update is complete.
*
* @example
* ```typescript
* await pollUpdated({
* data: { poll: updatedPoll, status: "started" },
* polls: currentPolls,
* poll: currentPoll,
* member: "user123",
* islevel: "1",
* showAlert: (alert) => console.log(alert.message),
* updatePolls: (polls) => setPolls(polls),
* updatePoll: (poll) => setCurrentPoll(poll),
* updateIsPollModalVisible: (visible) => setIsPollModalVisible(visible),
* });
* ```
*/
export const pollUpdated = async ({ data, polls, poll, member, islevel, showAlert, updatePolls, updatePoll, updateIsPollModalVisible, }) => {
try {
if (data.polls) {
polls = data.polls;
updatePolls(data.polls);
}
else {
polls = [data.poll];
updatePolls(polls);
}
let temp_poll = { id: '' };
if (poll) {
temp_poll = Object.assign({}, poll);
}
if (data.status !== 'ended') {
poll = data.poll;
updatePoll(data.poll);
}
if (data.status === 'started' && islevel !== '2') {
if (!poll.voters || (poll.voters && !poll.voters[member])) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'New poll started',
type: 'success',
duration: 3000,
});
updateIsPollModalVisible(true);
}
}
else if (data.status === 'ended') {
if (temp_poll.id === data.poll.id) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({ message: 'Poll ended', type: 'danger', duration: 3000 });
// update the poll
updatePoll(data.poll);
}
}
}
catch (_a) {
// console.log(error);
}
};
//# sourceMappingURL=pollUpdated.js.map