Neumožnit výběr zavřených podniků

This commit is contained in:
Martin Berka 2023-08-11 00:17:11 +02:00
parent 45bd84f96f
commit 282184b80b
3 changed files with 44 additions and 19 deletions

View File

@ -44,6 +44,7 @@ function App() {
const [food, setFood] = useState<{ [key in Restaurants]: Menu }>(); const [food, setFood] = useState<{ [key in Restaurants]: Menu }>();
const [myOrder, setMyOrder] = useState<Order>(); const [myOrder, setMyOrder] = useState<Order>();
const [foodChoiceList, setFoodChoiceList] = useState<Food[]>(); const [foodChoiceList, setFoodChoiceList] = useState<Food[]>();
const [closed, setClosed] = useState<boolean>(false);
const socket = useContext(SocketContext); const socket = useContext(SocketContext);
const choiceRef = useRef<HTMLSelectElement>(null); const choiceRef = useRef<HTMLSelectElement>(null);
const foodChoiceRef = useRef<HTMLSelectElement>(null); const foodChoiceRef = useRef<HTMLSelectElement>(null);
@ -111,22 +112,26 @@ function App() {
useEffect(() => { useEffect(() => {
if (choiceRef?.current?.value && choiceRef.current.value !== "") { if (choiceRef?.current?.value && choiceRef.current.value !== "") {
// TODO: wtf, cos pil, když jsi tohle psal? // TODO: wtf, cos pil, když jsi tohle psal?
const locationIndex = Object.values(Locations).indexOf(choiceRef?.current?.value as unknown as Locations); const key = choiceRef?.current?.value;
const locationIndex = Object.keys(Locations).indexOf(key as unknown as Locations);
const locationsKey = Object.keys(Locations)[locationIndex]; const locationsKey = Object.keys(Locations)[locationIndex];
const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey);
if (restaurantKey > -1 && food) { if (restaurantKey > -1 && food) {
const restaurant = Object.values(Restaurants)[restaurantKey]; const restaurant = Object.values(Restaurants)[restaurantKey];
setFoodChoiceList(food[restaurant].food); setFoodChoiceList(food[restaurant].food);
setClosed(food[restaurant].closed);
} else { } else {
setFoodChoiceList(undefined); setFoodChoiceList(undefined);
setClosed(false);
} }
} else { } else {
setFoodChoiceList(undefined); setFoodChoiceList(undefined);
setClosed(false);
} }
}, [choiceRef.current?.value, food]) }, [choiceRef.current?.value, food])
const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => { const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => {
const index = Object.values(Locations).indexOf(event.target.value as unknown as Locations); const index = Object.keys(Locations).indexOf(event.target.value as unknown as Locations);
if (auth?.login) { if (auth?.login) {
await addChoice(index); await addChoice(index);
if (foodChoiceRef.current?.value) { if (foodChoiceRef.current?.value) {
@ -137,8 +142,9 @@ function App() {
const doAddFoodChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => { const doAddFoodChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => {
if (event.target.value && foodChoiceList?.length && choiceRef.current?.value) { if (event.target.value && foodChoiceList?.length && choiceRef.current?.value) {
const restaurantKey = choiceRef.current.value;
if (auth?.login) { if (auth?.login) {
const locationIndex = Object.values(Locations).indexOf(choiceRef.current.value as unknown as Locations); const locationIndex = Object.keys(Locations).indexOf(restaurantKey as unknown as Locations);
await addChoice(locationIndex, Number(event.target.value)); await addChoice(locationIndex, Number(event.target.value));
} }
} }
@ -243,20 +249,28 @@ function App() {
} }
const renderFoodTable = (name: string, menu: Menu) => { const renderFoodTable = (name: string, menu: Menu) => {
return <Col md={12} lg={4}> let content;
<h3>{name}</h3> if (menu?.closed) {
{menu?.lastUpdate && <small>Poslední aktualizace: {menu.lastUpdate}</small>} content = <h3>Zavřeno</h3>
<Table striped bordered hover> } else if (menu?.food?.length > 0) {
content = <Table striped bordered hover>
<tbody> <tbody>
{menu?.food?.length > 0 ? menu.food.map((f: any, index: number) => {menu.food.map((f: any, index: number) =>
<tr key={index}> <tr key={index}>
<td>{f.amount}</td> <td>{f.amount}</td>
<td>{f.name}</td> <td>{f.name}</td>
<td>{f.price}</td> <td>{f.price}</td>
</tr> </tr>
) : <h1>Hmmmmm podivné.... nic se nevrátilo</h1>} )}
</tbody> </tbody>
</Table> </Table>
} else {
content = <h3>Chyba načtení dat</h3>
}
return <Col md={12} lg={4}>
<h3>{name}</h3>
{menu?.lastUpdate && <small>Poslední aktualizace: {menu.lastUpdate}</small>}
{content}
</Col> </Col>
} }
@ -285,6 +299,7 @@ function App() {
<li>Zobrazení jména uživatele místo loginu při přihlášení přes Authelia</li> <li>Zobrazení jména uživatele místo loginu při přihlášení přes Authelia</li>
<li>Funkční odhlášení přes Authelia</li> <li>Funkční odhlášení přes Authelia</li>
<li>Oprava stahování pizz z Pizza Chefie</li> <li>Oprava stahování pizz z Pizza Chefie</li>
<li>Pokročilá AI dektece zavřených podniků</li>
</ul> </ul>
</Alert> </Alert>
<h1 className='title'>Dnes je {data.date}</h1> <h1 className='title'>Dnes je {data.date}</h1>
@ -298,23 +313,27 @@ function App() {
<p>Jak to dnes vidíš s obědem?</p> <p>Jak to dnes vidíš s obědem?</p>
<Form.Select ref={choiceRef} onChange={doAddChoice}> <Form.Select ref={choiceRef} onChange={doAddChoice}>
<option></option> <option></option>
<option value={Locations.SLADOVNICKA}>Sladovnická</option> {Object.entries(Locations)
<option value={Locations.UMOTLIKU}>U Motlíků</option> .filter(entry => {
<option value={Locations.TECHTOWER}>TechTower</option> // TODO: wtf, cos pil, když jsi tohle psal? v2
<option value={Locations.SPSE}>SPŠE</option> const key = entry[0];
<option value={Locations.PIZZA}>Pizza day</option> const locationIndex = Object.keys(Locations).indexOf(key as unknown as Locations);
<option value={Locations.OBJEDNAVAM}>Budu objednávat (mimo pizzu)</option> const locationsKey = Object.keys(Locations)[locationIndex];
<option value={Locations.NEOBEDVAM}>Mám vlastní/neobědvám</option> const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey);
const v = Object.values(Restaurants)[restaurantKey];
return v == null || !food[v].closed;
})
.map(entry => <option key={entry[0]} value={entry[0]}>{entry[1]}</option>)}
</Form.Select> </Form.Select>
<small>Je možné vybrat jen jednu možnost. Výběr jiné odstraní předchozí.</small> <small>Je možné vybrat jen jednu možnost. Výběr jiné odstraní předchozí.</small>
{foodChoiceList && <> {foodChoiceList && !closed && <>
<p style={{ marginTop: "10px" }}>Na co dobrého? <small>(nepovinné)</small></p> <p style={{ marginTop: "10px" }}>Na co dobrého? <small>(nepovinné)</small></p>
<Form.Select ref={foodChoiceRef} onChange={doAddFoodChoice}> <Form.Select ref={foodChoiceRef} onChange={doAddFoodChoice}>
<option></option> <option></option>
{foodChoiceList.map((food, index) => <option key={index} value={index}>{food.name}</option>)} {foodChoiceList.map((food, index) => <option key={index} value={index}>{food.name}</option>)}
</Form.Select> </Form.Select>
</>} </>}
{foodChoiceList && <> {foodChoiceList && !closed && <>
<p style={{ marginTop: "10px" }}>V kolik hodin preferuješ odchod?</p> <p style={{ marginTop: "10px" }}>V kolik hodin preferuješ odchod?</p>
<Form.Select ref={foodChoiceRef} onChange={handleChangeDepartureTime}> <Form.Select ref={foodChoiceRef} onChange={handleChangeDepartureTime}>
<option></option> <option></option>

View File

@ -75,6 +75,7 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date, mo
if (!clientData?.menus?.[restaurant]) { if (!clientData?.menus?.[restaurant]) {
clientData.menus[restaurant] = { clientData.menus[restaurant] = {
lastUpdate: getHumanTime(new Date()), lastUpdate: getHumanTime(new Date()),
closed: false,
food: [], food: [],
}; };
switch (restaurant) { switch (restaurant) {
@ -82,7 +83,11 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date, mo
clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock); clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock);
break; break;
case Restaurants.UMOTLIKU: case Restaurants.UMOTLIKU:
clientData.menus[restaurant].food = await getMenuUMotliku(date, mock); const uMotlikuFood = await getMenuUMotliku(date, mock);
clientData.menus[restaurant].food = uMotlikuFood;
if (uMotlikuFood.length === 1 && uMotlikuFood[0].name.toLowerCase() === 'zavřeno') {
clientData.menus[restaurant].closed = true;
}
break; break;
case Restaurants.TECHTOWER: case Restaurants.TECHTOWER:
clientData.menus[restaurant].food = await getMenuTechTower(date, mock); clientData.menus[restaurant].food = await getMenuTechTower(date, mock);

View File

@ -80,6 +80,7 @@ export interface ClientData {
/** Nabídka jídel jednoho podniku. */ /** Nabídka jídel jednoho podniku. */
export interface Menu { export interface Menu {
lastUpdate: string, // human-readable čas poslední aktualizace menu lastUpdate: string, // human-readable čas poslední aktualizace menu
closed: boolean, // příznak, zda je daný podnik aktuálně zavřený
food: Food[], // seznam jídel v menu food: Food[], // seznam jídel v menu
} }