Worked on the latest Feedback
This commit is contained in:
parent
f553241092
commit
1affdf6274
|
@ -1,32 +1,32 @@
|
|||
/* eslint-disable react/prop-types */
|
||||
export const RecipeFilters = ({ filters, onFilterChange }) => (
|
||||
<div className="filters-container">
|
||||
<select value={filters?.diet}
|
||||
onChange={(e) => onFilterChange('diet', e?.target?.value)}
|
||||
<select
|
||||
value={filters?.diet}
|
||||
onChange={(e) => onFilterChange("diet", e?.target?.value)}
|
||||
className="filter-select"
|
||||
>
|
||||
|
||||
<option value="">All Diets</option>
|
||||
<option value="vegetarian">Vegetarian</option>
|
||||
<option value="vegan">Vegan</option>
|
||||
<option value="gluten-free">Gluten Free</option>
|
||||
</select>
|
||||
|
||||
|
||||
<select value={filters?.cuisine}
|
||||
onChange={(e) => onFilterChange('cuisine', e?.target?.value)}
|
||||
<select
|
||||
value={filters?.cuisine}
|
||||
onChange={(e) => onFilterChange("cuisine", e?.target?.value)}
|
||||
className="filter-select"
|
||||
>
|
||||
|
||||
<option value="american">American</option>
|
||||
<option value="italian">Italian</option>
|
||||
<option value="indian">Indian</option>
|
||||
<option value="mexican">Mexican</option>
|
||||
</select>
|
||||
<input type="number"
|
||||
<input
|
||||
type="number"
|
||||
placeholder="Max cooking time (mins)"
|
||||
value={filters?.maxTime}
|
||||
onChange={(e) => onFilterChange('maxTime', e?.target?.value)}
|
||||
onChange={(e) => onFilterChange("maxTime", e?.target?.value)}
|
||||
className="filter-input"
|
||||
/>
|
||||
</div>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* eslint-disable react/prop-types */
|
||||
import { Clock, Users } from 'lucide-react';
|
||||
import { Clock, Users } from "lucide-react";
|
||||
|
||||
export const RecipeDetailsCard = ({ recipe, onClick }) => (
|
||||
<div className="recipe-card" onClick={() => onClick(recipe)}>
|
||||
|
@ -7,18 +7,15 @@ export const RecipeDetailsCard = ({ recipe, onClick }) => (
|
|||
<div className="recipe-content">
|
||||
<h3 className="recipe-title">{recipe?.title}</h3>
|
||||
<div className="recipe-info">
|
||||
<div className="info-item">
|
||||
<Clock className="info-icon" />
|
||||
{recipe?.readyInMinutes || '30'} mins
|
||||
<div className="">
|
||||
<Clock />
|
||||
{recipe?.readyInMinutes || "N/A"} mins
|
||||
</div>
|
||||
<div className="info-item">
|
||||
<Users className="info-icon" />
|
||||
{recipe?.servings || '4'} servings
|
||||
<div className="">
|
||||
<Users />
|
||||
{recipe?.servings || (recipe?.readyInMinutes > 30 ? "6" : "2")}{" "}
|
||||
servings
|
||||
</div>
|
||||
{console.log(recipe?.servings)}
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,36 +1,34 @@
|
|||
/* eslint-disable react/no-unknown-property */
|
||||
/* eslint-disable react/prop-types */
|
||||
import { Clock, Users } from 'lucide-react';
|
||||
import { useRecipeNutrition } from '../../hooks/useCalorieUnits';
|
||||
import { closeIcon } from '../../svgIcons/svgIcons';
|
||||
import { Clock, Users, BookOpen } from "lucide-react";
|
||||
import { closeIcon } from "../../svgIcons/svgIcons";
|
||||
|
||||
export const RecipeDetails = ({ recipe, onClose }) => {
|
||||
const { data: nutrition, isLoading } = useRecipeNutrition(recipe?.id);
|
||||
|
||||
return (
|
||||
<div className="modal-overlay">
|
||||
<div className="modal-content">
|
||||
<button onClick={onClose} className="close-button">{closeIcon}</button>
|
||||
<button onClick={onClose} className="close-button">
|
||||
{closeIcon}
|
||||
</button>
|
||||
<h2 className="modal-title">{recipe?.title}</h2>
|
||||
<img src={recipe?.image} alt={recipe?.title} className="modal-image" />
|
||||
<div className="recipe-info">
|
||||
<div className="info-item">
|
||||
<Clock className="info-icon" />
|
||||
{recipe?.readyInMinutes} mins
|
||||
<Clock />
|
||||
{recipe?.readyInMinutes || "N/A"} mins
|
||||
</div>
|
||||
<div className="info-item">
|
||||
<Users className="info-icon" />
|
||||
{recipe?.servings} servings
|
||||
<Users />
|
||||
{recipe?.servings || (recipe?.readyInMinutes > 30 ? "6" : "2")}{" "}
|
||||
servings
|
||||
</div>
|
||||
{isLoading ? (
|
||||
<div>Loading nutrition info...</div>
|
||||
) : (
|
||||
nutrition && (
|
||||
<div className="info-item">
|
||||
<span>Amount: {nutrition?.amount}</span>
|
||||
<span>Units: {nutrition?.units}</span>
|
||||
<div>
|
||||
<span>
|
||||
<BookOpen /> <b>Directions</b>
|
||||
</span>
|
||||
<br />
|
||||
{recipe?.instructions || "N/A"}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -13,15 +13,13 @@
|
|||
|
||||
.search-container {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
max-width: 200px;
|
||||
margin: 0 auto 1rem;
|
||||
}
|
||||
|
||||
.search-input {
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
padding-left: 3rem;
|
||||
padding: 1rem 1rem 1rem 3rem;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
|
@ -39,6 +37,7 @@
|
|||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
|
@ -100,10 +99,6 @@
|
|||
align-items: center;
|
||||
}
|
||||
|
||||
.info-icon {
|
||||
margin-right: 0.25rem;
|
||||
}
|
||||
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
|
@ -154,17 +149,12 @@
|
|||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.loading {
|
||||
.loading,
|
||||
.error {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #dc2626;
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.filters-section{
|
||||
align-items: center;
|
||||
}
|
|
@ -1,34 +1,33 @@
|
|||
import { useReducer } from 'react';
|
||||
import { RecipeSearchBar } from '../components/SearchBar/index';
|
||||
import { RecipeFilters } from '../components/Filters/index';
|
||||
import { RecipeGrid } from '../components/RecipeGrid/index';
|
||||
import { RecipeDetails } from '../components/RecipeDetails/index';
|
||||
import { useRecipes } from '../hooks/useRecipe';
|
||||
import { initialState, recipeReducer } from '../store/RecipeStore';
|
||||
import '../components/RecipeFinder.css';
|
||||
import { useReducer } from "react";
|
||||
import { RecipeSearchBar } from "../components/SearchBar/index";
|
||||
import { RecipeFilters } from "../components/Filters/index";
|
||||
import { RecipeGrid } from "../components/RecipeGrid/index";
|
||||
import { RecipeDetails } from "../components/RecipeDetails/index";
|
||||
import { useDetailedRecipes } from "../hooks/useRecipe";
|
||||
import { initialState, recipeReducer } from "../store/RecipeStore";
|
||||
import "../components/RecipeFinder.css";
|
||||
|
||||
export const RecipeFinder = () => {
|
||||
const [state, dispatch] = useReducer(recipeReducer, initialState);
|
||||
const { data: recipes, isLoading, error } = useRecipes(
|
||||
state?.searchQuery,
|
||||
state?.filters
|
||||
);
|
||||
const {
|
||||
data: recipes,
|
||||
isLoading,
|
||||
error,
|
||||
} = useDetailedRecipes(state?.searchQuery, state?.filters);
|
||||
|
||||
const handleSearch = (query) => {
|
||||
dispatch({ type: 'SET_SEARCH', payload: query });
|
||||
dispatch({ type: "SET_SEARCH", payload: query });
|
||||
};
|
||||
|
||||
const handleFilterChange = (field, value) => {
|
||||
dispatch({ type: 'SET_FILTER', field, payload: value });
|
||||
dispatch({ type: "SET_FILTER", field, payload: value });
|
||||
};
|
||||
|
||||
const handleRecipeClick = (recipe) => {
|
||||
console.log("Recipe clicked:", recipe);
|
||||
dispatch({ type: 'SET_SELECTED_RECIPE', payload: recipe });
|
||||
dispatch({ type: "SET_SELECTED_RECIPE", payload: recipe });
|
||||
};
|
||||
|
||||
if (error) return <div className="error">Error loading recipes</div>
|
||||
|
||||
if (error) return <div className="error">Error loading recipes</div>;
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
|
@ -36,7 +35,10 @@ export const RecipeFinder = () => {
|
|||
<h1 className="title">Recipe Finder</h1>
|
||||
<div className="filters-section">
|
||||
<RecipeSearchBar onSearch={handleSearch} />
|
||||
<RecipeFilters filters={state?.filters} onFilterChange={handleFilterChange} />
|
||||
<RecipeFilters
|
||||
filters={state?.filters}
|
||||
onFilterChange={handleFilterChange}
|
||||
/>
|
||||
</div>
|
||||
{isLoading ? (
|
||||
<div className="loading">Loading recipes...</div>
|
||||
|
@ -47,7 +49,9 @@ export const RecipeFinder = () => {
|
|||
{state?.selectedRecipe && (
|
||||
<RecipeDetails
|
||||
recipe={state?.selectedRecipe}
|
||||
onClose={() => dispatch({ type: 'SET_SELECTED_RECIPE', payload: null })}
|
||||
onClose={() =>
|
||||
dispatch({ type: "SET_SELECTED_RECIPE", payload: null })
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* eslint-disable react/prop-types */
|
||||
import { RecipeDetailsCard } from '../RecipeCard/index';
|
||||
import { RecipeDetailsCard } from "../RecipeCard/index";
|
||||
|
||||
export const RecipeGrid = ({ recipes, onRecipeClick }) => (
|
||||
<div className="recipe-grid">
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* eslint-disable react/prop-types */
|
||||
import { Search } from 'lucide-react';
|
||||
import { Search } from "lucide-react";
|
||||
|
||||
export const RecipeSearchBar = ({ onSearch }) => (
|
||||
<div className="search-container">
|
||||
|
|
9
src/components/apiUtils/allApi.js
Normal file
9
src/components/apiUtils/allApi.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
export const fetchRecipeDetails = async (id) => {
|
||||
const response = await fetch(
|
||||
`https://api.spoonacular.com/recipes/${id}/information?includeNutrition=false&apiKey=2bed23978b4d417081acee70da2f9f5f`
|
||||
);
|
||||
if (!response?.ok) {
|
||||
throw new Error("Failed to fetch recipe details");
|
||||
}
|
||||
return await response?.json();
|
||||
};
|
|
@ -1,17 +0,0 @@
|
|||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export const useRecipeNutrition = (recipeId) => {
|
||||
return useQuery({
|
||||
queryKey: ['recipeNutrition', recipeId],
|
||||
queryFn: async () => {
|
||||
const response = await fetch(
|
||||
`https://api.spoonacular.com/recipes/${recipeId}/nutritionWidget.json?apiKey=a2f1ea26b02d4919b35c7152b5ebac6d`
|
||||
);
|
||||
if (!response?.ok) {
|
||||
throw new Error("Failed to fetch nutrition data");
|
||||
}
|
||||
return response?.json();
|
||||
},
|
||||
enabled: !!recipeId,
|
||||
});
|
||||
};
|
|
@ -1,11 +1,13 @@
|
|||
import { useQuery } from '@tanstack/react-query';
|
||||
/* eslint-disable no-unsafe-optional-chaining */
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { fetchRecipeDetails } from "../components/apiUtils/allApi";
|
||||
|
||||
export const useRecipes = (searchQuery, filters) => {
|
||||
export const useDetailedRecipes = (searchQuery, filters) => {
|
||||
return useQuery({
|
||||
queryKey: ['recipes', searchQuery, filters],
|
||||
queryKey: ["detailedRecipes", searchQuery, filters],
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams({
|
||||
apiKey: 'a2f1ea26b02d4919b35c7152b5ebac6d',
|
||||
apiKey: "2bed23978b4d417081acee70da2f9f5f",
|
||||
query: searchQuery,
|
||||
cuisine: filters?.cuisine,
|
||||
...(filters?.diet && { diet: filters?.diet }),
|
||||
|
@ -13,10 +15,15 @@ export const useRecipes = (searchQuery, filters) => {
|
|||
});
|
||||
|
||||
const response = await fetch(
|
||||
`https://api.spoonacular.com/recipes/complexSearch?${params}&_start=0&_limit=100`
|
||||
`https://api.spoonacular.com/recipes/complexSearch?${params}`
|
||||
);
|
||||
const data = await response?.json();
|
||||
return data?.results;
|
||||
const { results } = await response?.json();
|
||||
|
||||
const detailedRecipes = await Promise?.all(
|
||||
results?.map((recipe) => fetchRecipeDetails(recipe?.id))
|
||||
);
|
||||
|
||||
return detailedRecipes;
|
||||
},
|
||||
enabled: true,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue