From 270c671b8e79547e1bee76df7a2cfad3e4a1254c Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 31 May 2020 23:19:00 +0200 Subject: [PATCH] Add remove option on menu, support of remove on commands, remove socket for dish, ingredient, sauce, drink and dessert --- public/javascripts/commands.js | 100 ++++++++++++++++++++--------- public/javascripts/menu.js | 85 +++++++++++++++++++----- sockets/index.js | 10 +++ sockets/remove/removeDessert.js | 19 ++++++ sockets/remove/removeDish.js | 19 ++++++ sockets/remove/removeDrink.js | 19 ++++++ sockets/remove/removeIngredient.js | 19 ++++++ sockets/remove/removeSauce.js | 19 ++++++ 8 files changed, 243 insertions(+), 47 deletions(-) create mode 100644 sockets/remove/removeDessert.js create mode 100644 sockets/remove/removeDish.js create mode 100644 sockets/remove/removeDrink.js create mode 100644 sockets/remove/removeIngredient.js create mode 100644 sockets/remove/removeSauce.js diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index cd44de4..f0e4d99 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -107,6 +107,31 @@ function addDessert(d) { }) } +function removeDish(d) { + dish.querySelector("#dish"+d).parentElement.remove(); + delete db.dish[d]; +} + +function removeIngredient(d) { + ingredient.querySelector("#ingredient"+d).parentElement.remove(); + delete db.ingredient[d]; +} + +function removeSauce(d) { + sauce.querySelector("#sauce"+d).parentElement.remove(); + delete db.sauce[d]; +} + +function removeDrink(d) { + drink.querySelector("#drink"+d).parentElement.remove(); + delete db.drink[d]; +} + +function removeDessert(d) { + dessert.querySelector("#dessert"+d).parentElement.remove(); + delete db.dessert[d]; +} + function radioCheck (e) { if (e.checked) { let curr, name; @@ -336,58 +361,73 @@ socket.on("add user", data => { }); socket.on("set dish", data => { - if (data.available && db.dish[data.id] === undefined) { + if (data.available && db.dish[data.id] === undefined) addDish(data); - } else if (!data.available && db.dish[data.id] !== undefined) { - dish.querySelector("#dish" + data.id).parentElement.remove(); - delete db.dish[data.id]; - } else { + else if (!data.available && db.dish[data.id] !== undefined) + removeDish(data.id); + else db.dish[data.id] = data; - } }); socket.on("set ingredient", data => { - if (data.available && db.ingredient[data.id] === undefined) { + if (data.available && db.ingredient[data.id] === undefined) addIngredient(data); - } else if (!data.available && db.ingredient[data.id] !== undefined) { - ingredient.querySelector("#ingredient" + data.id).parentElement.remove(); - delete db.ingredient[data.id]; - } else { + else if (!data.available && db.ingredient[data.id] !== undefined) + removeIngredient(data.id); + else db.ingredient[data.id] = data; - } }); socket.on("set sauce", data => { - if (data.available && db.sauce[data.id] === undefined) { + if (data.available && db.sauce[data.id] === undefined) addSauce(data); - } else if (!data.available && db.sauce[data.id] !== undefined) { - sauce.querySelector("#sauce" + data.id).parentElement.remove(); - delete db.sauce[data.id]; - } else { + else if (!data.available && db.sauce[data.id] !== undefined) + removeSauce(data.id); + else db.sauce[data.id] = data; - } }); socket.on("set drink", data => { - if (data.available && db.drink[data.id] === undefined) { + if (data.available && db.drink[data.id] === undefined) addDrink(data); - } else if (!data.available && db.drink[data.id] !== undefined) { - drink.querySelector("#drink" + data.id).parentElement.remove(); - delete db.drink[data.id]; - } else { + else if (!data.available && db.drink[data.id] !== undefined) + removeDrink(data.id); + else db.drink[data.id] = data; - } }); socket.on("set dessert", data => { - if (data.available && db.dessert[data.id] === undefined) { + if (data.available && db.dessert[data.id] === undefined) addDessert(data); - } else if (!data.available && db.dessert[data.id] !== undefined) { - dessert.querySelector("#dessert" + data.id).parentElement.remove(); - delete db.dessert[data.id]; - } else { + else if (!data.available && db.dessert[data.id] !== undefined) + removeDessert(data.id) + else db.dessert[data.id] = data; - } +}); + +socket.on("remove dish", data => { + if (db.dish[data]) + removeDish(data); +}); + +socket.on("remove ingredient", data => { + if (db.ingredient[data]) + removeIngredient(data); +}); + +socket.on("remove sauce", data => { + if (db.sauce[data]) + removeSauce(data); +}); + +socket.on("remove drink", data => { + if (db.drink[data]) + removeDrink(data); +}); + +socket.on("remove dessert", data => { + if (db.dessert[data]) + removeDessert(data); }); socket.on("price", data => { diff --git a/public/javascripts/menu.js b/public/javascripts/menu.js index 202fa25..f8eb26f 100644 --- a/public/javascripts/menu.js +++ b/public/javascripts/menu.js @@ -72,47 +72,63 @@ function popup(ob, el) { function addDish(d) { db.dish[d.id] = d; - dish.insertAdjacentHTML("beforeend", `
  • ${d.name}
  • `); - let e = document.getElementById(`dish${d.id}`); - e.addEventListener("click", () => { - popup(db.dish[d.id], e); + dish.insertAdjacentHTML("beforeend", `
  • \u274C ${d.name}
  • `); + document.getElementById(`dish${d.id}`).addEventListener("click", ev => { + popup(db.dish[d.id], ev.target); + }); + document.getElementById("remove-dish"+d.id).addEventListener("click", () => { + if (confirm("Remove "+d.name+" ?")) + socket.emit("remove dish", d.id); }); } function addIngredient(i) { db.ingredient[i.id] = i; - ingredient.insertAdjacentHTML("beforeend", `
  • ${i.name}
  • `); - let e = document.getElementById(`ingredient${i.id}`); - e.addEventListener("click", () => { - popup(db.ingredient[i.id], e); + ingredient.insertAdjacentHTML("beforeend", `
  • \u274C ${i.name}
  • `); + document.getElementById(`ingredient${i.id}`).addEventListener("click", ev => { + popup(db.ingredient[i.id], ev.target); + }); + document.getElementById("remove-ingredient"+i.id).addEventListener("click", () => { + if (confirm("Remove "+i.name+" ?")) + socket.emit("remove ingredient", i.id); }); } function addSauce(s) { db.sauce[s.id] = s; - sauce.insertAdjacentHTML("beforeend", `
  • ${s.name}
  • `); - let e = document.getElementById(`sauce${s.id}`); - e.addEventListener("click", () => { - popup(db.sauce[s.id], e); + sauce.insertAdjacentHTML("beforeend", `
  • \u274C ${s.name}
  • `); + document.getElementById(`sauce${s.id}`).addEventListener("click", ev => { + popup(db.sauce[s.id], ev.target); + }); + document.getElementById("remove-sauce"+s.id).addEventListener("click", () => { + if (confirm("Remove "+s.name+" ?")) + socket.emit("remove sauce", s.id); }); } function addDrink(d) { db.drink[d.id] = d; - drink.insertAdjacentHTML("beforeend", `
  • ${d.name}
  • `); - let e = document.getElementById(`drink${d.id}`); - e.addEventListener("click", () => { - popup(db.drink[d.id], e); + drink.insertAdjacentHTML("beforeend", `
  • \u274C ${d.name}
  • `); + document.getElementById(`drink${d.id}`).addEventListener("click", ev => { + popup(db.drink[d.id], ev.target); + }); + document.getElementById("remove-drink"+d.id).addEventListener("click", () => { + if (confirm("Remove "+d.name+" ?")) + socket.emit("remove drink", d.id); }); } function addDessert(d) { db.dessert[d.id] = d; - dessert.insertAdjacentHTML("beforeend", `
  • ${d.name}
  • `); + dessert.insertAdjacentHTML("beforeend", `
  • \u274C ${d.name}
  • `); let e = document.getElementById(`dessert${d.id}`); e.addEventListener("click", () => { popup(db.dessert[d.id], e); }); + document.getElementById("remove-dessert"+d.id).addEventListener("click", () => { + if (confirm("Remove "+d.name+" ?")) + socket.emit("remove dessert", d.id); + }); } socket.on("connected", () => { @@ -213,6 +229,41 @@ socket.on("set dessert", data => { } }); +socket.on("remove dish", data => { + if (db.dish[data]) { + dish.querySelector("#dish" + data).parentElement.remove(); + delete db.dish[data] + } +}); + +socket.on("remove ingredient", data => { + if (db.ingredient[data]) { + ingredient.querySelector("#ingredient" + data).parentElement.remove(); + delete db.ingredient[data] + } +}); + +socket.on("remove sauce", data => { + if (db.sauce[data]) { + sauce.querySelector("#sauce" + data).parentElement.remove(); + delete db.sauce[data] + } +}); + +socket.on("remove drink", data => { + if (db.drink[data]) { + drink.querySelector("#drink" + data).parentElement.remove(); + delete db.drink[data] + } +}); + +socket.on("remove dessert", data => { + if (db.dessert[data]) { + dessert.querySelector("#dessert" + data).parentElement.remove(); + delete db.dessert[data] + } +}); + socket.on("internal error", () => { alert("An error occurred !"); }) diff --git a/sockets/index.js b/sockets/index.js index 5bfb658..d7c546d 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -14,6 +14,11 @@ const setSauce = require("./set/setSauce"); const setDrink = require("./set/setDrink"); const setDessert = require("./set/setDessert"); const setService = require("./set/setService"); +const removeDish = require("./remove/removeDish"); +const removeIngredient = require("./remove/removeIngredient"); +const removeSauce = require("./remove/removeSauce"); +const removeDrink = require("./remove/removeDrink"); +const removeDessert = require("./remove/removeDessert"); const giveCommand = require("./giveCommand"); const WIPCommand = require("./WIPCommand"); const doneCommand = require("./doneCommand"); @@ -38,6 +43,11 @@ module.exports = socket => { socket.on("set drink", setDrink(socket)); socket.on("set dessert", setDessert(socket)); socket.on("set service", setService(socket)); + socket.on("remove dish", removeDish(socket)); + socket.on("remove ingredient", removeIngredient(socket)); + socket.on("remove sauce", removeSauce(socket)); + socket.on("remove drink", removeDrink(socket)); + socket.on("remove dessert", removeDessert(socket)); socket.on("give command", giveCommand(socket)); socket.on("WIP command", WIPCommand(socket)); socket.on("done command", doneCommand(socket)); diff --git a/sockets/remove/removeDessert.js b/sockets/remove/removeDessert.js new file mode 100644 index 0000000..acbf1d0 --- /dev/null +++ b/sockets/remove/removeDessert.js @@ -0,0 +1,19 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + try { + let d = await models.Dessert.findByPk(data); + if (!d) + throw new Error("Dessert not found !"); + + await d.destroy(); + + socket.emit("remove dessert", d.id); + socket.broadcast.emit("remove dessert", d.id); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/remove/removeDish.js b/sockets/remove/removeDish.js new file mode 100644 index 0000000..3c82bbb --- /dev/null +++ b/sockets/remove/removeDish.js @@ -0,0 +1,19 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + try { + let d = await models.Dish.findByPk(data); + if (!d) + throw new Error("Dish not found !"); + + await d.destroy(); + + socket.emit("remove dish", d.id); + socket.broadcast.emit("remove dish", d.id); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/remove/removeDrink.js b/sockets/remove/removeDrink.js new file mode 100644 index 0000000..16ae260 --- /dev/null +++ b/sockets/remove/removeDrink.js @@ -0,0 +1,19 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + try { + let d = await models.Drink.findByPk(data); + if (!d) + throw new Error("Drink not found !"); + + await d.destroy(); + + socket.emit("remove drink", d.id); + socket.broadcast.emit("remove drink", d.id); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/remove/removeIngredient.js b/sockets/remove/removeIngredient.js new file mode 100644 index 0000000..9160df4 --- /dev/null +++ b/sockets/remove/removeIngredient.js @@ -0,0 +1,19 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + try { + let i = await models.Ingredient.findByPk(data); + if (!i) + throw new Error("Ingredient not found !"); + + await i.destroy(); + + socket.emit("remove ingredient", i.id); + socket.broadcast.emit("remove ingredient", i.id); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/remove/removeSauce.js b/sockets/remove/removeSauce.js new file mode 100644 index 0000000..12199ac --- /dev/null +++ b/sockets/remove/removeSauce.js @@ -0,0 +1,19 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + try { + let s = await models.Sauce.findByPk(data); + if (!s) + throw new Error("Sauce not found !"); + + await s.destroy(); + + socket.emit("remove sauce", s.id); + socket.broadcast.emit("remove sauce", s.id); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +}