@@ -9,37 +9,55 @@ import Vue from 'vue'
9
9
import { showError , showInfo , TOAST_PERMANENT_TIMEOUT } from '@nextcloud/dialogs'
10
10
import { t } from '@nextcloud/l10n'
11
11
12
- import pollService from '../services/pollService.js'
13
-
12
+ import {
13
+ createPoll ,
14
+ getPollData ,
15
+ submitVote ,
16
+ endPoll ,
17
+ } from '../services/pollService.ts'
18
+ import type {
19
+ ChatMessage ,
20
+ createPollParams ,
21
+ Poll , votePollParams
22
+ } from '../types/index.ts'
23
+
24
+ type createPollPayload = { token : string } & createPollParams
25
+ type submitVotePayload = { token : string , pollId : string } & Pick < votePollParams , 'optionIds' >
26
+ type State = {
27
+ polls : Record < string , Record < string , Poll > > ,
28
+ debouncedFunctions : Record < string , Record < string , ( ) => void > > ,
29
+ activePoll : null ,
30
+ pollToastsQueue : Record < string , ReturnType < typeof showInfo > > ,
31
+ }
14
32
export const usePollsStore = defineStore ( 'polls' , {
15
- state : ( ) => ( {
33
+ state : ( ) : State => ( {
16
34
polls : { } ,
17
35
debouncedFunctions : { } ,
18
36
activePoll : null ,
19
37
pollToastsQueue : { } ,
20
38
} ) ,
21
39
22
40
getters : {
23
- getPoll : ( state ) => ( token , pollId ) => {
41
+ getPoll : ( state ) => ( token : string , pollId : string ) : Poll => {
24
42
return state . polls [ token ] ?. [ pollId ]
25
43
} ,
26
44
27
- isNewPoll : ( state ) => ( pollId ) => {
45
+ isNewPoll : ( state ) => ( pollId : number ) => {
28
46
return state . pollToastsQueue [ pollId ] !== undefined
29
47
} ,
30
48
} ,
31
49
32
50
actions : {
33
- addPoll ( { token, poll } ) {
51
+ addPoll ( { token, poll } : { token : string , poll : Poll } ) {
34
52
if ( ! this . polls [ token ] ) {
35
53
Vue . set ( this . polls , token , { } )
36
54
}
37
55
Vue . set ( this . polls [ token ] , poll . id , poll )
38
56
} ,
39
57
40
- async getPollData ( { token, pollId } ) {
58
+ async getPollData ( { token, pollId } : { token : string , pollId : string } ) {
41
59
try {
42
- const response = await pollService . getPollData ( token , pollId )
60
+ const response = await getPollData ( token , pollId )
43
61
this . addPoll ( { token, poll : response . data . ocs . data } )
44
62
} catch ( error ) {
45
63
console . error ( error )
@@ -55,7 +73,7 @@ export const usePollsStore = defineStore('polls', {
55
73
* @param { string } root0.token The token of the conversation
56
74
* @param { number } root0.pollId The id of the poll
57
75
*/
58
- debounceGetPollData ( { token, pollId } ) {
76
+ debounceGetPollData ( { token, pollId } : { token : string , pollId : string } ) {
59
77
if ( ! this . debouncedFunctions [ token ] ) {
60
78
Vue . set ( this . debouncedFunctions , token , { } )
61
79
}
@@ -70,15 +88,15 @@ export const usePollsStore = defineStore('polls', {
70
88
this . debouncedFunctions [ token ] [ pollId ] ( )
71
89
} ,
72
90
73
- async createPoll ( { token, question, options, resultMode, maxVotes } ) {
91
+ async createPoll ( { token, question, options, resultMode, maxVotes } : createPollPayload ) {
74
92
try {
75
- const response = await pollService . postNewPoll (
93
+ const response = await createPoll ( {
76
94
token,
77
95
question,
78
96
options,
79
97
resultMode,
80
98
maxVotes,
81
- )
99
+ } )
82
100
this . addPoll ( { token, poll : response . data . ocs . data } )
83
101
84
102
return response . data . ocs . data
@@ -87,27 +105,27 @@ export const usePollsStore = defineStore('polls', {
87
105
}
88
106
} ,
89
107
90
- async submitVote ( { token, pollId, optionIds } ) {
108
+ async submitVote ( { token, pollId, optionIds } : submitVotePayload ) {
91
109
try {
92
- const response = await pollService . submitVote ( token , pollId , optionIds )
110
+ const response = await submitVote ( token , pollId , optionIds )
93
111
this . addPoll ( { token, poll : response . data . ocs . data } )
94
112
} catch ( error ) {
95
113
console . error ( error )
96
114
showError ( t ( 'spreed' , 'An error occurred while submitting your vote' ) )
97
115
}
98
116
} ,
99
117
100
- async endPoll ( { token, pollId } ) {
118
+ async endPoll ( { token, pollId } : { token : string , pollId : string } ) {
101
119
try {
102
- const response = await pollService . endPoll ( token , pollId )
120
+ const response = await endPoll ( token , pollId )
103
121
this . addPoll ( { token, poll : response . data . ocs . data } )
104
122
} catch ( error ) {
105
123
console . error ( error )
106
124
showError ( t ( 'spreed' , 'An error occurred while ending the poll' ) )
107
125
}
108
126
} ,
109
127
110
- setActivePoll ( { token, pollId, name } ) {
128
+ setActivePoll ( { token, pollId, name } : { token : string , pollId : string , name : string } ) {
111
129
Vue . set ( this , 'activePoll' , { token, id : pollId , name } )
112
130
} ,
113
131
@@ -117,7 +135,7 @@ export const usePollsStore = defineStore('polls', {
117
135
}
118
136
} ,
119
137
120
- addPollToast ( { token, message } ) {
138
+ addPollToast ( { token, message } : { token : string , message : ChatMessage } ) {
121
139
const pollId = message . messageParameters . object . id
122
140
const name = message . messageParameters . object . name
123
141
@@ -136,7 +154,7 @@ export const usePollsStore = defineStore('polls', {
136
154
Vue . set ( this . pollToastsQueue , pollId , toast )
137
155
} ,
138
156
139
- hidePollToast ( pollId ) {
157
+ hidePollToast ( pollId : string ) {
140
158
if ( this . pollToastsQueue [ pollId ] ) {
141
159
this . pollToastsQueue [ pollId ] . hideToast ( )
142
160
Vue . delete ( this . pollToastsQueue , pollId )
0 commit comments