Skip to content

Commit

Permalink
fix: migrate polls store to TS
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Oct 4, 2024
1 parent 17001f8 commit a4334e2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ import CallButton from '../../../../TopBar/CallButton.vue'
import { useIsInCall } from '../../../../../composables/useIsInCall.js'
import { useMessageInfo } from '../../../../../composables/useMessageInfo.js'
import { EventBus } from '../../../../../services/EventBus.js'
import { usePollsStore } from '../../../../../stores/polls.js'
import { usePollsStore } from '../../../../../stores/polls.ts'
import { parseSpecialSymbols, parseMentions } from '../../../../../utils/textParse.ts'

// Regular expression to check for Unicode emojis in message text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { t } from '@nextcloud/l10n'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'

import { POLL } from '../../../../../constants.js'
import { usePollsStore } from '../../../../../stores/polls.js'
import { usePollsStore } from '../../../../../stores/polls.ts'

export default {
name: 'Poll',
Expand Down
2 changes: 1 addition & 1 deletion src/components/NewMessage/NewMessagePollEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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 { usePollsStore } from '../../stores/polls.js'
import { usePollsStore } from '../../stores/polls.ts'

export default {
name: 'NewMessagePollEditor',
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 @@ -108,7 +108,7 @@ import { useId } from '../../composables/useId.ts'
import { useIsInCall } from '../../composables/useIsInCall.js'
import { POLL } from '../../constants.js'
import { EventBus } from '../../services/EventBus.js'
import { usePollsStore } from '../../stores/polls.js'
import { usePollsStore } from '../../stores/polls.ts'

export default {
name: 'PollViewer',
Expand Down
2 changes: 1 addition & 1 deletion src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../services/messagesService.ts'
import { useChatExtrasStore } from '../stores/chatExtras.js'
import { useGuestNameStore } from '../stores/guestName.js'
import { usePollsStore } from '../stores/polls.js'
import { usePollsStore } from '../stores/polls.ts'
import { useReactionsStore } from '../stores/reactions.js'
import { useSharedItemsStore } from '../stores/sharedItems.js'
import CancelableRequest from '../utils/cancelableRequest.js'
Expand Down
2 changes: 1 addition & 1 deletion src/stores/__tests__/polls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
endPoll,
} from '../../services/pollService.ts'
import { generateOCSResponse } from '../../test-helpers.js'
import { usePollsStore } from '../polls.js'
import { usePollsStore } from '../polls.ts'

jest.mock('../../services/pollService', () => ({
createPoll: jest.fn(),
Expand Down
43 changes: 28 additions & 15 deletions src/stores/polls.js → src/stores/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,47 @@ import {
submitVote,
endPoll,
} from '../services/pollService.ts'

import type {
ChatMessage,
createPollParams,
Poll, votePollParams
} from '../types/index.ts'

type createPollPayload = { token: string } & createPollParams
type submitVotePayload = { token: string, pollId: number } & Pick<votePollParams, 'optionIds'>
type State = {
polls: Record<string, Record<string, Poll>>,
debouncedFunctions: Record<string, Record<string, () => void>>,
activePoll: null,
pollToastsQueue: Record<string, ReturnType<typeof showInfo>>,
}
export const usePollsStore = defineStore('polls', {
state: () => ({
state: (): State => ({
polls: {},
debouncedFunctions: {},
activePoll: null,
pollToastsQueue: {},
}),

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

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

actions: {
addPoll({ token, poll }) {
addPoll({ token, poll }: { token: string, poll: Poll }) {
if (!this.polls[token]) {
Vue.set(this.polls, token, {})
}
Vue.set(this.polls[token], poll.id, poll)
},

async getPollData({ token, pollId }) {
async getPollData({ token, pollId }: { token: string, pollId: number }) {
try {
const response = await getPollData(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
Expand All @@ -60,7 +73,7 @@ export const usePollsStore = defineStore('polls', {
* @param { string } root0.token The token of the conversation
* @param { number } root0.pollId The id of the poll
*/
debounceGetPollData({ token, pollId }) {
debounceGetPollData({ token, pollId }: { token: string, pollId: number }) {
if (!this.debouncedFunctions[token]) {
Vue.set(this.debouncedFunctions, token, {})
}
Expand All @@ -75,7 +88,7 @@ export const usePollsStore = defineStore('polls', {
this.debouncedFunctions[token][pollId]()
},

async createPoll({ token, question, options, resultMode, maxVotes }) {
async createPoll({ token, question, options, resultMode, maxVotes }: createPollPayload) {
try {
const response = await createPoll({
token,
Expand All @@ -92,7 +105,7 @@ export const usePollsStore = defineStore('polls', {
}
},

async submitVote({ token, pollId, optionIds }) {
async submitVote({ token, pollId, optionIds }: submitVotePayload) {
try {
const response = await submitVote(token, pollId, optionIds)
this.addPoll({ token, poll: response.data.ocs.data })
Expand All @@ -102,7 +115,7 @@ export const usePollsStore = defineStore('polls', {
}
},

async endPoll({ token, pollId }) {
async endPoll({ token, pollId }: { token: string, pollId: number }) {
try {
const response = await endPoll(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
Expand All @@ -112,7 +125,7 @@ export const usePollsStore = defineStore('polls', {
}
},

setActivePoll({ token, pollId, name }) {
setActivePoll({ token, pollId, name }: { token: string, pollId: number, name: string }) {
Vue.set(this, 'activePoll', { token, id: pollId, name })
},

Expand All @@ -122,8 +135,8 @@ export const usePollsStore = defineStore('polls', {
}
},

addPollToast({ token, message }) {
const pollId = message.messageParameters.object.id
addPollToast({ token, message }: { token: string, message: ChatMessage }) {
const pollId = +message.messageParameters.object.id
const name = message.messageParameters.object.name

const toast = showInfo(t('spreed', 'Poll "{name}" was created by {user}. Click to vote', {
Expand All @@ -141,7 +154,7 @@ export const usePollsStore = defineStore('polls', {
Vue.set(this.pollToastsQueue, pollId, toast)
},

hidePollToast(pollId) {
hidePollToast(pollId: number) {
if (this.pollToastsQueue[pollId]) {
this.pollToastsQueue[pollId].hideToast()
Vue.delete(this.pollToastsQueue, pollId)
Expand All @@ -150,7 +163,7 @@ export const usePollsStore = defineStore('polls', {

hideAllPollToasts() {
for (const pollId in this.pollToastsQueue) {
this.hidePollToast(pollId)
this.hidePollToast(+pollId)
}
},
},
Expand Down

0 comments on commit a4334e2

Please sign in to comment.