From df5cba87ad5000eeddff06294c7c27c8b333a08e Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 30 May 2020 16:11:58 +0200 Subject: [PATCH] Setup dynamic max for ingredients and sauces, replace avoid if 0 --- models/dish.js | 10 ++++------ public/javascripts/commands.js | 11 +++++------ sockets/listDish.js | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/models/dish.js b/models/dish.js index 0270fbf..96fc15f 100644 --- a/models/dish.js +++ b/models/dish.js @@ -11,14 +11,12 @@ module.exports = (sequelize, DataTypes) => { defaultValue: 0, allowNull: false }, - avoidIngredients: { - type: DataTypes.BOOLEAN, - defaultValue: false, + maxIngredients: { + type: DataTypes.INTEGER, allowNull: false }, - avoidSauces: { - type: DataTypes.BOOLEAN, - defaultValue: false, + maxSauces: { + type: DataTypes.INTEGER, allowNull: false } }, { diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index b1e75d1..37b7422 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -65,7 +65,7 @@ function addDish(d) { el.click(); }); document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( el => { - el.disabled = !(e.checked && !db.dish[e.id.replace(e.name, "")]["avoid" + el.name.charAt(0).toUpperCase() + el.name.slice(1)]); + el.disabled = !(e.checked && !(db.dish[e.id.replace(e.name, "")]["max" + el.name.charAt(0).toUpperCase() + el.name.slice(1)+"s"] === 0)); }); }) } @@ -74,7 +74,7 @@ function addIngredient(i) { ingredient.insertAdjacentHTML("beforeend", `
  • `); let e = document.querySelector(`input[id=ingredient${i.id}]`); e.addEventListener("click", () => { - checkCheck(e, 3) + checkCheck(e) }) } @@ -82,7 +82,7 @@ function addSauce(s) { sauce.insertAdjacentHTML("beforeend", `
  • `); let e = document.querySelector(`input[id=sauce${s.id}]`); e.addEventListener("click", () => { - checkCheck(e,2) + checkCheck(e) }) } @@ -125,15 +125,14 @@ function radioCheck (e) { } } -function checkCheck(e, l) { - //ToDo setup dynamic maxSauces and maxIngredients from db ! +function checkCheck(e) { if (e.checked) current[e.name].push(e.id.replace(e.name, "")); else current[e.name].splice(current[e.name].indexOf(e.id.replace(e.name, "")), 1); document.querySelectorAll(`input[name=${e.name}]`).forEach( e => { if (!e.checked) - e.disabled = current[e.name].length === l + e.disabled = current[e.name].length === db.dish[current.dish]["max"+e.name.charAt(0).toUpperCase() + e.name.slice(1)+"s"]; }); current.price[e.name] = 0; for (let i of current[e.name]) { diff --git a/sockets/listDish.js b/sockets/listDish.js index dba7345..690d029 100644 --- a/sockets/listDish.js +++ b/sockets/listDish.js @@ -8,8 +8,8 @@ module.exports = socket => { id: d.id, name: d.name, price: d.price, - avoidIngredients: d.avoidIngredients, - avoidSauces: d.avoidSauces + maxIngredients: d.maxIngredients, + maxSauces: d.maxSauces }); } socket.emit("list dish", dishes);