Skip to content

Commit b9f94a2

Browse files
committed
Fix search bar
1 parent 659967a commit b9f94a2

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Diff for: app/javascript/components/SearchBar.jsx

+32-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ const SearchBar = () => {
6969
setSuggestions([]);
7070
};
7171

72+
// Handle ingredient removal from the selected list
73+
const handleIngredientRemove = (ingredientId) => {
74+
const updatedIngredients = selectedIngredients.filter(
75+
(ingredient) => ingredient.id !== ingredientId
76+
);
77+
setSelectedIngredients(updatedIngredients);
78+
fetchRecipes(updatedIngredients.map((ing) => ing.id), matchType);
79+
};
80+
7281
// Handle match type change and re-fetch recipes
7382
const handleMatchTypeChange = (type) => {
7483
setMatchType(type);
@@ -79,7 +88,6 @@ const SearchBar = () => {
7988

8089
return (
8190
<div className="w-full bg-white p-8 rounded-lg shadow-md max-w-full mt-8 mx-auto">
82-
8391
{/* Input Field */}
8492
<div className="relative mb-6">
8593
<input
@@ -104,6 +112,29 @@ const SearchBar = () => {
104112
)}
105113
</div>
106114

115+
{/* Selected Ingredients Section */}
116+
{selectedIngredients.length > 0 && (
117+
<div className="mb-6">
118+
<h5 className="font-semibold mb-2">Selected Ingredients:</h5>
119+
<div className="flex flex-wrap gap-2">
120+
{selectedIngredients.map((ingredient) => (
121+
<div
122+
key={ingredient.id}
123+
className="flex items-center p-2 bg-blue-100 rounded-lg text-sm"
124+
>
125+
{ingredient.name}
126+
<button
127+
onClick={() => handleIngredientRemove(ingredient.id)}
128+
className="ml-2 text-red-500 hover:text-red-700"
129+
>
130+
&times;
131+
</button>
132+
</div>
133+
))}
134+
</div>
135+
</div>
136+
)}
137+
107138
{/* Match Type Selection */}
108139
<div className="mb-6">
109140
<div className="flex items-center gap-6">

0 commit comments

Comments
 (0)