Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
kfet/public/javascripts/service.js

95 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-06-01 01:19:19 +02:00
import {alert, prompt} from "./popups.js";
let socket = io();
let users = {};
let usersAdd = [];
2020-06-01 01:19:19 +02:00
async function addUser(username) {
let firstName, lastName;
do {
2020-06-01 01:19:19 +02:00
firstName = await prompt("First name for " + username);
} while (firstName === "");
if (firstName) {
do {
2020-06-01 01:19:19 +02:00
lastName = await prompt("Last name for " + username);
} while (lastName === "");
if (lastName)
socket.emit("add user", {username: username, firstName: firstName, lastName: lastName});
}
if (!firstName|| !lastName)
2020-06-01 01:19:19 +02:00
await alert("User creation aborted for " + username);
}
2020-06-01 01:19:19 +02:00
async function next() {
if (usersAdd.length)
2020-06-01 01:19:19 +02:00
await addUser(usersAdd.pop());
else
socket.emit("set service", users);
}
function hinter(ev) {
let input = ev.target;
let min_characters = 0;
if (input.value.length < min_characters)
return;
socket.emit("list user", input.value);
}
socket.on("connected", () => {
socket.emit("list service");
});
socket.on("list service", data => {
for (let s in data) {
document.getElementById(s).value = data[s]
}
});
socket.on("list user", data => {
let user_list = document.getElementById("user_list");
user_list.innerHTML = "";
for (let u of data) {
user_list.insertAdjacentHTML("beforeend", `<option value="${u}">`);
}
document.querySelectorAll("input[type='text']:focus").forEach(e => {
if (data.indexOf(e.value) === -1)
e.style.color = "red";
else {
e.style.color = "";
}
});
});
2020-06-01 01:19:19 +02:00
socket.on("add user", async () => {
await next();
});
socket.on("set service", () => {
console.log("close !")
window.close();
});
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-06-01 01:19:19 +02:00
socket.on("internal error", async () => {
await alert("An error occurred !");
});
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 () => {
document.querySelectorAll("input[type='text']").forEach(e=> {
if (e.style.color)
usersAdd.push(e.value);
users[e.id] = e.value;
});
2020-06-01 01:19:19 +02:00
await next();
});