From 3af89d52cb3a0c0837f37f0cca27146a50c7167b Mon Sep 17 00:00:00 2001 From: Jaton David Date: Sat, 14 Dec 2024 14:38:31 +0100 Subject: [PATCH] delete message --- app/Http/Controllers/ChatController.php | 17 +++++++++++ resources/views/dashboard.blade.php | 38 ++++++++++++++++++++++--- routes/web.php | 1 + 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 8e6bcf3..b8bc3bb 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -177,4 +177,21 @@ public function leaveChat($chatId) return response()->json(['message' => 'You left the chat.']); } + + function deleteMessage($messageId) + { + $message = Message::find($messageId); + + if (!$message) { + return response()->json(['error' => 'Message not found.'], 404); + } + + if ($message->user_id !== auth()->id()) { + return response()->json(['error' => 'Unauthorized.'], 403); + } + + $message->delete(); + + return response()->json(['message' => 'Message deleted.']); + } } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 99ade0c..2fb17af 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -174,7 +174,6 @@ function sendMessage() { .catch(error => console.error('Erreur:', error)); } - // Fonction pour faire défiler les messages jusqu'en bas function scrollToBottom() { const messagesContainer = document.getElementById('messages'); if (messagesContainer) { @@ -206,11 +205,9 @@ function leaveChat() { alert(data.message || "Vous avez quitté la conversation."); currentChatId = null; - // Optionnel : mise à jour locale de l'interface document.getElementById('chat-area').style.display = 'none'; document.getElementById('chat-placeholder').style.display = 'flex'; - // Recharger la page pour mettre à jour la liste des discussions location.reload(); }) .catch(error => { @@ -218,7 +215,40 @@ function leaveChat() { alert("Une erreur s'est produite en essayant de quitter la conversation."); }); } +function deleteMessage(messageId, discussionId) { + if (!messageId || !discussionId) { + alert("Informations de message ou discussion manquantes."); + return; + } + fetch(`/chat/${discussionId}/${messageId}/delete`, { + method: 'DELETE', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }) + .then(response => { + if (response.ok) { + return response.json(); + } else { + throw new Error("Impossible de supprimer le message."); + } + }) + .then(data => { + alert(data.message || "Message supprimé."); + + const messageElement = document.getElementById(`message-${messageId}`); + if (messageElement) { + messageElement.remove(); + } + location.reload(); + }) + .catch(error => { + console.error("Erreur :", error); + alert("Une erreur s'est produite lors de la suppression du message."); + }); +} - startAutoRefresh(); // Démarrer l'auto-rafraîchissement + startAutoRefresh(); \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index e4c1ae0..edd6be5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,6 +24,7 @@ Route::post('/chat/{discussion}/messages', [ChatController::class, 'storeMessage']); 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::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');