Skip to content

Commit

Permalink
Merge pull request #84 from Hyleus/shoutbox-fix
Browse files Browse the repository at this point in the history
Fix shoutbox auto-updating
  • Loading branch information
HDVinnie authored Dec 28, 2017
2 parents a7ca7fa + b68afcb commit fe59a29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
15 changes: 13 additions & 2 deletions app/Http/Controllers/ShoutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,24 @@ public function send()
*
*
*/
public function fetch()
public function fetch($after = null)
{
if (Request::ajax()) {
$getData = Cache::remember('shoutbox_messages', 1440, function () {
return Shoutbox::orderBy('created_at', 'desc')->take(50)->get();
});

$getData = $getData->reverse();
$next_batch = null;
if ($getData->count() !== 0) {
$next_batch = $getData->last()->id;
}
if ($after !== null) {
$getData = $getData->filter(function ($value, $key) use ($after) {
return $value->id > $after;
});
}

$data = [];
$flag = false;
foreach ($getData as $messages) {
Expand Down Expand Up @@ -156,7 +167,7 @@ public function fetch()
' . ($flag ? $delete : "") . '
</p></li>';
}
return Response::json(['success' => true, 'data' => $data]);
return Response::json(['success' => true, 'data' => $data, 'next_batch' => $next_batch]);
}
}

Expand Down
23 changes: 16 additions & 7 deletions public/js/shout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var scollBox = $('.shoutbox');
var height = 0;
var next_batch = null;
$("ul li").each(function() {
height += $(this).outerHeight(true); // to include margins
});
Expand All @@ -18,13 +19,22 @@ $.ajaxSetup({
window.setInterval(function() {
$('.chat-messages .list-group');
$.ajax({
url: "shoutbox/messages",
type: 'get',
data: load_data,
dataType: 'json',
success: function(data) {
$('.chat-messages .list-group').html(data.data);
url: "shoutbox/messages/" + parseInt(next_batch),
type: 'get',
data: load_data,
dataType: 'json',
success: function(data) {
if (next_batch === null) {
next_batch = data.next_batch;
} else {
next_batch = data.next_batch;
let messages = $('.chat-messages .list-group');
data.data.forEach(function(h) {
let message = $(h);
messages.append(message);
});
}
}
});
}, 3000);

Expand All @@ -40,7 +50,6 @@ $("#chat-message").keypress(function(evt) {
data: post_data,
dataType: 'json',
success: function(data) {
$(data.data).hide().appendTo('.chat-messages .list-group').fadeIn();
$('#chat-error').addClass('hidden');
$('#chat-message').removeClass('invalid');
$('#chat-message').val('');
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
*/
Route::group(['prefix' => 'shoutbox', 'middleware' => 'auth'], function () {
Route::get('/', 'HomeController@home')->name('shoutbox-home');
Route::get('/messages', 'ShoutboxController@fetch')->name('shoutbox-fetch');
Route::get('/messages/{after?}', 'ShoutboxController@fetch')->name('shoutbox-fetch');
Route::post('/send', 'ShoutboxController@send')->name('shoutbox-send');
Route::get('/delete/{id}', 'ShoutboxController@deleteShout')->name('shout-delete');
});
Expand Down

0 comments on commit fe59a29

Please sign in to comment.