Skip to content

Commit

Permalink
Better deleted messages
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyNadd committed Dec 15, 2024
1 parent 5a8c9a0 commit ff020fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
9 changes: 0 additions & 9 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,4 @@ public function deleteMessage($discussionId, $messageId)

return response()->json(['message' => 'Message deleted successfully.']);
}

public function getDeletedMessages($chatId)
{
$deletedMessages = Message::onlyTrashed()
->where('discussion_id', $chatId)
->pluck('id');

return response()->json(['deletedMessages' => $deletedMessages]);
}
}
46 changes: 19 additions & 27 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true)
.then(response => response.json())
.then(data => {
let newMessages = [];
let deletedMessages = [];
// Si la liste de tout les messages correspond à la liste actuelle
if (JSON.stringify(allMessages) === JSON.stringify(data.messages)) {
return;
Expand All @@ -73,6 +74,9 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true)
newMessages = data.messages.filter(message => !allMessages.some(m => m.id === message.id));
}
// Récupérer la liste des messages supprimés
deletedMessages = allMessages.filter(message => !data.messages.some(m => m.id === message.id));
// Récupérer la position dans le scroll et vérifier si on est tout en bas
// Utile pour savoir si on doit défiler jusqu'en bas après l'ajout des nouveaux messages ou si l'utilisateur est entrain de consulter des anciens messages
const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop <= messagesContainer.clientHeight + 0.6;
Expand Down Expand Up @@ -156,6 +160,14 @@ class="ml-2 text-red-500 hover:text-red-700 focus:outline-none"
messagesContainer.appendChild(messageElement);
});
// Parcourir les messages supprimés et enlever la div du message correspondant
deletedMessages.forEach(message => {
const messageElement = document.getElementById(`message-div-${message.id}`);
if (messageElement) {
messageElement.innerHTML = `<p class="text-gray-500 italic">Ce message a été supprimé.</p>`;
}
});
// Mettre à jour la liste de tout les messages
allMessages = data.messages;
Expand All @@ -178,34 +190,14 @@ class="ml-2 text-red-500 hover:text-red-700 focus:outline-none"
}
function startAutoRefresh(intervalTime = 5000) {
if (interval) clearInterval(interval);
interval = setInterval(() => {
if (currentChatId) {
loadChat(currentChatId, null, null, false);
if (interval) clearInterval(interval);
fetch(`/chat/${currentChatId}/deleted-messages`, {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
})
.then(response => response.json())
.then(data => {
const deletedMessages = data.deletedMessages || [];
deletedMessages.forEach(messageId => {
const messageElement = document.getElementById(`message-div-${messageId}`);
if (messageElement) {
messageElement.innerHTML = `<p class="text-gray-500 italic">Ce message a été supprimé.</p>`;
}
});
})
.catch(error => console.error('Erreur lors de la récupération des suppressions:', error));
}
}, intervalTime);
}
interval = setInterval(() => {
if (currentChatId) {
loadChat(currentChatId, null, null, false);
}
}, intervalTime);
}
function sendMessage() {
const messageContent = document.getElementById('message-content').value;
Expand Down
1 change: 0 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
Route::post('/chat/{discussion}/capsule', [ChatController::class, 'storeCapsule']);
Route::delete('/chat/{discussion}/leave', [ChatController::class, 'leaveChat']);
Route::delete('/chat/{discussion}/{message}/delete', [ChatController::class, 'deleteMessage']);
Route::get('/chat/{chatId}/deleted-messages', [ChatController::class, 'getDeletedMessages']);
Route::post('/chats', [ChatController::class, 'storeChat'])->name('chats.store');
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Expand Down

0 comments on commit ff020fe

Please sign in to comment.