-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeFinder.html
More file actions
189 lines (158 loc) · 6.99 KB
/
RecipeFinder.html
File metadata and controls
189 lines (158 loc) · 6.99 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe Finder</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #f8f9fa;
}
.container {
max-width: 600px;
}
.card {
margin-top: 20px;
}
.loading {
text-align: center;
font-size: 20px;
}
.recipe-image {
width: 100%;
height: auto;
border-radius: 10px;
}
.input-wrapper {
position: relative;
}
.clear-btn {
position: absolute;
top: 72%;
right: 10px;
transform: translateY(-50%);
background: none;
border: none;
font-size: 18px;
cursor: pointer;
color: #888;
}
</style>
</head>
<body>
<div class="container my-5">
<h1 class="text-center mb-4">Recipe Finder</h1>
<!-- Search Form -->
<form id="recipeForm" class="mb-4">
<div class="mb-3 input-wrapper">
<label for="dishName" class="form-label">Dish Name</label>
<input type="text" id="dishName" class="form-control" placeholder="e.g. Chicken Alfredo">
<button type="button" id="clearButton" class="clear-btn" style="display: none;">×</button>
</div>
<button type="submit" class="btn btn-primary w-100">Find Recipe</button>
</form>
<!-- Loading Message -->
<div id="loadingMessage" class="loading" style="display: none;">
<p>Loading...</p>
</div>
<!-- Result Section -->
<div id="recipeResults" class="mt-4">
<!-- Recipe results will be displayed here -->
</div>
</div>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<script>
const recipeForm = document.getElementById('recipeForm');
const dishNameInput = document.getElementById('dishName');
const recipeResults = document.getElementById('recipeResults');
const loadingMessage = document.getElementById('loadingMessage');
const clearButton = document.getElementById('clearButton');
// API URL and headers
const url = 'https://ai-food-recipe-generator-api-custom-diet-quick-meals.p.rapidapi.com/generate?noqueue=1';
const headers = {
'x-rapidapi-key': '5abf6b8165mshff41c857ad49628p14130bjsn27547c3b91e9', // Replace with your own API key
'x-rapidapi-host': 'ai-food-recipe-generator-api-custom-diet-quick-meals.p.rapidapi.com',
'Content-Type': 'application/json'
};
// Show/hide clear button
dishNameInput.addEventListener('input', () => {
if (dishNameInput.value.trim() !== '') {
clearButton.style.display = 'block'; // Show the clear button
} else {
clearButton.style.display = 'none'; // Hide the clear button
}
});
// Clear input field when clear button is clicked
clearButton.addEventListener('click', () => {
dishNameInput.value = '';
clearButton.style.display = 'none'; // Hide the clear button after clearing input
});
// Submit event listener for the form
recipeForm.addEventListener('submit', async (event) => {
event.preventDefault();
const dishName = dishNameInput.value.trim();
// Display the loading message
loadingMessage.style.display = 'block';
recipeResults.innerHTML = '';
// Make API request with the specified ingredients
const requestBody = {
ingredients: ['chicken', 'rice', 'pepper'], // Use the ingredients you want to search for
dietary_restrictions: ['gluten_free'], // Optional dietary restrictions
cuisine: 'Italian', // Optional cuisine type
meal_type: 'dinner', // Optional meal type
servings: 4, // Default servings
lang: 'en' // Language in English
};
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(requestBody) // JSON.stringify the body data
});
// Check if the response is okay (status 200)
if (!response.ok) {
throw new Error('Failed to fetch recipe data. Response code: ' + response.status);
}
const result = await response.json();
// Clear previous results
recipeResults.innerHTML = '';
loadingMessage.style.display = 'none'; // Hide loading message
// Log result for debugging
console.log(result);
// Check if the recipe exists and has necessary information
if (result && result.result && result.result.title) {
const recipe = result.result;
// Extract only the ingredient names
const ingredientList = recipe.ingredients ? recipe.ingredients.map(ingredient => ingredient.name || ingredient) : [];
// Display Recipe HTML
const recipeHTML = `
<div class="card">
<div class="card-body">
<h5 class="card-title">${recipe.title}</h5>
<p class="card-text"><strong>Ingredients:</strong> ${ingredientList.join(', ')}</p>
<p class="card-text"><strong>Instructions:</strong> ${recipe.instructions.join(' ')}</p>
${recipe.nutrition_info ? `
<p class="card-text"><strong>Nutrition:</strong>
Calories: ${recipe.nutrition_info.calories},
Fat: ${recipe.nutrition_info.fat}g,
Protein: ${recipe.nutrition_info.protein}g
</p>` : ''}
</div>
</div>
`;
recipeResults.innerHTML = recipeHTML;
} else {
recipeResults.innerHTML = '<p class="text-danger">Recipe information is incomplete.</p>';
}
} catch (error) {
console.error(error);
recipeResults.innerHTML = '<p class="text-danger">An error occurred while fetching the recipe. Please try again later.</p>';
}
});
</script>
</body>
</html>