Výběr obědu kliknutím
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful

This commit is contained in:
Michal Hájek 2025-02-01 23:42:10 +01:00 committed by Martin Berka
parent ff650ec3b8
commit eaf0bc353d
3 changed files with 27 additions and 11 deletions

View File

@ -189,6 +189,13 @@ function App() {
} }
}, [auth?.login, easterEgg?.duration, easterEgg?.url, eggImage]); }, [auth?.login, easterEgg?.duration, easterEgg?.url, eggImage]);
const doAddClickFoodChoice = async (location: Locations, foodIndex: number) => {
const locationKey = Object.keys(Locations).find(key => Locations[key as keyof typeof Locations] === location) as LocationKey;
if (auth?.login) {
await errorHandler(() => addChoice(locationKey, foodIndex, dayIndex));
}
}
const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => { const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => {
const locationKey = event.target.value as LocationKey; const locationKey = event.target.value as LocationKey;
if (auth?.login) { if (auth?.login) {
@ -331,15 +338,15 @@ function App() {
} }
} }
const renderFoodTable = (name: string, menu: DayMenu) => { const renderFoodTable = (name: string, location: Locations, menu: DayMenu) => {
let content; let content;
if (menu?.closed) { if (menu?.closed) {
content = <h3>Zavřeno</h3> content = <h3>Zavřeno</h3>
} else if (menu?.food?.length > 0) { } else if (menu?.food?.length > 0) {
content = <Table striped bordered hover> content = <Table striped bordered hover>
<tbody> <tbody style={{ cursor: 'pointer' }}>
{menu.food.filter(f => (settings?.hideSoups ? !f.isSoup : true)).map((f: any, index: number) => {menu.food.filter(f => (settings?.hideSoups ? !f.isSoup : true)).map((f: any, index: number) =>
<tr key={index}> <tr key={index} onClick={() => doAddClickFoodChoice(location, 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>
@ -401,8 +408,8 @@ function App() {
<img src='snowman.png' style={{ position: "absolute", height: "110px", right: 10, top: 5 }} /> <img src='snowman.png' style={{ position: "absolute", height: "110px", right: 10, top: 5 }} />
Poslední změny: Poslední změny:
<ul> <ul>
<li>Přidání restaurace Zastávka u Michala</li> <li>Přidání restaurací Zastávka u Michala a Pivovarský šenk Šeříková</li>
<li>Přidání restaurace Pivovarský šenk Šeříková</li> <li>Možnost výběru restaurace a jídel kliknutím v tabulce</li>
</ul> </ul>
</Alert> </Alert>
{dayIndex != null && {dayIndex != null &&
@ -413,11 +420,11 @@ function App() {
</div> </div>
} }
<Row className='food-tables'> <Row className='food-tables'>
{food[Restaurants.SLADOVNICKA] && renderFoodTable('Sladovnická', food[Restaurants.SLADOVNICKA])} {food[Restaurants.SLADOVNICKA] && renderFoodTable('Sladovnická', Locations.SLADOVNICKA, food[Restaurants.SLADOVNICKA])}
{/* {food[Restaurants.UMOTLIKU] && renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])} */} {/* {food[Restaurants.UMOTLIKU] && renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])} */}
{food[Restaurants.TECHTOWER] && renderFoodTable('TechTower', food[Restaurants.TECHTOWER])} {food[Restaurants.TECHTOWER] && renderFoodTable('TechTower', Locations.TECHTOWER, food[Restaurants.TECHTOWER])}
{food[Restaurants.ZASTAVKAUMICHALA] && renderFoodTable('Zastávka u Michala', food[Restaurants.ZASTAVKAUMICHALA])} {food[Restaurants.ZASTAVKAUMICHALA] && renderFoodTable('Zastávka u Michala', Locations.ZASTAVKAUMICHALA, food[Restaurants.ZASTAVKAUMICHALA])}
{food[Restaurants.SENKSERIKOVA] && renderFoodTable('Pivovarský šenk Šeříková', food[Restaurants.SENKSERIKOVA])} {food[Restaurants.SENKSERIKOVA] && renderFoodTable('Pivovarský šenk Šeříková', Locations.SENKSERIKOVA, food[Restaurants.SENKSERIKOVA])}
</Row> </Row>
<div className='content-wrapper'> <div className='content-wrapper'>
<div className='content'> <div className='content'>

View File

@ -268,14 +268,19 @@ export async function removeChoice(login: string, trusted: boolean, locationKey:
} }
/** /**
* Odstraní kompletně volbu uživatele. * Odstraní kompletně volbu uživatele, vyjma ignoredLocationKey (pokud byla předána a existuje).
* *
* @param login login uživatele * @param login login uživatele
* @param date datum, ke kterému se volby vztahují
* @param ignoredLocationKey volba, která nebude odstraněna, pokud existuje
*/ */
async function removeChoiceIfPresent(login: string, date: string) { async function removeChoiceIfPresent(login: string, date: string, ignoredLocationKey?: LocationKey) {
let data: DayData = await storage.getData(date); let data: DayData = await storage.getData(date);
for (const key of Object.keys(data.choices)) { for (const key of Object.keys(data.choices)) {
const locationKey = key as LocationKey; const locationKey = key as LocationKey;
if (ignoredLocationKey != null && ignoredLocationKey == locationKey) {
continue;
}
if (data.choices[locationKey] && login in data.choices[locationKey]) { if (data.choices[locationKey] && login in data.choices[locationKey]) {
delete data.choices[locationKey][login]; delete data.choices[locationKey][login];
if (Object.keys(data.choices[locationKey]).length === 0) { if (Object.keys(data.choices[locationKey]).length === 0) {
@ -329,6 +334,9 @@ export async function addChoice(login: string, trusted: boolean, locationKey: Lo
// Pokud měníme pouze lokaci, mažeme případné předchozí // Pokud měníme pouze lokaci, mažeme případné předchozí
if (foodIndex == null) { if (foodIndex == null) {
data = await removeChoiceIfPresent(login, selectedDate); data = await removeChoiceIfPresent(login, selectedDate);
} else {
// Mažeme případné ostatní volby (měla by být maximálně jedna)
removeChoiceIfPresent(login, selectedDate, locationKey);
} }
// TODO vytáhnout inicializaci "prázdné struktury" do vlastní funkce // TODO vytáhnout inicializaci "prázdné struktury" do vlastní funkce
if (!(data.choices[locationKey])) { if (!(data.choices[locationKey])) {

View File

@ -111,6 +111,7 @@ export type Food = {
} }
// TODO tohle je dost špatné pojmenování, vzhledem k tomu co to obsahuje // TODO tohle je dost špatné pojmenování, vzhledem k tomu co to obsahuje
// TODO pokud by se použilo ovládáni výběru obědu kliknutím, pak bych restaurace z tohoto výčtu vyhodil
export enum Locations { export enum Locations {
SLADOVNICKA = 'Sladovnická', SLADOVNICKA = 'Sladovnická',
// UMOTLIKU = 'U Motlíků', // UMOTLIKU = 'U Motlíků',