2020-06-01 16:14:27 +02:00
|
|
|
import {alert, createUserPopup} from "./popups.js";
|
2020-06-01 01:19:19 +02:00
|
|
|
|
2020-05-28 22:25:55 +02:00
|
|
|
let socket = io();
|
2020-05-30 18:47:39 +02:00
|
|
|
let users = {};
|
|
|
|
let usersAdd = [];
|
2020-05-28 22:25:55 +02:00
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
async function addUser(username) {
|
2020-05-30 18:47:39 +02:00
|
|
|
let firstName, lastName;
|
|
|
|
do {
|
2020-06-01 16:14:27 +02:00
|
|
|
[firstName, lastName] = await createUserPopup(username);
|
|
|
|
} while (firstName === "" || lastName === "");
|
|
|
|
if (firstName && lastName)
|
|
|
|
socket.emit("add user", {username: username, firstName: firstName, lastName: lastName});
|
|
|
|
else
|
2020-06-01 01:19:19 +02:00
|
|
|
await alert("User creation aborted for " + username);
|
2020-05-30 18:47:39 +02:00
|
|
|
}
|
2020-05-28 22:25:55 +02:00
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
async function next() {
|
2020-05-30 18:47:39 +02:00
|
|
|
if (usersAdd.length)
|
2020-06-01 01:19:19 +02:00
|
|
|
await addUser(usersAdd.pop());
|
2020-05-30 18:47:39 +02:00
|
|
|
else
|
|
|
|
socket.emit("set service", users);
|
2020-06-01 16:14:27 +02:00
|
|
|
console.log(users)
|
2020-05-30 18:47:39 +02:00
|
|
|
}
|
2020-05-28 22:25:55 +02:00
|
|
|
|
|
|
|
function hinter(ev) {
|
|
|
|
let input = ev.target;
|
|
|
|
let min_characters = 0;
|
|
|
|
if (input.value.length < min_characters)
|
|
|
|
return;
|
2020-05-30 18:47:39 +02:00
|
|
|
socket.emit("list user", input.value);
|
2020-05-28 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 18:47:39 +02:00
|
|
|
socket.on("connected", () => {
|
|
|
|
socket.emit("list service");
|
2020-05-28 22:25:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("list service", data => {
|
2020-06-01 16:14:27 +02:00
|
|
|
for (let s in data)
|
|
|
|
document.getElementById(s).value = data[s];
|
2020-05-28 22:25:55 +02:00
|
|
|
});
|
|
|
|
|
2020-05-30 18:47:39 +02:00
|
|
|
socket.on("list user", data => {
|
2020-05-28 22:25:55 +02:00
|
|
|
let user_list = document.getElementById("user_list");
|
|
|
|
user_list.innerHTML = "";
|
2020-05-30 18:47:39 +02:00
|
|
|
for (let u of data) {
|
2020-05-28 22:25:55 +02:00
|
|
|
user_list.insertAdjacentHTML("beforeend", `<option value="${u}">`);
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:47:39 +02:00
|
|
|
document.querySelectorAll("input[type='text']:focus").forEach(e => {
|
|
|
|
if (data.indexOf(e.value) === -1)
|
2020-05-28 22:25:55 +02:00
|
|
|
e.style.color = "red";
|
|
|
|
else {
|
|
|
|
e.style.color = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-05-30 18:47:39 +02:00
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
socket.on("add user", async () => {
|
|
|
|
await next();
|
2020-05-30 18:47:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("set service", () => {
|
|
|
|
window.close();
|
2020-05-30 21:40:36 +02:00
|
|
|
});
|
2020-05-30 18:47:39 +02:00
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
socket.on("fail add user", async data => {
|
|
|
|
await alert("User creation fail !");
|
2020-05-30 19:01:33 +02:00
|
|
|
if (data && data.username)
|
2020-06-01 01:19:19 +02:00
|
|
|
await addUser(data);
|
2020-05-30 18:47:39 +02:00
|
|
|
});
|
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
socket.on("internal error", async () => {
|
|
|
|
await alert("An error occurred !");
|
2020-05-30 21:40:36 +02:00
|
|
|
});
|
2020-05-30 18:47:39 +02:00
|
|
|
|
|
|
|
document.querySelectorAll("input[type='text']").forEach(e => {
|
|
|
|
e.addEventListener("keyup", ev => {hinter(ev)})
|
|
|
|
});
|
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
document.querySelector("button").addEventListener("click", async () => {
|
2020-05-30 18:47:39 +02:00
|
|
|
document.querySelectorAll("input[type='text']").forEach(e=> {
|
2020-06-01 16:14:27 +02:00
|
|
|
if (e.value && e.style.color)
|
2020-05-30 18:47:39 +02:00
|
|
|
usersAdd.push(e.value);
|
|
|
|
users[e.id] = e.value;
|
|
|
|
});
|
2020-06-01 01:19:19 +02:00
|
|
|
await next();
|
2020-05-30 18:47:39 +02:00
|
|
|
});
|