diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index ec8aad4..da8d3df 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -273,8 +273,15 @@ public function deleteMessage($discussionId, $messageId) if ($message->user_id !== auth()->id()) { return response()->json(['error' => 'Unauthorized.'], 403); } - - $message->delete(); + if (Message::find($message->id + 10000000)) + { + $capsule = Message::where('id', $messageId + 10000000)->where('chat_id', $discussionId)->first(); + $capsule->delete(); + $message->delete(); + } + else{ + $message->delete(); + } return response()->json(['message' => 'Message deleted successfully.']); } diff --git a/public/source/media/1734448809_yealink_mp54-e2-teams_f_423320.jpg b/public/source/media/1734448809_yealink_mp54-e2-teams_f_423320.jpg new file mode 100644 index 0000000..ecde65e Binary files /dev/null and b/public/source/media/1734448809_yealink_mp54-e2-teams_f_423320.jpg differ diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 57f1766..5b39409 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -190,6 +190,16 @@ class="ml-2 text-red-500 hover:text-red-700 focus:outline-none"
${prettyDate} `; + if (isCurrentUser){ + mediaElement += ` + `; + } messageElement.innerHTML = mediaElement; } @@ -352,44 +362,45 @@ function leaveChat() { * @param {number} discussionId The ID of the discussion * @returns {void} or response of the fetch */ - function deleteMessage(messageId, discussionId) { - if (!messageId || !discussionId) { - alert("Informations de message ou discussion manquantes."); - return; + async function deleteMessage(messageId, discussionId) { + if (!messageId || !discussionId) { + alert("Informations de message ou discussion manquantes."); + return; + } + console.log("messageId", messageId) + + if(messageId >= 10000000) + { + const response = await fetch(`/chat/${discussionId}/${messageId - 10000000}/delete`, { + method: 'DELETE', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }); + } + else{ + const response = await fetch(`/chat/${discussionId}/${messageId}/delete`, { + method: 'DELETE', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-TOKEN': '{{ csrf_token() }}' } + }); + } - // Send the request to delete the message - fetch(`/chat/${discussionId}/${messageId}/delete`, { - method: 'DELETE', - headers: { - 'X-Requested-With': 'XMLHttpRequest', - 'X-CSRF-TOKEN': '{{ csrf_token() }}' - } - }) - .then(response => { - if (!response.ok) { - console.error("Erreur de suppression, statut:", response.status); - throw new Error("Impossible de supprimer le message."); - } - return response.json(); - }) - .then(data => { - currentChatId = null; + if (!response.ok) { + console.error("Erreur de suppression, statut:", response.status); + throw new Error("Impossible de supprimer le message."); + } + + const messageElement = document.getElementById(`message-div-${messageId}`); + if (messageElement) { + messageElement.remove(); + } +} - document.getElementById('chat-area').style.display = 'none'; - document.getElementById('chat-placeholder').style.display = 'flex'; - location.reload(); - const messageElement = document.getElementById(`message-div-${messageId}`); - if (messageElement) { - messageElement.remove(); - } - }) - .catch(error => { - console.error("Erreur lors de la suppression :", error); - alert("Une erreur s'est produite lors de la suppression du message."); - }); -} function leaveChatWithLoad() { const loader = document.getElementById('leave-chat-loader'); @@ -409,24 +420,23 @@ function leaveChatWithLoad() }); } -function deleteMessageWithLoader(messageId, chatId) { +async function deleteMessageWithLoader(messageId, chatId) { const loader = document.getElementById(`loader-${messageId}`); - const button = event.target; - button.style.display = 'none'; - loader.style.display = 'inline'; - - - deleteMessage(messageId, chatId) - .then(() => { - loader.style.display = 'none'; - button.style.display = 'inline'; - }) - .catch((error) => { - console.error(error); - loader.style.display = 'none'; - button.style.display = 'inline'; - }); -} + const button = event.target; + + button.style.display = 'none'; + loader.style.display = 'inline'; + + try { + await deleteMessage(messageId, chatId); + } catch (error) { + console.error(error); + } finally { + loader.style.display = 'none'; + button.style.display = 'inline'; + } +} + /** * Function to update the file name in the form