Skip to content

Commit

Permalink
fix: minor store refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Sep 11, 2024
1 parent 4208d92 commit 3456eb6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export default {
return false
}

return this.isInCall && !!this.pollsStore.getNewPolls[this.message.messageParameters.object.id]
return this.isInCall && this.pollsStore.isNewPoll(this.message.messageParameters.object.id)
},

isTemporary() {
Expand Down
23 changes: 8 additions & 15 deletions src/components/NewMessage/NewMessagePollEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadi
import NcDialog from '@nextcloud/vue/dist/Components/NcDialog.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'

import pollService from '../../services/pollService.js'
import { usePollsStore } from '../../stores/polls.js'

export default {
Expand Down Expand Up @@ -147,21 +146,15 @@ export default {
},

async createPoll() {
try {
const response = await pollService.postNewPoll(
this.token,
this.pollQuestion,
this.pollOptions,
this.isPrivate ? 1 : 0,
this.isMultipleAnswer ? 0 : 1)
// Add the poll immediately to the store
this.pollsStore.addPoll({
token: this.token,
poll: response.data.ocs.data,
})
const poll = await this.pollsStore.createPoll({
token: this.token,
question: this.pollQuestion,
options: this.pollOptions,
resultMode: this.isPrivate ? 1 : 0,
maxVotes: this.isMultipleAnswer ? 0 : 1
})
if (poll) {
this.dismissEditor()
} catch (error) {
console.debug(error)
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/components/PollViewer/PollViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export default {
await this.pollsStore.submitVote({
token: this.token,
pollId: this.id,
vote: this.voteToSubmit.map(element => +element),
optionIds: this.voteToSubmit.map(element => +element),
})
this.modalPage = 'results'
} catch (error) {
Expand Down
89 changes: 45 additions & 44 deletions src/stores/polls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ import pollService from '../services/pollService.js'
export const usePollsStore = defineStore('polls', {
state: () => ({
polls: {},
pollDebounceFunctions: {},
debouncedFunctions: {},
activePoll: null,
pollToastsQueue: {},
}),

getters: {
getPoll: (state) => (token, id) => {
return state.polls?.[token]?.[id]
getPoll: (state) => (token, pollId) => {
return state.polls[token]?.[pollId]
},

activePoll: (state) => {
return state.activePoll
},

getNewPolls: (state) => {
return state.pollToastsQueue
isNewPoll: (state) => (pollId) => {
return state.pollToastsQueue[pollId] !== undefined
},
},

Expand All @@ -44,11 +40,9 @@ export const usePollsStore = defineStore('polls', {
async getPollData({ token, pollId }) {
try {
const response = await pollService.getPollData(token, pollId)
const poll = response.data.ocs.data
this.addPoll({ token, poll })
console.debug('polldata', response)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.debug(error)
console.error(error)
}
},

Expand All @@ -59,46 +53,54 @@ export const usePollsStore = defineStore('polls', {
*
* @param { object } root0 The arguments passed to the action
* @param { string } root0.token The token of the conversation
* @param { number }root0.pollId The id of the poll
* @param { number } root0.pollId The id of the poll
*/
debounceGetPollData({ token, pollId }) {
// Create debounce function for getting this particular poll data
// if it does not exist yet
if (!this.pollDebounceFunctions[token]?.[pollId]) {
const debounceGetPollDataFunction = debounce(async () => {
await this.getPollData({
token,
pollId,
})
if (!this.debouncedFunctions[token]) {
Vue.set(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)
// Add the debounce function to the state object
if (!this.pollDebounceFunctions[token]) {
Vue.set(this.pollDebounceFunctions, token, {})
}
Vue.set(this.pollDebounceFunctions[token], pollId, debounceGetPollDataFunction)
Vue.set(this.debouncedFunctions[token], pollId, debouncedFunction)
}
// Call the debounced function for getting the poll data
this.debouncedFunctions[token][pollId]()
},

async createPoll({ token, question, options, resultMode, maxVotes }) {
try {
const response = await pollService.postNewPoll(
token,
question,
options,
resultMode,
maxVotes,
)
this.addPoll({ token, poll: response.data.ocs.data })

return response.data.ocs.data
} catch (error) {
console.error(error)
}
// Call the debounce function for getting the poll data
this.pollDebounceFunctions[token][pollId]()
},

async submitVote({ token, pollId, vote }) {
console.debug('Submitting vote')
async submitVote({ token, pollId, optionIds }) {
try {
const response = await pollService.submitVote(token, pollId, vote)
const poll = response.data.ocs.data
this.addPoll({ token, poll })
const response = await pollService.submitVote(token, pollId, optionIds)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
showError(t('spreed', 'An error occurred while submitting your vote'))
}
},

async endPoll({ token, pollId }) {
console.debug('Ending poll')
try {
const response = await pollService.endPoll(token, pollId)
const poll = response.data.ocs.data
this.addPoll({ token, poll })
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
showError(t('spreed', 'An error occurred while ending the poll'))
Expand Down Expand Up @@ -134,17 +136,16 @@ export const usePollsStore = defineStore('polls', {
Vue.set(this.pollToastsQueue, pollId, toast)
},

hidePollToast(id) {
if (this.pollToastsQueue[id]) {
this.pollToastsQueue[id].hideToast()
Vue.delete(this.pollToastsQueue, id)
hidePollToast(pollId) {
if (this.pollToastsQueue[pollId]) {
this.pollToastsQueue[pollId].hideToast()
Vue.delete(this.pollToastsQueue, pollId)
}
},

hideAllPollToasts() {
for (const id in this.pollToastsQueue) {
this.pollToastsQueue[id].hideToast()
Vue.delete(this.pollToastsQueue, id)
for (const pollId in this.pollToastsQueue) {
this.hidePollToast(pollId)
}
},
},
Expand Down

0 comments on commit 3456eb6

Please sign in to comment.