Skip to content

Commit

Permalink
Merge pull request #13465 from nextcloud/fix/noid/polls-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Antreesy authored Oct 7, 2024
2 parents b681d0e + bfdd93b commit b2d25cd
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 98 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
65 changes: 0 additions & 65 deletions src/services/pollService.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/services/pollService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

import type {
closePollResponse,
createPollParams,
createPollResponse,
getPollResponse,
votePollParams,
votePollResponse,
} from '../types/index.ts'

type createPollPayload = { token: string } & createPollParams

/**
* @param payload The payload
* @param payload.token The conversation token
* @param payload.question The question of the poll
* @param payload.options The options participants can vote for
* @param payload.resultMode Result mode of the poll (0 - always visible | 1 - hidden until the poll is closed)
* @param payload.maxVotes Maximum amount of options a user can vote for (0 - unlimited | 1 - single answer)
*/
const createPoll = async ({ token, question, options, resultMode, maxVotes }: createPollPayload): createPollResponse => {
return axios.post(generateOcsUrl('apps/spreed/api/v1/poll/{token}', { token }), {
question,
options,
resultMode,
maxVotes,
} as createPollParams)
}

/**
* @param token The conversation token
* @param pollId Id of the poll
*/
const getPollData = async (token: string, pollId: string): getPollResponse => {
return axios.get(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }))
}

/**
* @param token The conversation token
* @param pollId Id of the poll
* @param optionIds Indexes of options the participant votes for
*/
const submitVote = async (token: string, pollId: string, optionIds: votePollParams['optionIds']): votePollResponse => {
return axios.post(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }), {
optionIds,
} as votePollParams)
}

/**
* @param token The conversation token
* @param pollId Id of the poll
*/
const endPoll = async (token: string, pollId: string): closePollResponse => {
return axios.delete(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }))
}

export {
createPoll,
getPollData,
submitVote,
endPoll,
}
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
23 changes: 14 additions & 9 deletions src/stores/__tests__/polls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import flushPromises from 'flush-promises'
import { setActivePinia, createPinia } from 'pinia'

import { ATTENDEE } from '../../constants.js'
import pollService from '../../services/pollService.js'
import {
createPoll,
getPollData,
submitVote,
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.js', () => ({
postNewPoll: jest.fn(),
jest.mock('../../services/pollService', () => ({
createPoll: jest.fn(),
getPollData: jest.fn(),
submitVote: jest.fn(),
endPoll: jest.fn(),
Expand Down Expand Up @@ -88,7 +93,7 @@ describe('pollsStore', () => {
it('receives a poll from server and adds it to the store', async () => {
// Arrange
const response = generateOCSResponse({ payload: poll })
pollService.getPollData.mockResolvedValue(response)
getPollData.mockResolvedValue(response)

// Act
await pollsStore.getPollData({ token: TOKEN, pollId: poll.id })
Expand All @@ -101,7 +106,7 @@ describe('pollsStore', () => {
// Arrange
jest.useFakeTimers()
const response = generateOCSResponse({ payload: poll })
pollService.getPollData.mockResolvedValue(response)
getPollData.mockResolvedValue(response)

// Act
pollsStore.debounceGetPollData({ token: TOKEN, pollId: poll.id })
Expand All @@ -116,7 +121,7 @@ describe('pollsStore', () => {
it('creates a poll and adds it to the store', async () => {
// Arrange
const response = generateOCSResponse({ payload: poll })
pollService.postNewPoll.mockResolvedValue(response)
createPoll.mockResolvedValue(response)

// Act
await pollsStore.createPoll({ token: TOKEN, ...pollRequest })
Expand All @@ -129,7 +134,7 @@ describe('pollsStore', () => {
// Arrange
pollsStore.addPoll({ token: TOKEN, poll })
const response = generateOCSResponse({ payload: pollWithVote })
pollService.submitVote.mockResolvedValue(response)
submitVote.mockResolvedValue(response)

// Act
await pollsStore.submitVote({ token: TOKEN, pollId: poll.id, optionIds: [0] })
Expand All @@ -142,7 +147,7 @@ describe('pollsStore', () => {
// Arrange
pollsStore.addPoll({ token: TOKEN, poll: pollWithVote })
const response = generateOCSResponse({ payload: pollWithVoteEnded })
pollService.endPoll.mockResolvedValue(response)
endPoll.mockResolvedValue(response)

// Act
await pollsStore.endPoll({ token: TOKEN, pollId: poll.id })
Expand Down
56 changes: 37 additions & 19 deletions src/stores/polls.js → src/stores/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,55 @@ import Vue from 'vue'
import { showError, showInfo, TOAST_PERMANENT_TIMEOUT } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'

import pollService from '../services/pollService.js'

import {
createPoll,
getPollData,
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: string } & 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: string): 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: string }) {
try {
const response = await pollService.getPollData(token, pollId)
const response = await getPollData(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
Expand All @@ -55,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: string }) {
if (!this.debouncedFunctions[token]) {
Vue.set(this.debouncedFunctions, token, {})
}
Expand All @@ -70,15 +88,15 @@ 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 pollService.postNewPoll(
const response = await createPoll({
token,
question,
options,
resultMode,
maxVotes,
)
})
this.addPoll({ token, poll: response.data.ocs.data })

return response.data.ocs.data
Expand All @@ -87,27 +105,27 @@ export const usePollsStore = defineStore('polls', {
}
},

async submitVote({ token, pollId, optionIds }) {
async submitVote({ token, pollId, optionIds }: submitVotePayload) {
try {
const response = await pollService.submitVote(token, pollId, optionIds)
const response = await 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 }) {
async endPoll({ token, pollId }: { token: string, pollId: string }) {
try {
const response = await pollService.endPoll(token, pollId)
const response = await endPoll(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
showError(t('spreed', 'An error occurred while ending the poll'))
}
},

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

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

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

Expand All @@ -136,7 +154,7 @@ export const usePollsStore = defineStore('polls', {
Vue.set(this.pollToastsQueue, pollId, toast)
},

hidePollToast(pollId) {
hidePollToast(pollId: string) {
if (this.pollToastsQueue[pollId]) {
this.pollToastsQueue[pollId].hideToast()
Vue.delete(this.pollToastsQueue, pollId)
Expand Down
Loading

0 comments on commit b2d25cd

Please sign in to comment.