diff --git a/client/src/Api.ts b/client/src/Api.ts index 2f02b4d..efb21c2 100644 --- a/client/src/Api.ts +++ b/client/src/Api.ts @@ -48,4 +48,8 @@ export const deletePizzaDay = async (login) => { export const updateChoice = async (name: string, choice: number | null) => { return await api.post('/api/updateChoice', JSON.stringify({ name, choice })); +} + +export const addPizza = async (login: string, pizzaIndex: number, pizzaSizeIndex: number) => { + return await api.post('/api/addPizza', JSON.stringify({ login, pizzaIndex, pizzaSizeIndex })); } \ No newline at end of file diff --git a/client/src/App.tsx b/client/src/App.tsx index 4e20ecd..3aa10a7 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,7 +1,7 @@ import React, { useContext, useEffect, useMemo, useRef, useState } from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; import { EVENT_DISCONNECT, EVENT_MESSAGE, SocketContext } from './context/socket'; -import { createPizzaDay, deletePizzaDay, getData, getFood, getPizzy, updateChoice } from './Api'; +import { addPizza, createPizzaDay, deletePizzaDay, getData, getFood, getPizzy, updateChoice } from './Api'; import { useAuth } from './context/auth'; import Login from './Login'; import { Locations, ClientData, Pizza } from './Types'; @@ -108,13 +108,17 @@ function App() { return suggestions; }, [pizzy]); - const handlePizzaChange = (value) => { + const handlePizzaChange = async (value) => { console.log("Pizza vybrána", value); - if (pizzy) { + if (auth?.login && pizzy) { const s = value.split('|'); - const pizza = pizzy[Number.parseInt(s[0])]; - const size = pizza.sizes[Number.parseInt(s[1])]; + const pizzaIndex = Number.parseInt(s[0]); + const pizzaSizeIndex = Number.parseInt(s[1]); + const pizza = pizzy[pizzaIndex]; + const size = pizza.sizes[pizzaSizeIndex]; + // TODO smazat console.log("Vybraná pizza a velikost", pizza, size); + await addPizza(auth.login, pizzaIndex, pizzaSizeIndex); } } @@ -196,7 +200,7 @@ function App() { :
Zatím nikdo nehlasoval...
} - {/* {!data.pizzaDay && + {!data.pizzaDay &&

Pro dnešní den není aktuálně založen Pizza day.

} @@ -218,7 +222,7 @@ function App() { onChange={handlePizzaChange} /> -
} */} + } } diff --git a/client/src/components/PizzaOrderList.tsx b/client/src/components/PizzaOrderList.tsx index d40bc1c..3afadd4 100644 --- a/client/src/components/PizzaOrderList.tsx +++ b/client/src/components/PizzaOrderList.tsx @@ -3,7 +3,7 @@ import { Table } from "react-bootstrap"; import { Order } from "../Types"; export default function PizzaOrderList({ orders }: { orders: Order[] }) { - return + return
diff --git a/server/src/chefie.ts b/server/src/chefie.ts index 363d912..ceda484 100644 --- a/server/src/chefie.ts +++ b/server/src/chefie.ts @@ -83,7 +83,7 @@ const downloadPizzy = async () => { /** * Vrátí pizzy z tempu, nebo čerstvě stažené, pokud v tempu nejsou. */ -export const fetchPizzy = async () => { +export const fetchPizzy = async (): Promise => { const tmpDir = os.tmpdir(); const date_ob = new Date(); const date = ("0" + date_ob.getDate()).slice(-2); diff --git a/server/src/index.ts b/server/src/index.ts index e01028d..45f0ec4 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -47,7 +47,7 @@ app.get("/api/pizza", (req, res) => { }); }); -// /** Založí pizza day pro aktuální den, za předpokladu že dosud neexistuje. */ +/** Založí pizza day pro aktuální den, za předpokladu že dosud neexistuje. */ app.post("/api/createPizzaDay", (req, res) => { console.log("Založení pizza day", req) // TODO smazat if (!req.body?.creator) { @@ -58,7 +58,7 @@ app.post("/api/createPizzaDay", (req, res) => { io.emit("message", data); }); -// /** Smaže pizza day pro aktuální den, za předpokladu že existuje. */ +/** Smaže pizza day pro aktuální den, za předpokladu že existuje. */ app.post("/api/deletePizzaDay", (req, res) => { if (!req.body?.login) { throw Error("Nebyl předán login uživatele"); @@ -67,6 +67,34 @@ app.post("/api/deletePizzaDay", (req, res) => { io.emit("message", getData()); }); +app.post("/api/addPizza", (req, res) => { + if (!req.body?.login) { + throw Error("Nebyl předán login"); + } + if (isNaN(req.body?.pizzaIndex)) { + throw Error("Nebyl předán index pizzy"); + } + const pizzaIndex = req.body.pizzaIndex; + if (isNaN(req.body?.pizzaSizeIndex)) { + throw Error("Nebyl předán index velikosti pizzy"); + } + const pizzaSizeIndex = req.body.pizzaSizeIndex; + fetchPizzy().then(pizzy => { + if (!pizzy[pizzaIndex]) { + throw Error("Neplatný index pizzy: " + pizzaIndex); + } + if (!pizzy[pizzaIndex].sizes[pizzaSizeIndex]) { + throw Error("Neplatný index velikosti pizzy: " + pizzaSizeIndex); + } + console.log("Vybraná pizza", pizzy[pizzaIndex], pizzy[pizzaIndex].sizes[pizzaSizeIndex]); + // TODO implementovat přidání objednávky - nutno zjistit co vlastně chceme ukládat + // pravděpodobně název, velikost, cenu + res.status(200).json({}); + // TODO odeslat aktuální data socketem + // io.emit("message", data); + }) +}); + app.post("/api/updateChoice", (req, res) => { console.log("Změna výběru", req.body); if (!req.body.hasOwnProperty('name')) {
Pizza