Opravy TypeScriptu

This commit is contained in:
Martin Berka 2023-09-06 22:43:51 +02:00
parent 1a2b3c425e
commit 8615286c45
3 changed files with 19 additions and 19 deletions

View File

@ -43,7 +43,7 @@ function App() {
const bank = useBank(); const bank = useBank();
const [isConnected, setIsConnected] = useState<boolean>(false); const [isConnected, setIsConnected] = useState<boolean>(false);
const [data, setData] = useState<ClientData>(); const [data, setData] = useState<ClientData>();
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 [closed, setClosed] = useState<boolean>(false);
@ -144,8 +144,8 @@ function App() {
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); setClosed(food[restaurant]?.closed ?? false);
} else { } else {
setFoodChoiceList(undefined); setFoodChoiceList(undefined);
setClosed(false); setClosed(false);
@ -372,9 +372,9 @@ function App() {
</div> </div>
} }
<Row className='food-tables'> <Row className='food-tables'>
{renderFoodTable('Sladovnická', food[Restaurants.SLADOVNICKA])} {food[Restaurants.SLADOVNICKA] && renderFoodTable('Sladovnická', food[Restaurants.SLADOVNICKA])}
{renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])} {food[Restaurants.UMOTLIKU] && renderFoodTable('U Motlíků', food[Restaurants.UMOTLIKU])}
{renderFoodTable('TechTower', food[Restaurants.TECHTOWER])} {food[Restaurants.TECHTOWER] && renderFoodTable('TechTower', food[Restaurants.TECHTOWER])}
</Row> </Row>
<div className='content-wrapper'> <div className='content-wrapper'>
<div className='content'> <div className='content'>
@ -390,7 +390,7 @@ function App() {
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);
const v = Object.values(Restaurants)[restaurantKey]; const v = Object.values(Restaurants)[restaurantKey];
return v == null || !food[v].closed; return v == null || !food[v]?.closed;
}) })
.map(entry => <option key={entry[0]} value={entry[0]}>{entry[1]}</option>)} .map(entry => <option key={entry[0]} value={entry[0]}>{entry[1]}</option>)}
</Form.Select> </Form.Select>
@ -444,7 +444,7 @@ function App() {
const locationsKey = Object.keys(Locations)[Number(locationKey)] const locationsKey = Object.keys(Locations)[Number(locationKey)]
const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey);
const restaurant = Object.values(Restaurants)[restaurantKey]; const restaurant = Object.values(Restaurants)[restaurantKey];
const foodName = food[restaurant].food[foodIndex].name; const foodName = food[restaurant]?.food[foodIndex].name;
return <li key={foodIndex}> return <li key={foodIndex}>
{foodName} {foodName}
{login === auth.login && <FontAwesomeIcon onClick={() => { {login === auth.login && <FontAwesomeIcon onClick={() => {

View File

@ -91,13 +91,13 @@ export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
*/ */
export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise<Menu> { export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise<Menu> {
await initIfNeeded(date); await initIfNeeded(date);
const today = formatDate(date ?? getToday()); const selectedDay = formatDate(date ?? getToday());
const clientData: ClientData = await storage.getData(today); const clientData: ClientData = await storage.getData(selectedDay);
if (!clientData.menus) { if (!clientData.menus) {
clientData.menus = {}; clientData.menus = {};
storage.setData(today, clientData); storage.setData(selectedDay, clientData);
} }
if (!clientData?.menus?.[restaurant]) { if (!clientData.menus[restaurant]) {
clientData.menus[restaurant] = { clientData.menus[restaurant] = {
lastUpdate: getHumanTime(new Date()), lastUpdate: getHumanTime(new Date()),
closed: false, closed: false,
@ -106,22 +106,22 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P
const mock = process.env.MOCK_DATA === 'true'; const mock = process.env.MOCK_DATA === 'true';
switch (restaurant) { switch (restaurant) {
case Restaurants.SLADOVNICKA: case Restaurants.SLADOVNICKA:
clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock); clientData.menus[restaurant]!.food = await getMenuSladovnicka(date, mock);
break; break;
case Restaurants.UMOTLIKU: case Restaurants.UMOTLIKU:
const uMotlikuFood = await getMenuUMotliku(date, mock); const uMotlikuFood = await getMenuUMotliku(date, mock);
clientData.menus[restaurant].food = uMotlikuFood; clientData.menus[restaurant]!.food = uMotlikuFood;
if (uMotlikuFood.length === 1 && uMotlikuFood[0].name.toLowerCase() === 'zavřeno') { if (uMotlikuFood.length === 1 && uMotlikuFood[0].name.toLowerCase() === 'zavřeno') {
clientData.menus[restaurant].closed = true; 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);
break; break;
} }
storage.setData(today, clientData); storage.setData(selectedDay, clientData);
} }
return clientData?.menus?.[restaurant]; return clientData.menus[restaurant]!;
} }
/** /**

View File

@ -72,7 +72,7 @@ export interface ClientData {
isWeekend: boolean, // příznak, zda je dnes víkend isWeekend: boolean, // příznak, zda je dnes víkend
weekIndex: number, // index aktuálního dne v týdnu (0-6) weekIndex: number, // index aktuálního dne v týdnu (0-6)
choices: Choices, // seznam voleb choices: Choices, // seznam voleb
menus?: { [restaurant in Restaurants]: Menu }, // menu jednotlivých restaurací menus?: { [restaurant in Restaurants]?: Menu }, // menu jednotlivých restaurací
pizzaDay?: PizzaDay, // pizza day pro dnešní den, pokud existuje pizzaDay?: PizzaDay, // pizza day pro dnešní den, pokud existuje
pizzaList?: Pizza[], // seznam dostupných pizz pro dnešní den pizzaList?: Pizza[], // seznam dostupných pizz pro dnešní den
pizzaListLastUpdate?: Date, // datum a čas poslední aktualizace pizz pizzaListLastUpdate?: Date, // datum a čas poslední aktualizace pizz