-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecipeSuggestion.js
More file actions
135 lines (131 loc) · 4.26 KB
/
Copy pathRecipeSuggestion.js
File metadata and controls
135 lines (131 loc) · 4.26 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
"use client"
import React, { useState } from 'react';
import axios from 'axios';
import { Button, Box, Typography, Stack, CircularProgress } from '@mui/material';
const RecipeSuggestion = ({ pantryItems }) => {
const [recipes, setRecipes] = useState([]);
const [loading, setLoading] = useState(false);
const fetchRecipes = async () => {
setLoading(true);
try {
const ingredients = pantryItems.join(','); // Convert array to comma-separated string
const response = await axios.get('https://api.spoonacular.com/recipes/findByIngredients', {
params: {
ingredients: ingredients,
number: 5, // Number of recipes to fetch
apiKey: process.env.NEXT_PUBLIC_SPOONACULAR_API_KEY, // Ensure to set this in .env.local
},
});
const recipeIds = response.data.map(recipe => recipe.id);
const recipeInfoPromises = recipeIds.map(id => axios.get(`https://api.spoonacular.com/recipes/${id}/information`, {
params: {
apiKey: process.env.NEXT_PUBLIC_SPOONACULAR_API_KEY,
},
}));
const recipeInfos = await Promise.all(recipeInfoPromises);
const recipesWithSourceUrl = response.data.map((recipe, index) => ({
...recipe,
sourceUrl: recipeInfos[index].data.sourceUrl,
}));
setRecipes(recipesWithSourceUrl);
} catch (error) {
console.error('Error fetching recipes:', error);
}
setLoading(false);
};
return (
<Box sx={{ mt: 4, textAlign: 'center', padding: '0 16px', backgroundColor: '#f0f4f8', minHeight: '100vh' }}>
<Typography
variant="h3"
fontFamily="Poppins"
fontWeight="bold"
color="#0033cc"
mt={{ xs: 2, md: 4 }}
mb={{ xs: 2, md: 3 }}
>
Recipe Suggestion
</Typography>
<Button
variant="outlined"
onClick={fetchRecipes}
sx={{
backgroundColor: '#0033cc',
color: '#fff',
borderRadius: '15px',
fontFamily: 'Poppins',
textTransform: 'none',
'&:hover': {
backgroundColor: '#002a80',
borderColor: '#fff',
color: 'white',
},
mb: 3,
padding: '10px 20px',
}}
>
Generate Recipes
</Button>
{loading ? (
<CircularProgress sx={{ color: '#0033cc' }} />
) : (
<Stack spacing={2} alignItems="center">
{recipes.map((recipe) => (
<Box
key={recipe.id}
sx={{
p: 2,
border: '1px solid #ddd',
borderRadius: '8px',
width: '100%',
maxWidth: 600,
backgroundColor: '#fff',
textAlign: 'left',
boxShadow: '0px 4px 8px rgba(0, 0, 0, 0.1)'
}}
>
<Typography
variant="h6"
fontFamily="Poppins"
color="#0033cc"
mb={1}
>
{recipe.title}
</Typography>
<Typography
variant="body2"
fontFamily="Nunito"
color="#333"
mb={1}
>
Ingredients: {recipe.usedIngredients.map(ing => ing.name).join(', ')}
</Typography>
<Button
variant="outlined"
href={recipe.sourceUrl}
target="_blank"
sx={{
mt: 1,
backgroundColor: '#0033cc',
borderColor: '#0033cc',
color: '#fff',
borderRadius: '15px',
fontFamily: 'Poppins',
textTransform: 'none',
'&:hover': {
backgroundColor: '#002a80',
borderColor: '#002a80',
color: 'white',
},
padding: '10px 20px',
}}
>
View Recipe
</Button>
</Box>
))}
</Stack>
)}
</Box>
);
};
export default RecipeSuggestion;