iu9-ca-web-chat/assets/js/list-rooms.js

129 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-08-11 12:17:53 +00:00
let currentRoom = null;
let currentHistoryId = 0;
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
function openModal(roomName) {
currentRoom = roomName;
document.getElementById('passwordModal').style.display = 'block';
}
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
function closeModal() {
document.getElementById('passwordModal').style.display = 'none';
}
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
function openCreateRoomModal() {
document.getElementById('createRoomModal').style.display = 'block';
2024-08-05 11:42:32 +00:00
}
2024-08-11 12:17:53 +00:00
function closeCreateRoomModal() {
document.getElementById('createRoomModal').style.display = 'none';
2024-08-05 11:42:32 +00:00
}
2024-08-11 12:17:53 +00:00
async function createRoom() {
const errorElement = document.getElementById('error');
const roomName = document.getElementById('newRoomName').value.trim();
const roomNickname = document.getElementById('newRoomNickname').value.trim();
errorElement.style.display = 'none';
errorElement.textContent = '';
if (roomName === '' || roomNickname === '') {
errorElement.textContent = 'Пожалуйста, заполните все поля';
errorElement.style.display = 'block';
return;
}
const request = {
LocalHistoryId: currentHistoryId,
content: {
name: roomName,
nickname: roomNickname
}
};
try {
const response = await fetch('/internalapi/createChat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
});
const res = await response.json();
if (res.status === 0) {
addRoomToList(roomName, roomNickname);
closeCreateRoomModal();
currentHistoryId = res.update.LocalHistoryId;
window.location.href = '/chat/' + roomNickname;
} else {
throw new Error(res.error || 'Ошибка');
}
} catch (error) {
alert('Ошибка создания чата: ' + error.message);
}
2024-08-05 11:42:32 +00:00
}
2024-08-11 12:17:53 +00:00
function addRoomToList(roomName, roomNickname) {
const roomList = document.querySelector('.room-list');
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
if (existingRoomItem) {
existingRoomItem.remove();
}
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
const roomItem = document.createElement('li');
roomItem.classList.add('room-item');
roomItem.innerHTML = `
<span class="room-name">${roomName}</span>
<button class="join-button" onclick="window.location.href = '/chat/${roomNickname}'">Войти</button>
`;
roomList.appendChild(roomItem);
}
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
async function initializeRoomList() {
try {
const response = await fetch('/internalapi/getChatList', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
const res = await response.json();
2024-08-05 11:42:32 +00:00
2024-08-11 12:17:53 +00:00
if (res.status === 0) {
res.chats.forEach(chat => {
addRoomToList(chat.content.name, chat.content.nickname);
});
} else {
throw new Error(res.error || 'Неизвестная ошибка');
}
} catch (error) {
alert('Ошибка загрузки списка чатов: ' + error.message);
}
2024-08-05 11:42:32 +00:00
}
2024-08-11 12:17:53 +00:00
async function getChatID() {
const chatNickname = window.location.pathname.split('/').pop();
const response = await fetch('/internalapi/getChatList', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
const res = await response.json();
for (const chat of res.chats) {
if (chat.content.nickname === chatNickname) {
return chat.id;
}
}
return -1;
}
2024-08-11 12:17:53 +00:00
document.addEventListener('DOMContentLoaded', initializeRoomList);