Archived
1
0
Fork 0

Setup user creation and user suggestion on commands

This commit is contained in:
Ethanell 2020-05-30 15:42:04 +02:00
parent 19f79a5fdc
commit 8a7a137def
5 changed files with 136 additions and 60 deletions

View file

@ -3,8 +3,7 @@ module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', { const User = sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, primaryKey: true
unique: true
}, },
passwordHash: { passwordHash: {
type: DataTypes.STRING type: DataTypes.STRING

View file

@ -126,6 +126,7 @@ function radioCheck (e) {
} }
function checkCheck(e, l) { function checkCheck(e, l) {
//ToDo setup dynamic maxSauces and maxIngredients from db !
if (e.checked) if (e.checked)
current[e.name].push(e.id.replace(e.name, "")); current[e.name].push(e.id.replace(e.name, ""));
else else
@ -186,6 +187,49 @@ function price() {
return p; return p;
} }
function addUser() {
let firstName, lastName;
do {
firstName = prompt("First name");
} while (firstName === "");
if (firstName) {
do {
lastName = prompt("Last name");
} while (lastName === "");
if (lastName)
socket.emit("add user", {username: current.client, firstName: firstName, lastName: lastName});
}
if (!firstName|| !lastName) {
alert("User creation aborted");
}
}
function addCommand() {
current.pc = null;
current.price = price();
socket.emit("add command", current);
clearSelections();
}
function clearSelections() {
current = {dish: null, ingredient: [], sauce: [], drink: null, dessert: null, price: {}};
document.querySelectorAll("input[name=dish],input[name=drink],input[name=dessert]").forEach( e => {
e.checked = false;
});
document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( e => {
e.checked = false;
e.disabled = true;
});
document.querySelectorAll("#resume p").forEach( e => {
e.innerHTML = ""
});
let user = document.getElementById("user");
user.value = "";
user.style.color = "";
document.getElementById("user_list").innerHTML = "";
document.querySelector("#resume h2").innerHTML = "0€";
}
socket.on("connected", () => { socket.on("connected", () => {
socket.emit("list dish"); socket.emit("list dish");
socket.emit("list ingredient"); socket.emit("list ingredient");
@ -266,6 +310,19 @@ socket.on("list dessert", data => {
} }
}); });
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}">`);
let user = document.getElementById("user");
if (data.indexOf(user.value) === -1)
user.style.color = "red";
else
user.style.color = "";
});
socket.on("new command", data => { socket.on("new command", data => {
addCmd(data); addCmd(data);
}); });
@ -290,63 +347,35 @@ socket.on("error command", data => {
error(document.querySelector(`.list #cmd${data}`)) error(document.querySelector(`.list #cmd${data}`))
}); });
socket.on("error", () => { socket.on("add user", data => {
alert("An error occurred :") if (data === current.client)
addCommand();
});
socket.on("fail add user", () => {
alert("User creation fail !");
addUser();
});
socket.on("internal error", () => {
alert("An error occurred !");
}) })
document.querySelector(".validation").addEventListener("click", ev => { document.querySelector(".validation").addEventListener("click", ev => {
ev.stopPropagation(); ev.stopPropagation();
let user = document.getElementById("user"); if (!current.dish && !current.ingredient.length && !current.sauce.length && !current.drink && !current.dessert)
if (!current.dish && !current.ingredient.length && !current.sauce.length && !current.drink && !current.dessert) {
alert("Empty command !"); alert("Empty command !");
return; else if (user.style.color === "red")
} else if (user.style.color === "red") { addUser();
current.firstname = prompt("Prénom"); else
current.lastname = prompt("Nom"); addCommand();
}
current.client = user.value;
current.pc = 0;
current.price = price();
socket.emit("add command", current);
current = {dish: null, ingredient: [], sauce: [], drink: null, dessert: null, price: {}};
document.querySelectorAll("input[name=dish],input[name=drink],input[name=dessert]").forEach( e => {
e.checked = false;
});
document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( e => {
e.checked = false;
e.disabled = true;
});
document.querySelectorAll("#resume p").forEach( e => {
e.innerHTML = ""
});
user.value = "";
user.style.color = "";
document.getElementById("user_list").innerHTML = "";
document.querySelector("#resume h2").innerHTML = "0€";
}); });
document.getElementById("user").addEventListener("keyup", ev => {hinter(ev)}); document.getElementById("user").addEventListener("keyup", ev => {
function hinter(ev) {
let input = ev.target; let input = ev.target;
let min_characters = 0; let min_characters = 0;
if (input.value.length < min_characters) if (input.value.length < min_characters)
return; return;
socket.emit("list users", {"user": input.value}); socket.emit("list user", input.value);
} current.client = input.value;
socket.on("list users", data => {
let user_list = document.getElementById("user_list");
user_list.innerHTML = "";
for (let u of data) {
user_list.insertAdjacentHTML("beforeend", `<option value="${u}">`);
}
let user = document.getElementById("user");
if (data.indexOf(user.value) === -1)
user.style.color = "red";
else {
user.style.color = "";
}
}); });

27
sockets/addUser.js Normal file
View file

@ -0,0 +1,27 @@
const models = require("../models");
module.exports = socket => {
return async (data) => {
try {
let u;
try {
u = await models.User.create({
username: data.username,
firstName: data.firstName,
lastName: data.lastName
})
} catch (e) {
if (e instanceof models.Sequelize.ValidationError)
socket.emit("fail add user")
else
throw e;
}
socket.emit("add user", u.username);
socket.broadcast.emit("add user", u.username);
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

View file

@ -1,13 +1,15 @@
const listCommand = require("./listCommand") const listCommand = require("./listCommand");
const listDish = require("./listDish") const listDish = require("./listDish");
const listIngredient = require("./listIngredient") const listIngredient = require("./listIngredient");
const listSauce = require("./listSauce") const listSauce = require("./listSauce");
const listDrink = require("./listDrink") const listDrink = require("./listDrink");
const listDessert = require("./listDessert") const listDessert = require("./listDessert");
const addCommand = require("./addCommand") const listUser = require("./listUser");
const giveCommand = require("./giveCommand") const addCommand = require("./addCommand");
const errorCommand = require("./errorCommand") const addUSer = require("./addUser")
const clearCommand = require("./clearCommand") const giveCommand = require("./giveCommand");
const errorCommand = require("./errorCommand");
const clearCommand = require("./clearCommand");
module.exports = socket => { module.exports = socket => {
socket.on("list command", listCommand(socket)); socket.on("list command", listCommand(socket));
@ -17,7 +19,9 @@ module.exports = socket => {
socket.on("list drink", listDrink(socket)); socket.on("list drink", listDrink(socket));
socket.on("list dessert", listDessert(socket)); socket.on("list dessert", listDessert(socket));
socket.on("list dessert", listDessert(socket)); socket.on("list dessert", listDessert(socket));
socket.on("list user", listUser(socket));
socket.on("add command", addCommand(socket)); socket.on("add command", addCommand(socket));
socket.on("add user", addUSer(socket));
socket.on("give command", giveCommand(socket)); socket.on("give command", giveCommand(socket));
socket.on("error command", errorCommand(socket)); socket.on("error command", errorCommand(socket));
socket.on("clear command", clearCommand(socket)); socket.on("clear command", clearCommand(socket));

17
sockets/listUser.js Normal file
View file

@ -0,0 +1,17 @@
const models = require("../models");
module.exports = socket => {
return async data => {
let users = []
for (let u of await models.User.findAll({
where: {
username: {
[models.Sequelize.Op.like]: data + "%"
}
}
})) {
users.push(u.username);
}
socket.emit("list user", users);
}
}