Skip to content

Commit 46d1afe

Browse files
author
vagrant
committed
Vote feature with AJAX
1 parent b173665 commit 46d1afe

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

app/Http/Controllers/VotesController.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace TeachMe\Http\Controllers;
22

3-
use TeachMe\Entities\Ticket;
3+
use Illuminate\Http\Request;
44
use TeachMe\Repositories\TicketRepository;
55
use TeachMe\Repositories\VoteRepository;
66

@@ -18,18 +18,26 @@ public function __construct(
1818
$this->voteRepository = $voteRepository;
1919
}
2020

21-
public function submit($id)
21+
public function submit($id, Request $request)
2222
{
2323
$ticket = $this->ticketRepository->findOrFail($id);
24-
$this->voteRepository->vote(currentUser(), $ticket);
24+
$success = $this->voteRepository->vote(currentUser(), $ticket);
25+
26+
if ($request->ajax()) {
27+
return response()->json(compact('success'));
28+
}
2529

2630
return redirect()->back();
2731
}
2832

29-
public function destroy($id)
33+
public function destroy($id, Request $request)
3034
{
3135
$ticket = $this->ticketRepository->findOrFail($id);
32-
$this->voteRepository->unvote(currentUser(), $ticket);
36+
$success = $this->voteRepository->unvote(currentUser(), $ticket);
37+
38+
if ($request->ajax()) {
39+
return response()->json(compact('success'));
40+
}
3341

3442
return redirect()->back();
3543
}

app/Repositories/VoteRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public function vote(User $user, Ticket $ticket)
1717

1818
public function unvote(User $user, Ticket $ticket)
1919
{
20+
if ( ! $user->hasVoted($ticket)) return false;
21+
2022
$user->voted()->detach($ticket);
23+
return true;
2124
}
2225

2326
}

public/assets/js/app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
$(document).ready(function () {
2+
3+
$('.btn-vote').click(function (e) {
4+
e.preventDefault();
5+
6+
var form = $('#form-vote');
7+
8+
var button = $(this);
9+
var ticket = button.closest('.ticket');
10+
var id = ticket.data('id');
11+
12+
var action = form.attr('action').replace(':id', id);
13+
14+
button.addClass('hidden');
15+
16+
$.post(action, form.serialize(), function (response) {
17+
//alert
18+
//update count votes
19+
ticket.find('.btn-unvote').removeClass('hidden');
20+
}).fail(function () {
21+
//print error message
22+
button.removeClass('hidden');
23+
});
24+
});
25+
26+
});

resources/views/layout.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
<!-- Scripts -->
5353
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
5454
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
55+
<script src="{{ asset('assets/js/app.js') }}"></script>
5556
</body>
5657
</html>

resources/views/tickets/list.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
</div>
3030
</div>
3131

32+
{!! Form::open(['id' => 'form-vote', 'route' => ['votes.submit', ':id'], 'method' => 'POST']) !!}
33+
{!! Form::close() !!}
34+
35+
{!! Form::open(['id' => 'form-unvote', 'route' => ['votes.destroy', ':id'], 'method' => 'DELETE']) !!}
36+
{!! Form::close() !!}
37+
3238
@endsection

resources/views/tickets/partials/item.blade.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
<div data-id="25" class="well well-sm request">
1+
<div data-id="{{ $ticket->id }}" class="well well-sm ticket">
22
<h4 class="list-title">
33
{{ $ticket->title }}
44
@include('tickets/partials/status', compact('ticket'))
55
</h4>
6+
@if (Auth::check())
67
<p>
7-
{{--
8-
<a href="#" class="btn btn-primary btn-vote" title="Votar por este tutorial">
8+
<a href="#"
9+
{!! Html::classes(['btn btn-primary btn-vote', 'hidden' => currentUser()->hasVoted($ticket)]) !!}
10+
title="Votar por este tutorial">
911
<span class="glyphicon glyphicon-thumbs-up"></span> Votar
1012
</a>
11-
<a href="#" class="btn btn-hight btn-unvote hide">
12-
<span class="glyphicon glyphicon-thumbs-down"></span> No votar
13+
<a href="#"
14+
{!! Html::classes(['btn btn-hight btn-unvote', 'hidden' => !currentUser()->hasVoted($ticket)]) !!}
15+
title="Quitar el voto a este tutorial">
16+
<span class="glyphicon glyphicon-thumbs-down"></span> Quitar voto
1317
</a>
14-
--}}
1518
<a href="{{ route('tickets.details', $ticket) }}">
1619
<span class="votes-count">{{ $ticket->num_votes }} votos</span>
1720
- <span class="comments-count">{{ $ticket->num_comments }} comentarios</span>.
1821
</a>
1922
</p>
23+
@endif
2024
<p class="date-t">
2125
<span class="glyphicon glyphicon-time"></span> {{ $ticket->created_at->format('d/m/y h:ia') }}
2226
Por {{ $ticket->author->name }}

0 commit comments

Comments
 (0)