Archived
1
0
Fork 0

Add remove option on menu, support of remove on commands, remove socket for dish, ingredient, sauce, drink and dessert

This commit is contained in:
Ethanell 2020-05-31 23:19:00 +02:00
parent 79d330ae47
commit 270c671b8e
8 changed files with 243 additions and 47 deletions

View file

@ -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) { function radioCheck (e) {
if (e.checked) { if (e.checked) {
let curr, name; let curr, name;
@ -336,58 +361,73 @@ socket.on("add user", data => {
}); });
socket.on("set dish", data => { socket.on("set dish", data => {
if (data.available && db.dish[data.id] === undefined) { if (data.available && db.dish[data.id] === undefined)
addDish(data); addDish(data);
} else if (!data.available && db.dish[data.id] !== undefined) { else if (!data.available && db.dish[data.id] !== undefined)
dish.querySelector("#dish" + data.id).parentElement.remove(); removeDish(data.id);
delete db.dish[data.id]; else
} else {
db.dish[data.id] = data; db.dish[data.id] = data;
}
}); });
socket.on("set ingredient", data => { socket.on("set ingredient", data => {
if (data.available && db.ingredient[data.id] === undefined) { if (data.available && db.ingredient[data.id] === undefined)
addIngredient(data); addIngredient(data);
} else if (!data.available && db.ingredient[data.id] !== undefined) { else if (!data.available && db.ingredient[data.id] !== undefined)
ingredient.querySelector("#ingredient" + data.id).parentElement.remove(); removeIngredient(data.id);
delete db.ingredient[data.id]; else
} else {
db.ingredient[data.id] = data; db.ingredient[data.id] = data;
}
}); });
socket.on("set sauce", data => { socket.on("set sauce", data => {
if (data.available && db.sauce[data.id] === undefined) { if (data.available && db.sauce[data.id] === undefined)
addSauce(data); addSauce(data);
} else if (!data.available && db.sauce[data.id] !== undefined) { else if (!data.available && db.sauce[data.id] !== undefined)
sauce.querySelector("#sauce" + data.id).parentElement.remove(); removeSauce(data.id);
delete db.sauce[data.id]; else
} else {
db.sauce[data.id] = data; db.sauce[data.id] = data;
}
}); });
socket.on("set drink", data => { socket.on("set drink", data => {
if (data.available && db.drink[data.id] === undefined) { if (data.available && db.drink[data.id] === undefined)
addDrink(data); addDrink(data);
} else if (!data.available && db.drink[data.id] !== undefined) { else if (!data.available && db.drink[data.id] !== undefined)
drink.querySelector("#drink" + data.id).parentElement.remove(); removeDrink(data.id);
delete db.drink[data.id]; else
} else {
db.drink[data.id] = data; db.drink[data.id] = data;
}
}); });
socket.on("set dessert", data => { socket.on("set dessert", data => {
if (data.available && db.dessert[data.id] === undefined) { if (data.available && db.dessert[data.id] === undefined)
addDessert(data); addDessert(data);
} else if (!data.available && db.dessert[data.id] !== undefined) { else if (!data.available && db.dessert[data.id] !== undefined)
dessert.querySelector("#dessert" + data.id).parentElement.remove(); removeDessert(data.id)
delete db.dessert[data.id]; else
} else {
db.dessert[data.id] = data; 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 => { socket.on("price", data => {

View file

@ -72,47 +72,63 @@ function popup(ob, el) {
function addDish(d) { function addDish(d) {
db.dish[d.id] = d; db.dish[d.id] = d;
dish.insertAdjacentHTML("beforeend", `<li><a id="dish${d.id}">${d.name}</a></li>`); dish.insertAdjacentHTML("beforeend", `<li><a id="remove-dish${d.id}">\u274C</a> <a id="dish${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`dish${d.id}`); document.getElementById(`dish${d.id}`).addEventListener("click", ev => {
e.addEventListener("click", () => { popup(db.dish[d.id], ev.target);
popup(db.dish[d.id], e); });
document.getElementById("remove-dish"+d.id).addEventListener("click", () => {
if (confirm("Remove "+d.name+" ?"))
socket.emit("remove dish", d.id);
}); });
} }
function addIngredient(i) { function addIngredient(i) {
db.ingredient[i.id] = i; db.ingredient[i.id] = i;
ingredient.insertAdjacentHTML("beforeend", `<li><a id="ingredient${i.id}">${i.name}</a></li>`); ingredient.insertAdjacentHTML("beforeend", `<li><a id="remove-ingredient${i.id}">\u274C</a> <a id="ingredient${i.id}">${i.name}</a></li>`);
let e = document.getElementById(`ingredient${i.id}`); document.getElementById(`ingredient${i.id}`).addEventListener("click", ev => {
e.addEventListener("click", () => { popup(db.ingredient[i.id], ev.target);
popup(db.ingredient[i.id], e); });
document.getElementById("remove-ingredient"+i.id).addEventListener("click", () => {
if (confirm("Remove "+i.name+" ?"))
socket.emit("remove ingredient", i.id);
}); });
} }
function addSauce(s) { function addSauce(s) {
db.sauce[s.id] = s; db.sauce[s.id] = s;
sauce.insertAdjacentHTML("beforeend", `<li><a id="sauce${s.id}">${s.name}</a></li>`); sauce.insertAdjacentHTML("beforeend", `<li><a id="remove-sauce${s.id}">\u274C</a> <a id="sauce${s.id}">${s.name}</a></li>`);
let e = document.getElementById(`sauce${s.id}`); document.getElementById(`sauce${s.id}`).addEventListener("click", ev => {
e.addEventListener("click", () => { popup(db.sauce[s.id], ev.target);
popup(db.sauce[s.id], e); });
document.getElementById("remove-sauce"+s.id).addEventListener("click", () => {
if (confirm("Remove "+s.name+" ?"))
socket.emit("remove sauce", s.id);
}); });
} }
function addDrink(d) { function addDrink(d) {
db.drink[d.id] = d; db.drink[d.id] = d;
drink.insertAdjacentHTML("beforeend", `<li><a id="drink${d.id}">${d.name}</a></li>`); drink.insertAdjacentHTML("beforeend", `<li><a id="remove-drink${d.id}">\u274C</a> <a id="drink${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`drink${d.id}`); document.getElementById(`drink${d.id}`).addEventListener("click", ev => {
e.addEventListener("click", () => { popup(db.drink[d.id], ev.target);
popup(db.drink[d.id], e); });
document.getElementById("remove-drink"+d.id).addEventListener("click", () => {
if (confirm("Remove "+d.name+" ?"))
socket.emit("remove drink", d.id);
}); });
} }
function addDessert(d) { function addDessert(d) {
db.dessert[d.id] = d; db.dessert[d.id] = d;
dessert.insertAdjacentHTML("beforeend", `<li><a id="dessert${d.id}">${d.name}</a></li>`); dessert.insertAdjacentHTML("beforeend", `<li><a id="remove-dessert${d.id}">\u274C</a> <a id="dessert${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`dessert${d.id}`); let e = document.getElementById(`dessert${d.id}`);
e.addEventListener("click", () => { e.addEventListener("click", () => {
popup(db.dessert[d.id], e); 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", () => { 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", () => { socket.on("internal error", () => {
alert("An error occurred !"); alert("An error occurred !");
}) })

View file

@ -14,6 +14,11 @@ const setSauce = require("./set/setSauce");
const setDrink = require("./set/setDrink"); const setDrink = require("./set/setDrink");
const setDessert = require("./set/setDessert"); const setDessert = require("./set/setDessert");
const setService = require("./set/setService"); 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 giveCommand = require("./giveCommand");
const WIPCommand = require("./WIPCommand"); const WIPCommand = require("./WIPCommand");
const doneCommand = require("./doneCommand"); const doneCommand = require("./doneCommand");
@ -38,6 +43,11 @@ module.exports = socket => {
socket.on("set drink", setDrink(socket)); socket.on("set drink", setDrink(socket));
socket.on("set dessert", setDessert(socket)); socket.on("set dessert", setDessert(socket));
socket.on("set service", setService(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("give command", giveCommand(socket));
socket.on("WIP command", WIPCommand(socket)); socket.on("WIP command", WIPCommand(socket));
socket.on("done command", doneCommand(socket)); socket.on("done command", doneCommand(socket));

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}