Skip to content

Commit

Permalink
Merge pull request #3676 from nextcloud/fix/multiple-click-vote-item
Browse files Browse the repository at this point in the history
fix: disable vote option while loading
  • Loading branch information
dartcafe authored Sep 5, 2024
2 parents 3e7ce64 + 2837614 commit 78b97c2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
13 changes: 11 additions & 2 deletions lib/Controller/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Service\OptionService;
use OCA\Polls\Service\PollService;
use OCA\Polls\Service\VoteService;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
Expand All @@ -21,7 +23,9 @@ class VoteController extends BaseController {
public function __construct(
string $appName,
IRequest $request,
private VoteService $voteService
private VoteService $voteService,
private PollService $pollService,
private OptionService $optionService,
) {
parent::__construct($appName, $request);
}
Expand All @@ -44,7 +48,12 @@ public function list(int $pollId): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
public function set(int $optionId, string $setTo): JSONResponse {
return $this->response(fn () => ['vote' => $this->voteService->set($optionId, $setTo)]);
$option = $this->optionService->get($optionId);
return $this->response(fn () => [
'vote' => $this->voteService->set($optionId, $setTo),
'poll' => $this->pollService->get($option->getPollId()),
'options' => $this->optionService->list($option->getPollId()),
]);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/OptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function __construct(
$this->options = [];
}

public function get(int $optionId): Option {
return $this->optionMapper->find($optionId);
}
/**
* Get all options of given poll
*
Expand Down
17 changes: 11 additions & 6 deletions lib/Service/VoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,21 @@ public function set(int $optionId, string $setTo): ?Vote {
$option = $this->optionMapper->find($optionId);
$poll = $this->pollMapper->find($option->getPollId());
$poll->request(Poll::PERMISSION_VOTE_EDIT);

if ($setTo === Vote::VOTE_YES) {
$this->checkLimits($option);
}
// delete no votes, if poll setting is set to useNo === 0
$deleteVoteInsteadOfNoVote = in_array(trim($setTo), [Vote::VOTE_NO, '']) && !boolval($poll->getUseNo());

try {
$this->vote = $this->voteMapper->findSingleVote($poll->getId(), $option->getPollOptionText(), $this->acl->getCurrentUserId());

if ($setTo === $this->vote->getVoteAnswer()) {
return $this->vote;
}

if ($setTo === Vote::VOTE_YES) {
$this->checkLimits($option);
}

// delete no votes, if poll setting is set to useNo === 0
$deleteVoteInsteadOfNoVote = in_array(trim($setTo), [Vote::VOTE_NO, '']) && !boolval($poll->getUseNo());

if ($deleteVoteInsteadOfNoVote) {
$this->vote->setVoteAnswer('');
$this->voteMapper->delete($this->vote);
Expand Down
1 change: 0 additions & 1 deletion src/js/components/VoteTable/VoteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export default {
showSuccess(t('polls', 'Vote saved'), { timeout: 2000 })
} catch (e) {
showError(t('polls', 'Error saving vote'))
}
},
},
Expand Down
7 changes: 5 additions & 2 deletions src/js/store/modules/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ const actions = {
response = await VotesAPI.setVote(payload.option.id, payload.setTo)
}
context.commit('setItem', { option: payload.option, pollId: context.rootState.poll.id, vote: response.data.vote })
context.dispatch('options/list', null, { root: true })
context.dispatch('poll/get', null, { root: true })
context.commit('options/set', { options: response.data.options }, { root: true })
context.commit('poll/set', { poll: response.data.poll }, { root: true })
// context.dispatch('options/list', null, { root: true })
// context.dispatch('poll/get', null, { root: true })
return response
} catch (error) {
if (error?.code === 'ERR_CANCELED') return
if (error.response.status === 409) {
context.dispatch('list')
context.dispatch('options/list', null, { root: true })
context.dispatch('poll/get', null, { root: true })
} else {
Logger.error('Error setting vote', { error, payload })
throw error
Expand Down

0 comments on commit 78b97c2

Please sign in to comment.