const socket = io();
const dish = document.querySelector("#dish ul");
const ingredient = document.querySelector("#ingredient ul");
const sauce = document.querySelector("#sauce ul");
const drink = document.querySelector("#drink ul");
const dessert = document.querySelector("#dessert ul");
let db = {dish: {}, ingredient: {}, sauce: {}, drink: {}, dessert: {}};
if (window.location.href.endsWith("#popup"))
window.location.href = window.location.href.replace("#popup", "#");
function popup(ob, el) {
document.querySelector("body").insertAdjacentHTML("afterbegin", `
`);
let e = document.getElementById("popup");
if (ob.available)
e.querySelector("#available").click()
if (ob.maxIngredients !== undefined)
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", ``);
if (ob.maxSauces !== undefined)
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", ``);
e.querySelector("#cancel").addEventListener("click", () => {
e.remove();
});
e.querySelector("#apply").addEventListener("click", () => {
socket.emit("set " + el.id.replace(ob.id, ""), {
id: ob.id,
available: e.querySelector("#available").checked,
price: parseFloat(e.querySelector("#price").value),
maxIngredients: ob.maxIngredients === undefined ? undefined : parseInt(e.querySelector("#maxIngredients").value),
maxSauces: ob.maxSauces === undefined ? undefined : parseInt(e.querySelector("#maxSauces").value),
});
e.remove();
});
if (window.location.href.endsWith("#"))
window.location.href = window.location.href + "popup";
else if (!window.location.href.endsWith("#popup"))
window.location.href = window.location.href + "#popup";
else
window.location.href = window.location.href.replace("#popup", "#");
}
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);
});
}
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);
});
}
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);
});
}
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);
});
}
function addDessert(d) {
db.dessert[d.id] = d;
dessert.insertAdjacentHTML("beforeend", `${d.name}`);
let e = document.getElementById(`dessert${d.id}`);
e.addEventListener("click", () => {
popup(db.dessert[d.id], e);
});
}
socket.on("connected", () => {
socket.emit("list dish");
socket.emit("list ingredient");
socket.emit("list sauce");
socket.emit("list drink");
socket.emit("list dessert");
});
socket.on("list dish", data => {
let child = dish.lastElementChild;
while (child) {
dish.removeChild(child);
child = dish.lastElementChild;
}
for (let d of data)
addDish(d);
});
socket.on("list ingredient", data => {
let child = ingredient.lastElementChild;
while (child) {
ingredient.removeChild(child);
child = ingredient.lastElementChild;
}
for (let i of data)
addIngredient(i);
});
socket.on("list sauce", data => {
let child = sauce.lastElementChild;
while (child) {
sauce.removeChild(child);
child = sauce.lastElementChild;
}
for (let s of data)
addSauce(s);
});
socket.on("list drink", data => {
let child = drink.lastElementChild;
while (child) {
drink.removeChild(child);
child = drink.lastElementChild;
}
for (let d of data)
addDrink(d);
});
socket.on("list dessert", data => {
let child = dessert.lastElementChild;
while (child) {
dessert.removeChild(child);
child = dessert.lastElementChild;
}
for (let d of data)
addDessert(d);
});
socket.on("set dish", data => {
if (data.available && db.dish[data.id] === undefined) {
addDish(data);
} else {
db.dish[data.id] = data;
}
});
socket.on("set ingredient", data => {
if (data.available && db.ingredient[data.id] === undefined) {
addIngredient(data);
} else {
db.ingredient[data.id] = data;
}
});
socket.on("set sauce", data => {
if (data.available && db.sauce[data.id] === undefined) {
addSauce(data);
} else {
db.sauce[data.id] = data;
}
});
socket.on("set drink", data => {
if (data.available && db.drink[data.id] === undefined) {
addDrink(data);
} else {
db.drink[data.id] = data;
}
});
socket.on("set dessert", data => {
if (data.available && db.dessert[data.id] === undefined) {
addDessert(data);
} else {
db.dessert[data.id] = data;
}
});
socket.on("internal error", () => {
alert("An error occurred !");
})