2020-06-01 01:19:19 +02:00
|
|
|
import {alert, confirm} from "./popups.js";
|
2020-06-01 14:06:35 +02:00
|
|
|
|
2020-05-31 21:27:04 +02:00
|
|
|
const socket = io();
|
|
|
|
|
2020-05-31 22:26:40 +02:00
|
|
|
let db = {dish: {}, ingredient: {}, sauce: {}, drink: {}, dessert: {}};
|
|
|
|
|
2020-05-31 21:27:04 +02:00
|
|
|
|
2020-06-01 14:06:35 +02:00
|
|
|
function editPopup(ob, el) {
|
2020-06-01 01:19:19 +02:00
|
|
|
document.querySelector("body").insertAdjacentHTML("afterbegin", `<div id="popup-container">
|
|
|
|
<div id="popup">
|
|
|
|
<h1>Edition of ${ob.name}</h1>
|
|
|
|
<div id="edit">
|
|
|
|
<div>
|
|
|
|
<label for="available">Available: </label>
|
|
|
|
<input id="available" type="checkbox">
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="price">Price: </label>
|
|
|
|
<input id="price" type="number" min="0" step="any" value="${ob.price}">
|
2020-05-31 21:27:04 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-06-01 01:19:19 +02:00
|
|
|
<div id="popup-validation">
|
|
|
|
<div class="container-contact2-form-btn">
|
|
|
|
<div class="wrap-contact2-form-btn">
|
|
|
|
<div class="contact2-form-bgbtn"></div>
|
|
|
|
<a href="#" style=""><button id="cancel" class="contact2-form-btn">Cancel</button></a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="container-contact2-form-btn">
|
|
|
|
<div class="wrap-contact2-form-btn">
|
|
|
|
<div class="contact2-form-bgbtn"></div>
|
|
|
|
<a href="#"><button id="apply" class="contact2-form-btn">Apply</button></a>
|
|
|
|
</div>
|
2020-05-31 21:27:04 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`);
|
2020-06-01 01:19:19 +02:00
|
|
|
let e = document.getElementById("popup-container");
|
2020-05-31 21:27:04 +02:00
|
|
|
if (ob.available)
|
|
|
|
e.querySelector("#available").click()
|
|
|
|
if (ob.maxIngredients !== undefined)
|
|
|
|
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", `<div><label for="maxIngredients">Max ingredients: </label><input id="maxIngredients" type="number" min="0" value="${ob.maxIngredients}"></div>`);
|
|
|
|
if (ob.maxSauces !== undefined)
|
|
|
|
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", `<div><label for="maxSauces">Max sauces: </label><input id="maxSauces" type="number" min="0" value="${ob.maxSauces}"></div>`);
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:06:35 +02:00
|
|
|
async function createPopup(type) {
|
|
|
|
document.querySelector("body").insertAdjacentHTML("afterbegin", `<div id="popup-container">
|
|
|
|
<div id="popup">
|
|
|
|
<h1>Create ${type}</h1>
|
|
|
|
<div id="edit">
|
|
|
|
<div>
|
|
|
|
<label for="name">Name: </label>
|
|
|
|
<input class="input2" id="name" type="text">
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="available">Available: </label>
|
|
|
|
<input id="available" type="checkbox" checked>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="price">Price: </label>
|
|
|
|
<input id="price" type="number" min="0" step="any">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="popup-validation">
|
|
|
|
<div class="container-contact2-form-btn">
|
|
|
|
<div class="wrap-contact2-form-btn">
|
|
|
|
<div class="contact2-form-bgbtn"></div>
|
|
|
|
<a href="#" style=""><button id="cancel" class="contact2-form-btn">Cancel</button></a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="container-contact2-form-btn">
|
|
|
|
<div class="wrap-contact2-form-btn">
|
|
|
|
<div class="contact2-form-bgbtn"></div>
|
|
|
|
<a href="#"><button id="apply" class="contact2-form-btn">Apply</button></a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`);
|
|
|
|
let e = document.getElementById("popup-container");
|
|
|
|
if (type === "dish") {
|
|
|
|
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", `<div><label for="maxIngredients">Max ingredients: </label><input id="maxIngredients" type="number" min="0""></div>`);
|
|
|
|
e.querySelector("#edit div:last-of-type").insertAdjacentHTML("afterend", `<div><label for="maxSauces">Max sauces: </label><input id="maxSauces" type="number" min="0"></div>`);
|
|
|
|
}
|
2020-05-31 21:27:04 +02:00
|
|
|
|
2020-06-01 14:06:35 +02:00
|
|
|
e.querySelector("#cancel").addEventListener("click", () => {
|
|
|
|
e.remove();
|
2020-05-31 23:19:00 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
e.querySelector("#apply").addEventListener("click", () => {
|
|
|
|
socket.emit("add " + type, {
|
|
|
|
name: e.querySelector("#name").value,
|
|
|
|
available: e.querySelector("#available").checked,
|
|
|
|
price: parseFloat(e.querySelector("#price").value),
|
|
|
|
maxIngredients: type !== "dish" ? undefined : parseInt(e.querySelector("#maxIngredients").value),
|
|
|
|
maxSauces: type !== "dish" ? undefined : parseInt(e.querySelector("#maxSauces").value),
|
|
|
|
});
|
|
|
|
e.remove();
|
2020-05-31 21:27:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:06:35 +02:00
|
|
|
socket.on("connected", () => {
|
|
|
|
for (let i of ["dish", "ingredient", "sauce", "drink", "dessert"])
|
|
|
|
socket.emit("list " + i);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let i of ["dish", "ingredient", "sauce", "drink", "dessert"]) {
|
|
|
|
let el = document.querySelector(`#${i} ul`);
|
|
|
|
function add(e) {
|
|
|
|
db[i][e.id] = e;
|
|
|
|
el.insertAdjacentHTML("beforeend", `<li><a id="remove-${i}${e.id}">\u274C</a> <a id="${i}${e.id}">${e.name}</a></li>`);
|
|
|
|
document.getElementById(`${i}${e.id}`).addEventListener("click", ev => {
|
|
|
|
editPopup(db[i][e.id], ev.target);
|
|
|
|
});
|
|
|
|
document.getElementById(`remove-${i}${e.id}`).addEventListener("click", async () => {
|
|
|
|
if (await confirm("Remove "+e.name+" ?"))
|
|
|
|
socket.emit("remove " + i, e.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
socket.on("list " + i, data => {
|
|
|
|
let child = el.lastElementChild;
|
|
|
|
while (child) {
|
|
|
|
el.removeChild(child);
|
|
|
|
child = el.lastElementChild;
|
|
|
|
}
|
|
|
|
for (let d of data)
|
|
|
|
add(d);
|
2020-05-31 23:19:00 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
socket.on("add " + i, data => {
|
|
|
|
if (data.available)
|
|
|
|
add(data);
|
2020-05-31 21:27:04 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
document.getElementById("add-" + i).addEventListener("click", async () => {
|
|
|
|
await createPopup(i);
|
2020-05-31 23:19:00 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
socket.on("fail add "+i, async data => {
|
|
|
|
await alert("Fail to add " + i + " " + data.name)
|
2020-05-31 21:27:04 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
socket.on("set " + i, data => {
|
|
|
|
if (data.available && db[i][data.id] === undefined) {
|
|
|
|
add(data);
|
|
|
|
} else {
|
|
|
|
db[i][data.id] = data;
|
|
|
|
}
|
2020-05-31 21:27:04 +02:00
|
|
|
});
|
2020-06-01 14:06:35 +02:00
|
|
|
socket.on("remove " + i, data => {
|
|
|
|
if (db[i][data]) {
|
|
|
|
el.querySelector("#" + i + data).parentElement.remove();
|
|
|
|
delete db[i][data]
|
|
|
|
}
|
2020-05-31 23:19:00 +02:00
|
|
|
});
|
2020-05-31 21:27:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 01:19:19 +02:00
|
|
|
socket.on("internal error", async () => {
|
|
|
|
await alert("An error occurred !");
|
|
|
|
});
|