Skip to content

Commit

Permalink
delete message
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Dec 14, 2024
1 parent c4ac413 commit 3af89d5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
}
}
38 changes: 34 additions & 4 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -206,19 +205,50 @@ 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 => {
console.error("Erreur :", error);
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();
</script>
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 3af89d5

Please sign in to comment.