Merge pull request #187 from dansup/frontend-ui-refactor
Fix like count
This commit is contained in:
commit
3953fb0c42
4 changed files with 42 additions and 33 deletions
|
@ -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++;
|
||||
}
|
||||
|
||||
$like = new Like;
|
||||
$like->profile_id = $profile->id;
|
||||
$like->status_id = $status->id;
|
||||
$like->save();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
2
public/js/app.js
vendored
2
public/js/app.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"/js/app.js": "/js/app.js?id=a688664f2a2b0ff3b7c3",
|
||||
"/js/app.js": "/js/app.js?id=478eacc17a0ea6fba319",
|
||||
"/css/app.css": "/css/app.css?id=ede5557443f098707138",
|
||||
"/js/timeline.js": "/js/timeline.js?id=fcc86a9419b33a821c23"
|
||||
}
|
46
resources/assets/js/components/likebutton.js
vendored
46
resources/assets/js/components/likebutton.js
vendored
|
@ -30,30 +30,30 @@ $(document).ready(function() {
|
|||
e.preventDefault();
|
||||
var el = $(this);
|
||||
var id = el.data('id');
|
||||
var res = axios.post('/i/like', {item: id});
|
||||
var likes = ls.get('likes');
|
||||
var action = false;
|
||||
var counter = el.parents().eq(2).find('.like-count');
|
||||
var count = parseInt(counter.text());
|
||||
var heart = el.find('.status-heart');
|
||||
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 = res.data.count;
|
||||
var heart = el.find('.status-heart');
|
||||
|
||||
if(likes.indexOf(id) > -1) {
|
||||
heart.addClass('far fa-heart').removeClass('fas fa-heart');
|
||||
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';
|
||||
}
|
||||
if(likes.indexOf(id) > -1) {
|
||||
heart.addClass('far fa-heart').removeClass('fas fa-heart');
|
||||
likes = likes.filter(function(item) {
|
||||
return item !== id
|
||||
});
|
||||
counter.text(count);
|
||||
action = 'unlike';
|
||||
} else {
|
||||
heart.addClass('fas fa-heart').removeClass('far fa-heart');
|
||||
likes.push(id);
|
||||
counter.text(count);
|
||||
action = 'like';
|
||||
}
|
||||
|
||||
ls.set('likes', likes);
|
||||
console.log(action + ' - ' + $(this).data('id') + ' like event');
|
||||
ls.set('likes', likes);
|
||||
console.log(action + ' - ' + $(this).data('id') + ' like event');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue