-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from HungrySlugs-CSE115A/searchbar
Added slightly buggy search bar to each dining hall page that filters each food
- Loading branch information
Showing
5 changed files
with
402 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"use client"; | ||
import React, { useState, useEffect } from 'react'; | ||
import axios from 'axios'; | ||
|
||
interface Food { | ||
name: string; | ||
} | ||
|
||
interface subCategory { | ||
name: string; | ||
foods: Array<Food>; | ||
} | ||
|
||
interface Category { | ||
name: string; | ||
sub_categories: Array<subCategory>; | ||
} | ||
|
||
interface DiningHall { | ||
name: string; | ||
categories: Category[]; | ||
} | ||
|
||
const BarebonesComponent = () => { | ||
const [dhs, setDhs] = useState<DiningHall[]>([]); | ||
const [searchInput, setSearchInput] = useState<string>(''); | ||
const [filteredFoods, setFilteredFoods] = useState<{ food: Food; dhName: string; categoryName: string }[]>([]); | ||
const [showSearchResults, setShowSearchResults] = useState<boolean>(false); | ||
const [noFoodsFound, setNoFoodsFound] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
axios | ||
.get('http://localhost:8000/myapi/dining-halls/') | ||
.then((response) => { | ||
const dhsData: DiningHall[] = response.data.locations; | ||
setDhs(dhsData); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
}, []); | ||
|
||
const handleSearchInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setSearchInput(event.target.value); | ||
}; | ||
|
||
const handleSearch = () => { | ||
const allFoods: { food: Food; dhName: string; categoryName: string }[] = []; | ||
dhs.forEach((dh) => { | ||
dh.categories.forEach((category) => { | ||
category.sub_categories.forEach((subCategory) => { | ||
subCategory.foods.forEach((food) => { | ||
allFoods.push({ food, dhName: dh.name, categoryName: category.name }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
const filtered = allFoods.filter(({ food }) => food.name.toLowerCase().includes(searchInput.toLowerCase())); | ||
|
||
setNoFoodsFound(filtered.length === 0); | ||
setFilteredFoods(filtered); | ||
setShowSearchResults(true); | ||
}; | ||
|
||
return ( | ||
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}> | ||
{/* Title */} | ||
<h1 className="text-8xl">Welcome to Hungry Slugs!</h1> | ||
{/* Search bar */} | ||
<div className="search-bar" style={{ marginTop: '20px' }}> {/* Adjust margin as needed */} | ||
<input | ||
type="text" | ||
placeholder="Search foods..." | ||
value={searchInput} | ||
onChange={handleSearchInputChange} | ||
/> | ||
<button onClick={handleSearch}>Search</button> | ||
</div> | ||
{/* Display search results if button clicked */} | ||
{showSearchResults && ( | ||
<div> | ||
<h3>Search Results:</h3> | ||
<ul> | ||
{filteredFoods.map(({ food, dhName, categoryName }, index) => ( | ||
<li key={index}> | ||
{food.name} - {categoryName} ({dhName}) | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
)} | ||
|
||
{noFoodsFound && ( | ||
<div> | ||
<h3>No foods found at this dining hall.</h3> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default BarebonesComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.