diff --git a/src/components/PollViewer/PollViewer.vue b/src/components/PollViewer/PollViewer.vue index 4b633f9e00f..8fa5451345e 100644 --- a/src/components/PollViewer/PollViewer.vue +++ b/src/components/PollViewer/PollViewer.vue @@ -296,6 +296,12 @@ export default { id(value) { this.pollsStore.hidePollToast(value) + if (this.pollsStore.scheduledPolls[this.token]?.[value]) { + this.pollsStore.getPollData({ + token: this.token, + pollId: value, + }) + } }, isInCall(value) { diff --git a/src/store/messagesStore.js b/src/store/messagesStore.js index d4fbd587b09..ea942f763fb 100644 --- a/src/store/messagesStore.js +++ b/src/store/messagesStore.js @@ -464,7 +464,7 @@ const actions = { if (isHiddenSystemMessage(message)) { if (message.systemMessage === MESSAGE.SYSTEM_TYPE.POLL_VOTED) { const pollsStore = usePollsStore() - pollsStore.debounceGetPollData({ + pollsStore.scheduleGetPollData({ token, pollId: message.messageParameters.poll.id, }) @@ -562,7 +562,7 @@ const actions = { if (message.systemMessage === MESSAGE.SYSTEM_TYPE.POLL_CLOSED) { const pollsStore = usePollsStore() - pollsStore.getPollData({ + pollsStore.scheduleGetPollData({ token, pollId: message.messageParameters.poll.id, }) diff --git a/src/stores/polls.ts b/src/stores/polls.ts index 0108a9c5df8..82ca554eb03 100644 --- a/src/stores/polls.ts +++ b/src/stores/polls.ts @@ -30,7 +30,7 @@ type submitVotePayload = { token: string, pollId: string } & Pick> drafts: Record> - debouncedFunctions: Record void>> + scheduledPolls: Record> activePoll: { token: string, id: string, name: string } | null pollToastsQueue: Record> } @@ -38,7 +38,7 @@ export const usePollsStore = defineStore('polls', { state: (): State => ({ polls: {}, drafts: {}, - debouncedFunctions: {}, + scheduledPolls: {}, activePoll: null, pollToastsQueue: {}, }), @@ -101,6 +101,11 @@ export const usePollsStore = defineStore('polls', { try { const response = await getPollData(token, pollId) this.addPoll({ token, poll: response.data.ocs.data }) + + // Unmark the poll as scheduled for update + if (this.scheduledPolls[token]?.[pollId]) { + delete this.scheduledPolls[token][pollId] + } } catch (error) { console.error(error) } @@ -108,26 +113,21 @@ export const usePollsStore = defineStore('polls', { /** * In order to limit the amount of requests, we cannot get the - * poll data every time someone votes, so we create a debounce - * function for each poll and store it in the pollStore + * poll data every time someone votes, so we schedule an update + * of poll information for the next time user opens a dialog * * @param payload The arguments passed to the action * @param payload.token The token of the conversation * @param payload.pollId The id of the poll */ - debounceGetPollData({ token, pollId }: { token: string, pollId: string }) { - if (!this.debouncedFunctions[token]) { - this.debouncedFunctions[token] = {} - } - // Create the debounced function for getting poll data if not exist yet - if (!this.debouncedFunctions[token]?.[pollId]) { - const debouncedFunction = debounce(async () => { - await this.getPollData({ token, pollId }) - }, 5000) - this.debouncedFunctions[token][pollId] = debouncedFunction - } - // Call the debounced function for getting the poll data - this.debouncedFunctions[token][pollId]() + scheduleGetPollData({ token, pollId }: { token: string, pollId: string }) { + if (!this.scheduledPolls[token]) { + this.scheduledPolls[token] = {} + } + // Mark the poll as scheduled for update + if (!this.scheduledPolls[token]?.[pollId]) { + this.scheduledPolls[token][pollId] = true + } }, async createPoll({ token, form, threadId }: { token: string, form: createPollParams, threadId?: number }) {