diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index a4ba9d1..8e6bcf3 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -164,4 +164,17 @@ public function storeChat(Request $request) // Rediriger avec un message de succès return redirect()->route('dashboard')->with('success', 'Discussion créée avec succès !'); } + + public function leaveChat($chatId) + { + $chat = Chat::find($chatId); + + if (!$chat) { + return response()->json(['error' => 'Chat not found.'], 404); + } + + $chat->users()->detach(auth()->id()); + + return response()->json(['message' => 'You left the chat.']); + } } diff --git a/public/source/media/1734181073_yealink_mp54-e2-teams_f_423320.jpg b/public/source/media/1734181073_yealink_mp54-e2-teams_f_423320.jpg new file mode 100644 index 0000000..ecde65e Binary files /dev/null and b/public/source/media/1734181073_yealink_mp54-e2-teams_f_423320.jpg differ diff --git a/resources/views/components/messaging/header.blade.php b/resources/views/components/messaging/header.blade.php index e2569e2..585f4a8 100644 --- a/resources/views/components/messaging/header.blade.php +++ b/resources/views/components/messaging/header.blade.php @@ -1,4 +1,11 @@ -
- Discussion Picture - -
+
+
+ Discussion Picture + +
+ +
\ No newline at end of file diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 1343706..99ade0c 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -1,16 +1,17 @@
+ +
+
@@ -20,17 +21,15 @@ let currentChatId = null; let interval = null; let allMessages = []; - const currentUserId = {{ auth()->id() }}; + // Fonction pour charger la discussion function loadChat(chatId, discussionName, discussionPicture, newOpening = true) { - const messagesContainer = document.getElementById('messages'); + const messagesContainer = document.getElementById('messages'); - document.getElementById('chat-placeholder').style.display = 'none'; - document.getElementById('chat-area').style.display = 'flex'; + // Masquer le placeholder et afficher la zone de chat + document.getElementById('chat-placeholder').style.display = 'none'; + document.getElementById('chat-area').style.display = 'flex'; - if (currentChatId !== chatId) { - allMessages = []; - messagesContainer.innerHTML = ''; // Vérifier si la discussion est déjà chargée if (currentChatId !== chatId) { // Réinitialiser la liste de tout les messages @@ -51,7 +50,7 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true) if (headerTitle) headerTitle.textContent = discussionName; const headerImage = document.querySelector('.headerImage'); - if(headerImage) headerImage.src = discussionPicture; + if (headerImage) headerImage.src = discussionPicture; } fetch(`/chat/${chatId}/messages`, { @@ -109,7 +108,7 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true) Your browser does not support the audio element. `; - }else { + } else { mediaElement += ` `; } @@ -119,7 +118,7 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true) ${message.message}

`; - messageElement.innerHTML = mediaElement; + messageElement.innerHTML = mediaElement; } messagesContainer.appendChild(messageElement); }); @@ -145,91 +144,7 @@ function loadChat(chatId, discussionName, discussionPicture, newOpening = true) .catch(error => console.error('Erreur:', error)) } - currentChatId = chatId; - - if (newOpening) { - const headerTitle = document.querySelector('.headerTitle'); - if (headerTitle) headerTitle.textContent = discussionName; - - const headerImage = document.querySelector('.headerImage'); - if (headerImage) headerImage.src = discussionPicture; - } - - fetch(`/chat/${chatId}/messages`, { - method: 'GET', - headers: { - 'X-Requested-With': 'XMLHttpRequest', - 'X-CSRF-TOKEN': '{{ csrf_token() }}' - } - }) - .then(response => response.json()) - .then(data => { - let newMessages = []; - - if (JSON.stringify(allMessages) === JSON.stringify(data.messages)) { - return; - } else if (allMessages.length === 0) { - newMessages = data.messages; - } else { - newMessages = data.messages.filter(message => !allMessages.some(m => m.id === message.id)); - } - - const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop === messagesContainer.clientHeight; - - newMessages.forEach(message => { - const isCurrentUser = message.user_id === currentUserId; - const messageElement = document.createElement('div'); - messageElement.className = `flex ${isCurrentUser ? 'justify-end' : 'justify-start'} mb-2`; - - let messageContent = ` -

- ${message.user.name} - ${message.message} -

- `; - - if (message.media_url) { - if (message.media_url.endsWith('.mp4')) { - messageContent = ` - `; - } else if (message.media_url.endsWith('.mp3')) { - messageContent = ` - `; - } else { - messageContent = ` - -

${message.message}

`; - } - } - - messageElement.innerHTML = messageContent; - messagesContainer.appendChild(messageElement); - }); - - allMessages = data.messages; - - const mediaElements = document.querySelectorAll('img, video'); - mediaElements.forEach(mediaElement => { - mediaElement.addEventListener('load', () => { - if (isAtBottom) { - scrollToBottom(); - } - }); - }); - - if (isAtBottom) { - scrollToBottom(); - } - }) - .catch(error => console.error('Erreur:', error)); -} - - + // Fonction pour démarrer la mise à jour automatique des messages function startAutoRefresh(intervalTime = 500) { if (interval) clearInterval(interval); interval = setInterval(() => { @@ -253,29 +168,13 @@ function sendMessage() { message: messageContent }) }) - .then(response => { - return response.json(); - }) - .then(data => { - const errorMessage = document.getElementById('error-message'); - if (data.error) { - errorMessage.textContent = data.error; - errorMessage.style.display = 'block'; - } else { - - document.getElementById('message-content').value = ''; - errorMessage.style.display = 'none'; - } + .then(() => { + document.getElementById('message-content').value = ''; }) - .catch(error => { - console.error('Erreur:', error); - const errorMessage = document.getElementById('error-message'); - errorMessage.textContent = 'Une erreur est survenue. Veuillez réessayer.'; - errorMessage.style.display = 'block'; - }); + .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) { @@ -283,5 +182,43 @@ function scrollToBottom() { } } - startAutoRefresh(); - +function leaveChat() { + if (!currentChatId) { + alert("Aucune discussion sélectionnée."); + return; + } + + fetch(`/chat/${currentChatId}/leave`, { + 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 quitter la conversation."); + } + }) + .then(data => { + 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."); + }); +} + + + startAutoRefresh(); // Démarrer l'auto-rafraîchissement + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 4e44660..e4c1ae0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,6 +23,7 @@ Route::get('/chat/{discussion}/messages', [ChatController::class, 'getMessages']); 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::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');