Archived
1
0
Fork 0

Add menu page, route, view, add socket set for dish, ingredient, sauce, drink and dessert

This commit is contained in:
Ethanell 2020-05-31 21:27:04 +02:00
parent 28fd51555e
commit 2cf0dd800b
22 changed files with 452 additions and 38 deletions

14
app.js
View file

@ -7,12 +7,13 @@ const logger = require("morgan");
const config = require("./config/config.json");
const indexRouter = require("./routes/index");
const loginRouter = require("./routes/login")
const logoutRouter = require("./routes/logout")
const commandsRouter = require("./routes/commands")
const kitchenRouter = require("./routes/kitchen")
const serviceRouter = require("./routes/service")
const stocksRouter = require("./routes/stocks")
const loginRouter = require("./routes/login");
const logoutRouter = require("./routes/logout");
const commandsRouter = require("./routes/commands");
const kitchenRouter = require("./routes/kitchen");
const menuRouter = require("./routes/menu");
const serviceRouter = require("./routes/service");
const stocksRouter = require("./routes/stocks");
const app = express();
const sess = {
@ -49,6 +50,7 @@ app.use("/login", loginRouter);
app.use("/logout", logoutRouter);
app.use("/commands", commandsRouter);
app.use("/kitchen", kitchenRouter);
app.use("/menu", menuRouter);
app.use("/stocks", stocksRouter);
app.use("/service", serviceRouter);

View file

@ -10,6 +10,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.FLOAT,
defaultValue: 0,
allowNull: false
},
available: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: "Desserts"

View file

@ -11,6 +11,11 @@ module.exports = (sequelize, DataTypes) => {
defaultValue: 0,
allowNull: false
},
available: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
},
maxIngredients: {
type: DataTypes.INTEGER,
allowNull: false

View file

@ -10,6 +10,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.FLOAT,
defaultValue: 0,
allowNull: false
},
available: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: "Drinks"

View file

@ -10,6 +10,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.FLOAT,
defaultValue: 0,
allowNull: false
},
available: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: "Ingredients"

View file

@ -10,6 +10,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.FLOAT,
defaultValue: 0,
allowNull: false
},
available: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
}
}, {
tableName: "Sauces"

171
public/javascripts/menu.js Normal file
View file

@ -0,0 +1,171 @@
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");
if (window.location.href.endsWith("#popup"))
window.location.href = window.location.href.replace("#popup", "#");
function popup(ob, el) {
document.querySelector("body").insertAdjacentHTML("afterbegin", `<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}">
</div>
</div>
<div id="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>`);
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", `<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();
});
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) {
dish.insertAdjacentHTML("beforeend", `<li><a id="dish${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`dish${d.id}`);
e.addEventListener("click", () => {
popup(d, e);
});
}
function addIngredient(i) {
ingredient.insertAdjacentHTML("beforeend", `<li><a id="ingredient${i.id}">${i.name}</a></li>`);
let e = document.getElementById(`ingredient${i.id}`);
e.addEventListener("click", () => {
popup(i, e);
});
}
function addSauce(s) {
sauce.insertAdjacentHTML("beforeend", `<li><a id="sauce${s.id}">${s.name}</a></li>`);
let e = document.getElementById(`sauce${s.id}`);
e.addEventListener("click", () => {
popup(s, e);
});
}
function addDrink(d) {
drink.insertAdjacentHTML("beforeend", `<li><a id="drink${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`drink${d.id}`);
e.addEventListener("click", () => {
popup(d, e);
});
}
function addDessert(d) {
dessert.insertAdjacentHTML("beforeend", `<li><a id="dessert${d.id}">${d.name}</a></li>`);
let e = document.getElementById(`dessert${d.id}`);
e.addEventListener("click", () => {
popup(d, 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("internal error", () => {
alert("An error occurred !");
})

View file

@ -661,6 +661,10 @@ audio{
opacity: 0.2;
}
#popup h1 {
text-align: center;
}
#WIP{
display: flex;
flex-wrap : wrap;
@ -830,4 +834,21 @@ div#teams{
#okteam button{
font-size: 30px;
}
}
#edit {
display: flex;
flex-direction: column;
align-content: space-between;
justify-content: space-between;
}
#edit div {
margin-bottom: 0.5em;
text-align: center;
}
#validation {
display: flex;
flex-direction: row;
}

10
routes/menu.js Normal file
View file

@ -0,0 +1,10 @@
const express = require("express");
const router = express.Router();
const middleware = require("./middleware");
/* GET home page. */
router.get("/", middleware.sessionCheck, (req, res) => {
res.render("menu", { title: "Kfet - Menu" });
});
module.exports = router;

View file

@ -8,6 +8,11 @@ const listUser = require("./listUser");
const listService = require("./listService");
const addCommand = require("./addCommand");
const addUSer = require("./addUser");
const setDish = require("./setDish");
const setIngredient = require("./setIngredient");
const setSauce = require("./setSauce");
const setDrink = require("./setDrink");
const setDessert = require("./setDessert");
const setService = require("./setService");
const giveCommand = require("./giveCommand");
const WIPCommand = require("./WIPCommand");
@ -23,11 +28,15 @@ module.exports = socket => {
socket.on("list sauce", listSauce(socket));
socket.on("list drink", listDrink(socket));
socket.on("list dessert", listDessert(socket));
socket.on("list dessert", listDessert(socket));
socket.on("list user", listUser(socket));
socket.on("list service", listService(socket));
socket.on("add command", addCommand(socket));
socket.on("add user", addUSer(socket));
socket.on("set dish", setDish(socket));
socket.on("set ingredient", setIngredient(socket));
socket.on("set sauce", setSauce(socket));
socket.on("set drink", setDrink(socket));
socket.on("set dessert", setDessert(socket));
socket.on("set service", setService(socket));
socket.on("give command", giveCommand(socket));
socket.on("WIP command", WIPCommand(socket));

View file

@ -1,16 +1,13 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async () => {
let desserts = []
for (let i of await models.Dessert.findAll({
for (let d of await models.Dessert.findAll({
order: ["name"]
})) {
desserts.push({
id: i.id,
name: i.name,
price: i.price
});
desserts.push(utils.dessertExport(d));
}
socket.emit("list dessert", desserts);
}

View file

@ -1,4 +1,5 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async () => {
@ -6,13 +7,7 @@ module.exports = socket => {
for (let d of await models.Dish.findAll({
order: ["name"]
})) {
dishes.push({
id: d.id,
name: d.name,
price: d.price,
maxIngredients: d.maxIngredients,
maxSauces: d.maxSauces
});
dishes.push(utils.dishExport(d));
}
socket.emit("list dish", dishes);
}

View file

@ -1,16 +1,13 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async () => {
let drinks = []
for (let i of await models.Drink.findAll({
for (let d of await models.Drink.findAll({
order: ["name"]
})) {
drinks.push({
id: i.id,
name: i.name,
price: i.price
});
drinks.push(utils.drinkExport(d));
}
socket.emit("list drink", drinks);
}

View file

@ -1,4 +1,5 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async () => {
@ -6,11 +7,7 @@ module.exports = socket => {
for (let i of await models.Ingredient.findAll({
order: ["name"]
})) {
ingredients.push({
id: i.id,
name: i.name,
price: i.price
});
ingredients.push(utils.ingredientExport(i));
}
socket.emit("list ingredient", ingredients);
}

View file

@ -1,16 +1,13 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async () => {
let sauces = []
for (let d of await models.Sauce.findAll({
for (let s of await models.Sauce.findAll({
order: ["name"]
})) {
sauces.push({
id: d.id,
name: d.name,
price: d.price
});
sauces.push(utils.sauceExport(s));
}
socket.emit("list sauce", sauces);
}

21
sockets/setDessert.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async (data) => {
try {
let d = await models.Dessert.findByPk(data.id);
if (!d)
throw new Error("Dessert not found !");
if (await utils.update(d, data)) {
let send = utils.dessertExport(d);
socket.emit("set dessert", send);
socket.broadcast.emit("set dessert", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

21
sockets/setDish.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async (data) => {
try {
let d = await models.Dish.findByPk(data.id);
if (!d)
throw new Error("Dish not found !");
if (await utils.update(d, data)) {
let send = utils.dishExport(d);
socket.emit("set dish", send);
socket.broadcast.emit("set dish", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

21
sockets/setDrink.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async (data) => {
try {
let d = await models.Drink.findByPk(data.id);
if (!d)
throw new Error("Drink not found !");
if (await utils.update(d, data)) {
let send = utils.drinkExport(d);
socket.emit("set drink", send);
socket.broadcast.emit("set drink", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

21
sockets/setIngredient.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async (data) => {
try {
let i = await models.Ingredient.findByPk(data.id);
if (!i)
throw new Error("Ingredient not found !");
if (await utils.update(i, data)) {
let send = utils.ingredientExport(i);
socket.emit("set ingredient", send);
socket.broadcast.emit("set ingredient", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

21
sockets/setSauce.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../models");
const utils = require("./utils");
module.exports = socket => {
return async (data) => {
try {
let s = await models.Sauce.findByPk(data.id);
if (!s)
throw new Error("Sauce not found !");
if (await utils.update(s, data)) {
let send = utils.sauceExport(s);
socket.emit("set sauce", send);
socket.broadcast.emit("set sauce", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

View file

@ -17,6 +17,65 @@ function commandExport (c) {
}
}
function dishExport(d) {
return {
id: d.id,
name: d.name,
available: d.available,
price: d.price,
maxIngredients: d.maxIngredients,
maxSauces: d.maxSauces
}
}
function ingredientExport(i) {
return {
id: i.id,
name: i.name,
available: i.available,
price: i.price
}
}
function sauceExport(s) {
return {
id: s.id,
name: s.name,
available: s.available,
price: s.price
}
}
function drinkExport(d) {
return {
id: d.id,
name: d.name,
available: d.available,
price: d.price
}
}
function dessertExport(d) {
return {
id: d.id,
name: d.name,
available: d.available,
price: d.price
}
}
async function update(s, v) {
let edit = false;
for (let a in v)
if (s[a] !== v[a]) {
s[a] = v[a];
edit = true;
}
if (edit)
await s.save();
return edit;
}
async function resetService(c) {
let service = await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}, include: ["sandwich1", "sandwich2", "sandwich3"]});
if (c.WIP && service) {
@ -51,5 +110,11 @@ async function price(data) {
}
module.exports.commandExport = commandExport;
module.exports.dishExport = dishExport;
module.exports.ingredientExport = ingredientExport;
module.exports.sauceExport = sauceExport;
module.exports.drinkExport = drinkExport;
module.exports.dessertExport = dessertExport;
module.exports.resetService = resetService;
module.exports.update = update;
module.exports.price = price;

View file

@ -0,0 +1,23 @@
extends layout
block content
div#main-container.bg-contact2(style="background-image: url(/images/bg-01.jpg);")
form#stocks.container(action="")
div#dish
h1 Dishes
ul
div#ingredient
h1 Ingredients
ul
div#sauce
h1 Sauces
ul
div#drink
h1 Drinks
ul
div#dessert
h1 Desserts
ul
script(src="/socket.io/socket.io.js")
script(src="/javascripts/menu.js")