Up & Down Voting
With each row as its own component, it's easy to add up & down voting + keep track of which items have been voted on. This uses a LiveAction to save everything with Ajax.
Banana 🍌 | Votes: 99 | |
---|---|---|
Apple 🍎 | Votes: 10 | |
Hamburger 🍔 | Votes: 65 | |
Watermelon 🍉 | Votes: 58 | |
Cheese 🧀 | Votes: 94 | |
Pizza 🍕 | Votes: 99 | |
Pretzel 🥨 | Votes: 37 | |
Donut 🍩 | Votes: 67 | |
Pineapple 🍍 | Votes: 74 | |
Popcorn 🍿 | Votes: 0 | |
Egg 🍳 | Votes: 68 | |
Taco 🌮 | Votes: 10 | |
Ice Cream 🍦 | Votes: 43 | |
Cookie 🍪 | Votes: 4 |
// ... use statements hidden - click to show
#[AsLiveComponent]
class FoodVote extends AbstractController
{
use DefaultActionTrait;
#[LiveProp]
public Food $food;
#[LiveProp]
public bool $hasVoted = false;
public function __construct(private FoodRepository $foodRepository)
{
}
#[LiveAction]
public function vote(#[LiveArg] string $direction)
{
if ('up' === $direction) {
$this->food->upVote();
} else {
$this->food->downVote();
}
$this->foodRepository->add($this->food, true);
$this->hasVoted = true;
}
}
<tr {{ attributes }}>
<th>{{ food.name }}</th>
<td>
Votes: {{ food.votes }}
</td>
<td style="width: 250px;">
{% if hasVoted %}
<div class="alert alert-success">
Thanks for voting! <twig:ux:icon name="circle-check" />
</div>
{% else %}
<button
type="button"
class="btn btn-secondary"
data-action="live#action"
data-live-action-param="vote"
data-live-direction-param="up"
>
<twig:ux:icon name="arrow-up" />
</button>
<button
type="button"
class="btn btn-secondary"
data-action="live#action"
data-live-action-param="vote"
data-live-direction-param="down"
>
<twig:ux:icon name="arrow-down" />
</button>
{% endif %}
</td>
</tr>
Author
weaverryan
Published
2022-06-17