-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvailableMeals.jsx
99 lines (94 loc) · 2.27 KB
/
AvailableMeals.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import classes from "./AvailableMeals.module.css";
import MealItem from "./MealItem/MealItem";
import Card from "../UI/Card";
import { useEffect, useState } from "react";
// const DUMMY_MEALS = [
// {
// id: "m1",
// name: "Sushi",
// description: "Finest fish and veggies",
// price: 22.99,
// },
// {
// id: "m2",
// name: "Schnitzel",
// description: "A german specialty!",
// price: 16.5,
// },
// {
// id: "m3",
// name: "Barbecue Burger",
// description: "American, raw, meaty",
// price: 12.99,
// },
// {
// id: "m4",
// name: "Green Bowl",
// description: "Healthy...and green...",
// price: 18.99,
// },
// ];
const AvailableMeals = () => {
const [meals, setMeals] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
// console.log(avaliableMealsList);
useEffect(() => {
const loadedMeals = [];
async function fetchMeals() {
try {
const apiUrl = process.env.REACT_APP_AVAILABLE_MEALS;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error("Uh oh...Something went terribly wrong....!!");
}
const data = await response.json();
for (const mealKey in data) {
const meal = { id: mealKey, ...data[mealKey] };
loadedMeals.push(meal);
}
setMeals(loadedMeals);
setIsLoading(false);
} catch (error) {
setError(error.message);
setIsLoading(false);
}
}
fetchMeals();
}, []);
if (isLoading) {
return (
<section className={classes.meals}>
<Card>
<p className={classes.mealsLoading}>Loading....</p>;
</Card>
</section>
);
}
if (error) {
return (
<section className={classes.meals}>
<Card>
<p className={classes.error}>{error}</p>;
</Card>
</section>
);
}
const mealsList = meals.map((meal) => (
<MealItem
id={meal.id}
key={meal.id}
name={meal.name}
description={meal.description}
price={meal.price}
/>
));
return (
<section className={classes.meals}>
<Card>
<ul>{mealsList}</ul>
</Card>
</section>
);
};
export default AvailableMeals;