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

View File

@ -75,6 +75,7 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date, mo
if (!clientData?.menus?.[restaurant]) {
clientData.menus[restaurant] = {
lastUpdate: getHumanTime(new Date()),
closed: false,
food: [],
};
switch (restaurant) {
@ -82,7 +83,11 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date, mo
clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock);
break;
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;
case Restaurants.TECHTOWER:
clientData.menus[restaurant].food = await getMenuTechTower(date, mock);

View File

@ -80,6 +80,7 @@ export interface ClientData {
/** Nabídka jídel jednoho podniku. */
export interface 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
}