diff --git a/app/global_search/page.tsx b/app/global_search/page.tsx index 6998cb5..b72468b 100644 --- a/app/global_search/page.tsx +++ b/app/global_search/page.tsx @@ -1,153 +1,230 @@ "use client"; import React, { useState, useEffect } from "react"; import axios from "axios"; -import styles from "./Search.module.css"; import Image from "next/image"; +import Link from "next/link"; +import styles from "./Search.module.css"; +import { fetchLocations } from "@/app/db"; import { Location, Food } from "@/interfaces/Location"; -const BarebonesComponent = () => { - const [dhs, setDhs] = useState([]); +interface FoodWithCategory { + food: Food; + category: string; + diningHall: string; // Added dining hall name +} + +const restrictions: string[] = [ + "eggs", + "vegan", + "fish", + "veggie", + "gluten", + "pork", + "milk", + "beef", + "nuts", + "halal", + "soy", + "shellfish", + "treenut", + "sesame", + "alcohol", +]; + +const GlobalSearch = () => { + const [locations, setLocations] = useState([]); + const [foods, setFoods] = useState([]); const [searchInput, setSearchInput] = useState(""); - const [filteredFoods, setFilteredFoods] = useState< - { food: Food; dhName: string; categoryName: string }[] - >([]); - const [showSearchResults, setShowSearchResults] = useState(false); - const [noFoodsFound, setNoFoodsFound] = useState(false); - - // Retrieve hide and show allergies from local storage - const [selectedHideAllergies, setSelectedHideAllergies] = useState( - () => { - const storedHideAllergies = localStorage.getItem("hideAllergies"); - return storedHideAllergies ? JSON.parse(storedHideAllergies) : []; - }, - ); - - const [selectedShowAllergies, setSelectedShowAllergies] = useState( + const [foundFoods, setFoundFoods] = useState([]); + const [isFilterPopupOpen, setIsFilterPopupOpen] = useState(false); + const [selectedRestrictions, setSelectedRestrictions] = useState( () => { - const storedShowAllergies = localStorage.getItem("showAllergies"); - return storedShowAllergies ? JSON.parse(storedShowAllergies) : []; - }, + const storedRestrictions = localStorage.getItem("selectedRestrictions"); + return storedRestrictions ? JSON.parse(storedRestrictions) : []; + } ); + const [filterApplied, setFilterApplied] = useState(false); useEffect(() => { - axios - .get("http://localhost:8000/api/locations/") - .then((response) => { - const dhsData: Location[] = response.data.locations; - setDhs(dhsData); - }) - .catch((error) => { - console.log(error); - }); + fetchLocations().then((locations: Location[]) => { + setLocations(locations); + + const allFoods = locations.flatMap(location => + location.categories.flatMap(category => + category.sub_categories.flatMap(subCategory => + subCategory.foods.map(food => ({ + food: food, + category: category.name, + diningHall: location.name // Added dining hall name + })) + ) + ) + ); + setFoods(allFoods); + }); }, []); - const handleSearchInputChange = ( - event: React.ChangeEvent, - ) => { - setSearchInput(event.target.value); - }; - - const handleFilter = () => { - window.location.href = "Filter-Window"; - }; - - 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 searchForFood = (food_name: string) => { + const foundFoods = foods.filter(foodWithCategory => + foodWithCategory.food.name + .toLowerCase() + .includes(food_name.toLowerCase()) + ); - const filtered = allFoods.filter(({ food }) => - food.name.toLowerCase().includes(searchInput.toLowerCase()), + const filteredFoods = foundFoods.filter(({ food }) => + selectedRestrictions.every(restriction => + food.restrictions.includes(restriction) + ) ); - // Check if all boxes are unchecked - const allBoxesUnchecked = - selectedShowAllergies.length === 0 && selectedHideAllergies.length === 0; + setFoundFoods(filteredFoods); + }; - let finalFilteredFoods = filtered; - if (!allBoxesUnchecked) { - // Filter foods based on selectedShowAllergies and selectedHideAllergies - finalFilteredFoods = filtered.filter(({ food }) => { - const hasShowAllergy = - selectedShowAllergies.length === 0 || - selectedShowAllergies.every((allergy) => - food.name.toLowerCase().includes(allergy.toLowerCase()), - ); + const toggleFilterPopup = () => { + setIsFilterPopupOpen(!isFilterPopupOpen); + }; - const hasHideAllergy = selectedHideAllergies.some((allergy) => - food.restrictions.includes(allergy.toLowerCase()), - ); - return hasShowAllergy && !hasHideAllergy; - }); - } + const handleRestrictionChange = (restriction: string, checked: boolean) => { + const newRestrictions = checked + ? [...selectedRestrictions, restriction] + : selectedRestrictions.filter(r => r !== restriction); + setSelectedRestrictions(newRestrictions); + localStorage.setItem( + "selectedRestrictions", + JSON.stringify(newRestrictions) + ); + }; - setNoFoodsFound(finalFilteredFoods.length === 0); - setFilteredFoods(finalFilteredFoods); - setShowSearchResults(true); + const applyFilter = () => { + setFilterApplied(true); + searchForFood(searchInput); + toggleFilterPopup(); }; return ( -
-

Global Search

-
- - -
- Filter +
+
+
+

+ Global Search +

-
- - {showSearchResults && ( -
-

Search Results:

-
    - {filteredFoods.map(({ food, dhName, categoryName }, index) => ( -
  • - {food.name} - {categoryName} ({dhName}) -
    - {food.restrictions.map((restriction, index) => ( - {restriction} - ))} -
    -
  • - ))} -
+ +
+
+ setSearchInput(e.target.value)} + /> + +
- )} - - {noFoodsFound && ( -
-

No foods found.

+ +
+
- )} -
- ); -}; -export default BarebonesComponent; + {isFilterPopupOpen && ( +
+
+

+ Filter by Restrictions +

+
+ {restrictions.map(restriction => ( +
+ + handleRestrictionChange(restriction, e.target.checked) + } + /> + +
+ ))} +
+ + +
+
+ )} + +
+ {filterApplied && foundFoods.length === 0 ? ( +

No foods found with the specified allergy constraints

+ ) : ( + foundFoods.map((foodWithCategory, index) => ( +
+ +

{foodWithCategory.food.name}

+
+ {foodWithCategory.category} @ {foodWithCategory.diningHall} {/* Added dining hall name */} +
+ +
    + {foodWithCategory.food.restrictions.map( + (restriction, index) => ( +
  • + {restriction} +
  • + ) + )} +
+
+ )) + )} +
+
+
+ ); + }; + + export default GlobalSearch; +