Fix like count

This commit is contained in:
Daniel Supernault 2018-06-03 20:46:55 -06:00
parent 9952920aa7
commit dfe451db70
4 changed files with 40 additions and 31 deletions

View file

@ -21,21 +21,30 @@ class LikeController extends Controller
]);
$profile = Auth::user()->profile;
$status = Status::findOrFail($request->input('item'));
$status = Status::withCount('likes')->findOrFail($request->input('item'));
$count = $status->likes_count;
if($status->likes()->whereProfileId($profile->id)->count() !== 0) {
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
$like->delete();
return redirect()->back();
}
$count--;
} else {
$like = new Like;
$like->profile_id = $profile->id;
$like->status_id = $status->id;
$like->save();
$count++;
}
LikePipeline::dispatch($like);
return redirect($status->url());
if($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count];
} else {
$response = redirect($status->url());
}
return $response;
}
}

BIN
public/js/app.js vendored

Binary file not shown.

Binary file not shown.

View file

@ -30,11 +30,12 @@ $(document).ready(function() {
e.preventDefault();
var el = $(this);
var id = el.data('id');
var res = axios.post('/i/like', {item: id});
axios.post('/i/like', {item: id})
.then(function (res) {
var likes = ls.get('likes');
var action = false;
var counter = el.parents().eq(2).find('.like-count');
var count = parseInt(counter.text());
var count = res.data.count;
var heart = el.find('.status-heart');
if(likes.indexOf(id) > -1) {
@ -42,13 +43,11 @@ $(document).ready(function() {
likes = likes.filter(function(item) {
return item !== id
});
count = count == 0 ? 0 : count--;
counter.text(count);
action = 'unlike';
} else {
heart.addClass('fas fa-heart').removeClass('far fa-heart');
likes.push(id);
count++;
counter.text(count);
action = 'like';
}
@ -57,3 +56,4 @@ $(document).ready(function() {
console.log(action + ' - ' + $(this).data('id') + ' like event');
});
});
});