Skip to content

Commit

Permalink
consolidate requests upon voting
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <[email protected]>
  • Loading branch information
dartcafe committed Aug 31, 2024
1 parent 8f1961b commit 5ac9ce2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 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
6 changes: 4 additions & 2 deletions src/js/store/modules/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ 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
Expand Down

0 comments on commit 5ac9ce2

Please sign in to comment.