From 8a7a137def6d9bcaea0cce33c74d52c820d26377 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 30 May 2020 15:42:04 +0200 Subject: [PATCH] Setup user creation and user suggestion on commands --- models/user.js | 3 +- public/javascripts/commands.js | 125 ++++++++++++++++++++------------- sockets/addUser.js | 27 +++++++ sockets/index.js | 24 ++++--- sockets/listUser.js | 17 +++++ 5 files changed, 136 insertions(+), 60 deletions(-) create mode 100644 sockets/addUser.js create mode 100644 sockets/listUser.js diff --git a/models/user.js b/models/user.js index de2f0ca..39fdcd1 100644 --- a/models/user.js +++ b/models/user.js @@ -3,8 +3,7 @@ module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { username: { type: DataTypes.STRING, - allowNull: false, - unique: true + primaryKey: true }, passwordHash: { type: DataTypes.STRING diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index 4f115f1..b1e75d1 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -126,6 +126,7 @@ function radioCheck (e) { } function checkCheck(e, l) { + //ToDo setup dynamic maxSauces and maxIngredients from db ! if (e.checked) current[e.name].push(e.id.replace(e.name, "")); else @@ -186,6 +187,49 @@ function price() { 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.emit("list dish"); 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", `