/
App.js
163 lines (140 loc) · 5.68 KB
/
App.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { useEffect, useState } from 'react';
import './App.css';
function Search({onSubmit, setSearchValue}){
const handleChange = (event) => {
setSearchValue(event.target.value)
}
return (
<div className='container'>
<form id='searchForm' onSubmit={onSubmit}>
<span className="material-symbols-outlined">search</span>
<input
type='text'
id='searchInput'
placeholder='Search for any dish'
onChange={handleChange}
></input>
<button type='submit'>Search</button>
</form>
</div>
)
}
export default function App() {
const [searchValue, setSearchValue] = useState('');
const [results, setResults] = useState([]);
const [searchResultText, setSrText] = useState('');
const [recipeVisible, setRecipeVisible] = useState(false);
const [mealRecipe, setMealRecipe] = useState([]);
useEffect(() => {
const searchResultContainer = document.getElementById('searchResultContainer');
function handleGridItemClick(event){
if(event.target.classList.contains('gridItem')){
const gridItemId = event.target.getAttribute('data-id');
console.log(gridItemId);
}
}
searchResultContainer.addEventListener('click', handleGridItemClick);
}, [])
function searchApi(event){
event.preventDefault();
// console.log(searchValue);
document.getElementById('searchInput').value = '';
if(searchValue !== ''){
fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchValue}`)
.then(response => response.json())
.then(data => {
// console.log(data);
if(data.meals !== null){
const fetchedResults = data.meals.map(item => (
<div
key={item.idMeal}
className='gridItem'
data-id={item.idMeal}
onClick={() => handleGridItemClick(item.idMeal)}
>
<img
src={item.strMealThumb}
style={{
width: 275,
height: 175
}}
alt={item.strMeal}
></img>
<h2 id='gridItemTitle'>{item.strMeal}</h2>
<p id='gridItemArea'>- {item.strArea}</p>
<p id='gridItemCategory'>- {item.strCategory}</p>
</div>
))
setResults(fetchedResults);
setSrText(`Search Results for ${searchValue}`);
}else{
setSrText(`No results found for ${searchValue}`);
setResults('');
}
})
}
}
function handleGridItemClick(gridItemId) {
// console.log(gridItemId);s
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${gridItemId}`)
.then(response => response.json())
.then(data => {
console.log(data);
const parsedIngredients = [];
function countIngredients(){
for(let i=1; i<20; i++){
let tempString = `strIngredient${i}`;
if(data.meals[0][tempString] === '' || data.meals[0][tempString] === null){
return i;
}
}
if(data.meals[0].strIngredient20 != ''){
return 20;
}
}
let numOfIngredients = countIngredients();
for(let i=1; i<numOfIngredients; i++){
let tempIngredient = `strIngredient${i}`;
let tempMeasure = `strMeasure${i}`;
parsedIngredients.push(
<li key={i}>{data.meals[0][tempMeasure]} of {data.meals[0][tempIngredient]}</li>
)
}
function handleClose(){
document.getElementById('specificRecipe').style.display = 'none';
}
const parsedMealRecipe = data.meals.map(meal => (
<div key={meal.idMeal}>
<h1 id='recipeTitle'>Recipe for {meal.strMeal}</h1>
<span className="material-symbols-outlined" onClick={handleClose}>close</span>
<div id='recipeIngredients'>
<h2>Ingredients</h2>
<ul>
{parsedIngredients}
</ul>
</div>
<div id='recipeInstructions'>
<h2>Instructions</h2>
{meal.strInstructions}
</div>
</div>
))
setMealRecipe(parsedMealRecipe);
setRecipeVisible(true);
})
document.getElementById('specificRecipe').style.display = 'block';
}
return (
<div>
<div id='header'>
<h1>Find any dish in the world!</h1>
</div>
<Search onSubmit={searchApi} setSearchValue={setSearchValue}></Search>
<h2 id='srHeader'>{searchResultText}</h2>
<div id='searchResultContainer'>
{results}
</div>
<div id='specificRecipe' style={{display: recipeVisible?'flex':'none'}}>{mealRecipe}</div>
</div>
)
}