55
66<script setup lang="ts">
77import { computed } from ' vue'
8+ import { AxiosError } from ' @nextcloud/axios'
9+
10+ import { t } from ' @nextcloud/l10n'
811import { showSuccess , showError } from ' @nextcloud/dialogs'
912
13+ import VoteIndicator from ' ./VoteIndicator.vue'
1014import { usePollStore } from ' ../../stores/poll.ts'
11- import { useVotesStore } from ' ../../stores/votes.ts'
15+ import { Answer , useVotesStore } from ' ../../stores/votes.ts'
1216import { Option , User } from ' ../../Types/index.ts'
1317
14- import { t } from ' @nextcloud/l10n'
15- import VoteIndicator from ' ./VoteIndicator.vue'
16- import { AxiosError } from ' @nextcloud/axios'
17-
1818interface Props {
1919 option: Option
2020 user: User
2121}
2222
23+ export type richAnswer = {
24+ name: Answer
25+ translated: string
26+ }
27+
28+ const richAnswers: { [key in Answer ]: richAnswer } = {
29+ yes: { name: ' yes' , translated: t (' polls' , ' Yes' ) },
30+ maybe: { name: ' maybe' , translated: t (' polls' , ' Maybe' ) },
31+ no: { name: ' no' , translated: t (' polls' , ' No' ) },
32+ ' ' : { name: ' ' , translated: t (' polls' , ' No answer' ) },
33+ }
34+
2335const { option, user } = defineProps <Props >()
2436
2537const pollStore = usePollStore ()
2638const votesStore = useVotesStore ()
2739
28- const thisVote = computed (() =>
40+ const vote = computed (() =>
2941 votesStore .getVote ({
3042 option ,
3143 user ,
3244 }),
3345)
3446
35- const iconAnswer = computed (() => {
36- if ([' no' , ' ' ].includes (thisVote .value .answer )) {
37- return pollStore . isClosed && option . confirmed ? ' no ' : ' '
47+ const nextAnswer = computed < richAnswer > (() => {
48+ if ([' no' , ' ' ].includes (vote .value .answer )) {
49+ return richAnswers . yes
3850 }
39- return thisVote .value .answer
40- })
4151
42- const nextAnswer = computed (() => {
43- if (pollStore .answerSequence .indexOf (thisVote .value .answer ) < 0 ) {
44- return pollStore .answerSequence [1 ]
52+ if (vote .value .answer === ' yes' && pollStore .configuration .allowMaybe ) {
53+ return richAnswers .maybe
4554 }
46- return pollStore .answerSequence [
47- (pollStore .answerSequence .indexOf (thisVote .value .answer ) + 1 )
48- % pollStore .answerSequence .length
49- ]
50- })
51- const nextAnswerTranslated = computed (() => {
52- if (nextAnswer .value === ' yes' ) {
53- return t (' polls' , ' Yes' )
54- }
55- if (nextAnswer .value === ' maybe' ) {
56- return t (' polls' , ' Maybe' )
57- }
58- return t (' polls' , ' No' )
55+
56+ return pollStore .configuration .useNo ? richAnswers .no : richAnswers [' ' ]
5957})
6058
6159async function setVote() {
6260 try {
6361 await votesStore .set ({
6462 option ,
65- setTo: nextAnswer .value ,
63+ setTo: nextAnswer .value . name ,
6664 })
6765 showSuccess (t (' polls' , ' Vote saved' ), { timeout: 2000 })
6866 } catch (error ) {
@@ -78,15 +76,15 @@ async function setVote() {
7876<template >
7977 <button
8078 class =" vote-button active"
81- :class =" [thisVote .answer]"
79+ :class =" [vote .answer]"
8280 :aria-label ="
83- t('polls', 'Vote {nextAnswer} for {option}', {
81+ t('polls', 'Click to vote with {nextAnswer} for option {option}', {
8482 option: option.text,
85- nextAnswer: nextAnswerTranslated ,
83+ nextAnswer: nextAnswer.translated ,
8684 })
8785 "
8886 @click =" setVote()" >
89- <VoteIndicator :answer =" iconAnswer " />
87+ <VoteIndicator :answer =" vote . answer " />
9088 </button >
9189</template >
9290
0 commit comments