- new file pour handler request allListUser
    - add function on button new conversation in chat
    - remove cookies from js
Django
    - fix new_msg.sender.id
This commit is contained in:
Kum1ta
2024-08-26 01:27:32 +02:00
parent b08a90226a
commit 1c9e985320
5 changed files with 70 additions and 38 deletions

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/04 19:17:54 by edbernar #+# #+# */
/* Updated: 2024/08/25 21:24:06 by edbernar ### ########.fr */
/* Updated: 2024/08/26 00:40:11 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -86,7 +86,6 @@ async function displayAllMessage(divMessageListChatHome)
newDiv = document.createElement("div");
contentNode = document.createTextNode(element.content);
dateNode = document.createTextNode(element.date);
console.log(element.from, userMeInfo.id);
newDiv.classList.add(element.from === userMeInfo.id ? "meMessage" : "opponentMessage");
tmp = document.createElement("p");
tmp.classList.add("content");

View File

@ -6,11 +6,12 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/04 19:21:10 by edbernar #+# #+# */
/* Updated: 2024/08/24 23:31:19 by edbernar ### ########.fr */
/* Updated: 2024/08/26 01:24:28 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import { waitForUserList } from "/static/javascript/typeResponse/typePrivateListUser.js";
import { waitForallListUser } from "/static/javascript/typeResponse/typeAllListUser.js";
import { userList } from "/static/javascript/typeResponse/typePrivateListUser.js";
import { showPrivateChat } from "/static/javascript/liveChat/showPrivateChat.js";
import { sendRequest } from "/static/javascript/websocket.js";
@ -41,13 +42,33 @@ async function showListUser() {
divMessageListChatHome.appendChild(user);
});
}
divMessageListChatHome.innerHTML += "<p style='text-align: center; margin-top: 20px;'>New conversation +</p>";
divMessageListChatHome.innerHTML += "<p id='newConversation' style='text-align: center; margin-top: 20px; cursor: pointer;'>New conversation +</p>";
divUser = document.getElementsByClassName("user");
for (let i = 0; i < divUser.length; i++) {
divUser[i].addEventListener("click", async () => {
await showPrivateChat(userList[i]);
});
}
document.getElementById('newConversation').addEventListener('mouseup', () => {
divMessageListChatHome.innerText = 'Loading...';
sendRequest("get_all_list_user", {});
waitForallListUser().then((listUser) => {
divMessageListChatHome.innerText = '';
listUser.forEach(element => {
let user = document.createElement("div");
user.classList.add("user");
user.innerHTML = `
<div class="status ${element.status}">
<img>
</div>
<h3></h3>
`
user.querySelector("img").src = element.pfp;
user.querySelector("h3").innerText = element.name;
divMessageListChatHome.appendChild(user);
})
})
});
}
export { showListUser };

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* typeAllListUser.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 01:00:35 by edbernar #+# #+# */
/* Updated: 2024/08/26 01:11:49 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
let allListUser = [];
let allListUserAvailable = false;
let allListUserResolve = null;
function waitForallListUser() {
return new Promise((resolve) => {
if (allListUserAvailable)
resolve();
else
allListUserResolve = resolve;
});
}
function typePrivateListMessage(list) {
allListUser = list;
allListUserAvailable = true;
if (allListUserResolve)
{
allListUserResolve(allListUser);
allListUserResolve = null;
allListUserAvailable = false;
}
}
export { waitForallListUser, typePrivateListMessage, allListUser };

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/31 22:17:24 by edbernar #+# #+# */
/* Updated: 2024/08/25 18:09:25 by edbernar ### ########.fr */
/* Updated: 2024/08/25 23:25:30 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,23 +28,7 @@ const errorFunction = [typeErrorInvalidPassword, typeErrorInvalidToken42, typeEr
let status = 0;
function getCookie(name)
{
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
let token = null;
if (parts.length === 2)
{
token = parts.pop().split(';').shift();
token = token.substring(1, token.length - 1);
}
return (token);
}
socket.onopen = () => {
let token = getCookie("token");
status = 1;
console.log('Connected');
};
@ -92,21 +76,10 @@ function sendRequest(type, content) {
coc = JSON.stringify(content);
else
coc = content;
if (getCookie("token"))
{
socket.send(JSON.stringify({
type: type,
token: getCookie("token"),
content: content
}));
}
else
{
socket.send(JSON.stringify({
type: type,
content: content
}));
}
socket.send(JSON.stringify({
type: type,
content: content
}));
}
export { socket, sendRequest };